From 47d72e9b5fe23ec803cc7f8da284ddca72493745 Mon Sep 17 00:00:00 2001 From: hansonr Date: Mon, 18 Jun 2018 16:18:02 +0200 Subject: [PATCH] JAL-3026 adding javajs package adding a general utility package for Java or JavaScript. --- src/javajs/J2SIgnoreImport.java | 7 + src/javajs/J2SRequireImport.java | 7 + src/javajs/_README.txt | 38 + src/javajs/api/BytePoster.java | 7 + src/javajs/api/EigenInterface.java | 15 + src/javajs/api/GenericBinaryDocument.java | 40 + src/javajs/api/GenericBinaryDocumentReader.java | 25 + src/javajs/api/GenericCifDataParser.java | 45 + src/javajs/api/GenericColor.java | 19 + src/javajs/api/GenericImageDialog.java | 9 + src/javajs/api/GenericImageEncoder.java | 12 + src/javajs/api/GenericLineReader.java | 5 + src/javajs/api/GenericOutputChannel.java | 22 + src/javajs/api/GenericZipInputStream.java | 13 + src/javajs/api/GenericZipTools.java | 45 + src/javajs/api/Interface.java | 54 + src/javajs/api/JSFunction.java | 12 + src/javajs/api/JSInterface.java | 29 + src/javajs/api/JSONEncodable.java | 7 + src/javajs/api/ResettableStream.java | 7 + src/javajs/api/ZInputStream.java | 6 + src/javajs/api/js/J2SObjectInterface.java | 12 + src/javajs/api/js/JSInterface.java | 41 + src/javajs/export/PDFCreator.java | 367 +++++ src/javajs/export/PDFObject.java | 152 +++ src/javajs/img/BMPDecoder.java | 211 +++ src/javajs/img/CRCEncoder.java | 110 ++ src/javajs/img/GifEncoder.java | 1142 ++++++++++++++++ src/javajs/img/ImageEncoder.java | 127 ++ src/javajs/img/Jpg64Encoder.java | 64 + src/javajs/img/JpgEncoder.java | 1208 +++++++++++++++++ src/javajs/img/PdfEncoder.java | 108 ++ src/javajs/img/PngEncoder.java | 480 +++++++ src/javajs/img/PpmEncoder.java | 60 + src/javajs/util/A4.java | 258 ++++ src/javajs/util/AU.java | 551 ++++++++ src/javajs/util/AjaxURLConnection.java | 122 ++ src/javajs/util/AjaxURLStreamHandler.java | 53 + src/javajs/util/AjaxURLStreamHandlerFactory.java | 31 + src/javajs/util/ArrayDataReader.java | 57 + src/javajs/util/BArray.java | 33 + src/javajs/util/BC.java | 187 +++ src/javajs/util/BS.java | 960 ++++++++++++++ src/javajs/util/Base64.java | 120 ++ src/javajs/util/BinaryDocument.java | 352 +++++ src/javajs/util/CU.java | 555 ++++++++ src/javajs/util/CifDataParser.java | 905 +++++++++++++ src/javajs/util/CompoundDocDirEntry.java | 96 ++ src/javajs/util/CompoundDocHeader.java | 115 ++ src/javajs/util/CompoundDocument.java | 395 ++++++ src/javajs/util/DF.java | 164 +++ src/javajs/util/DataReader.java | 56 + src/javajs/util/DebugJS.java | 28 + src/javajs/util/Eigen.java | 1063 +++++++++++++++ src/javajs/util/Encoding.java | 33 + src/javajs/util/JSAudioThread.java | 258 ++++ src/javajs/util/JSJSONParser.java | 328 +++++ src/javajs/util/JSONException.java | 13 + src/javajs/util/JSThread.java | 214 +++ src/javajs/util/LimitedLineReader.java | 48 + src/javajs/util/ListDataReader.java | 57 + src/javajs/util/Lst.java | 56 + src/javajs/util/M3.java | 633 +++++++++ src/javajs/util/M34.java | 417 ++++++ src/javajs/util/M4.java | 940 +++++++++++++ src/javajs/util/Matrix.java | 457 +++++++ src/javajs/util/Measure.java | 729 ++++++++++ src/javajs/util/MessagePackReader.java | 703 ++++++++++ src/javajs/util/OC.java | 418 ++++++ src/javajs/util/P3.java | 59 + src/javajs/util/P3i.java | 44 + src/javajs/util/P4.java | 63 + src/javajs/util/PT.java | 1543 ++++++++++++++++++++++ src/javajs/util/Quat.java | 818 ++++++++++++ src/javajs/util/Rdr.java | 560 ++++++++ src/javajs/util/SB.java | 357 +++++ src/javajs/util/StringDataReader.java | 51 + src/javajs/util/T3.java | 333 +++++ src/javajs/util/T3d.java | 224 ++++ src/javajs/util/T3i.java | 150 +++ src/javajs/util/T4.java | 113 ++ src/javajs/util/V3.java | 72 + src/javajs/util/V3d.java | 94 ++ src/javajs/util/XmlUtil.java | 171 +++ src/javajs/util/ZipData.java | 79 ++ src/javajs/util/ZipTools.java | 456 +++++++ 86 files changed, 21098 insertions(+) create mode 100644 src/javajs/J2SIgnoreImport.java create mode 100644 src/javajs/J2SRequireImport.java create mode 100644 src/javajs/_README.txt create mode 100644 src/javajs/api/BytePoster.java create mode 100644 src/javajs/api/EigenInterface.java create mode 100644 src/javajs/api/GenericBinaryDocument.java create mode 100644 src/javajs/api/GenericBinaryDocumentReader.java create mode 100644 src/javajs/api/GenericCifDataParser.java create mode 100644 src/javajs/api/GenericColor.java create mode 100644 src/javajs/api/GenericImageDialog.java create mode 100644 src/javajs/api/GenericImageEncoder.java create mode 100644 src/javajs/api/GenericLineReader.java create mode 100644 src/javajs/api/GenericOutputChannel.java create mode 100644 src/javajs/api/GenericZipInputStream.java create mode 100644 src/javajs/api/GenericZipTools.java create mode 100644 src/javajs/api/Interface.java create mode 100644 src/javajs/api/JSFunction.java create mode 100644 src/javajs/api/JSInterface.java create mode 100644 src/javajs/api/JSONEncodable.java create mode 100644 src/javajs/api/ResettableStream.java create mode 100644 src/javajs/api/ZInputStream.java create mode 100644 src/javajs/api/js/J2SObjectInterface.java create mode 100644 src/javajs/api/js/JSInterface.java create mode 100644 src/javajs/export/PDFCreator.java create mode 100644 src/javajs/export/PDFObject.java create mode 100644 src/javajs/img/BMPDecoder.java create mode 100644 src/javajs/img/CRCEncoder.java create mode 100644 src/javajs/img/GifEncoder.java create mode 100644 src/javajs/img/ImageEncoder.java create mode 100644 src/javajs/img/Jpg64Encoder.java create mode 100644 src/javajs/img/JpgEncoder.java create mode 100644 src/javajs/img/PdfEncoder.java create mode 100644 src/javajs/img/PngEncoder.java create mode 100644 src/javajs/img/PpmEncoder.java create mode 100644 src/javajs/util/A4.java create mode 100644 src/javajs/util/AU.java create mode 100644 src/javajs/util/AjaxURLConnection.java create mode 100644 src/javajs/util/AjaxURLStreamHandler.java create mode 100644 src/javajs/util/AjaxURLStreamHandlerFactory.java create mode 100644 src/javajs/util/ArrayDataReader.java create mode 100644 src/javajs/util/BArray.java create mode 100644 src/javajs/util/BC.java create mode 100644 src/javajs/util/BS.java create mode 100644 src/javajs/util/Base64.java create mode 100644 src/javajs/util/BinaryDocument.java create mode 100644 src/javajs/util/CU.java create mode 100644 src/javajs/util/CifDataParser.java create mode 100644 src/javajs/util/CompoundDocDirEntry.java create mode 100644 src/javajs/util/CompoundDocHeader.java create mode 100644 src/javajs/util/CompoundDocument.java create mode 100644 src/javajs/util/DF.java create mode 100644 src/javajs/util/DataReader.java create mode 100644 src/javajs/util/DebugJS.java create mode 100644 src/javajs/util/Eigen.java create mode 100644 src/javajs/util/Encoding.java create mode 100644 src/javajs/util/JSAudioThread.java create mode 100644 src/javajs/util/JSJSONParser.java create mode 100644 src/javajs/util/JSONException.java create mode 100644 src/javajs/util/JSThread.java create mode 100644 src/javajs/util/LimitedLineReader.java create mode 100644 src/javajs/util/ListDataReader.java create mode 100644 src/javajs/util/Lst.java create mode 100644 src/javajs/util/M3.java create mode 100644 src/javajs/util/M34.java create mode 100644 src/javajs/util/M4.java create mode 100644 src/javajs/util/Matrix.java create mode 100644 src/javajs/util/Measure.java create mode 100644 src/javajs/util/MessagePackReader.java create mode 100644 src/javajs/util/OC.java create mode 100644 src/javajs/util/P3.java create mode 100644 src/javajs/util/P3i.java create mode 100644 src/javajs/util/P4.java create mode 100644 src/javajs/util/PT.java create mode 100644 src/javajs/util/Quat.java create mode 100644 src/javajs/util/Rdr.java create mode 100644 src/javajs/util/SB.java create mode 100644 src/javajs/util/StringDataReader.java create mode 100644 src/javajs/util/T3.java create mode 100644 src/javajs/util/T3d.java create mode 100644 src/javajs/util/T3i.java create mode 100644 src/javajs/util/T4.java create mode 100644 src/javajs/util/V3.java create mode 100644 src/javajs/util/V3d.java create mode 100644 src/javajs/util/XmlUtil.java create mode 100644 src/javajs/util/ZipData.java create mode 100644 src/javajs/util/ZipTools.java diff --git a/src/javajs/J2SIgnoreImport.java b/src/javajs/J2SIgnoreImport.java new file mode 100644 index 0000000..b92c82b --- /dev/null +++ b/src/javajs/J2SIgnoreImport.java @@ -0,0 +1,7 @@ +package javajs; + +public @interface J2SIgnoreImport { + + Class[] value(); + +} diff --git a/src/javajs/J2SRequireImport.java b/src/javajs/J2SRequireImport.java new file mode 100644 index 0000000..646cebd --- /dev/null +++ b/src/javajs/J2SRequireImport.java @@ -0,0 +1,7 @@ +package javajs; + +public @interface J2SRequireImport { + + Class[] value(); + +} diff --git a/src/javajs/_README.txt b/src/javajs/_README.txt new file mode 100644 index 0000000..e8c3779 --- /dev/null +++ b/src/javajs/_README.txt @@ -0,0 +1,38 @@ +The javajs directory contains a variety of Java files that you are +encouraged to use in your project. It is self-contained, and the +classes and methods all work in Java as well as JavaScript. + +These classes perform functions not standard in Java or provide +classes that are particularly optimized for JavaScript (fewer overloaded +methods, especially). + +-- creating (minimal) PDF files +-- decoding and encoding image files +-- working with binary, compound document, JSON, and zip data +-- providing very efficient classes for vectors, matricies, and quaternions +-- providing a java.lang.Thread subclass (JSThread) that can be used + wherever a thread is necessary; note that JSThread cannot sleep(), but + it provides a means of managing setTimeout callbacks that is very easy + to implement. +-- providing two very important annotations: + + @J2SIgnoreImport Indicates that this class will never be used in JavaScript + and thus does not need to be indicated as a dependency. In + some cases, this annotation is necessary just to break a + cyclical dependency that Java somehow handles, but J2S does not. + + @J2SRequireImport Allows a SwingJS developer to work around J2S transpiler + issues that for some reason it is not indicating the need for + a dependent class. Typically this is because a static call + to a static method in a class. + + In addition, though, @J2SRequireImport can be used to "inject" + JavaScript at a specific point in code loading. For example, + swingjs.plaf.JSSlider uses the following annotation to load + jQueryUI, fooling J2S to think that it is a "class" file. + + @J2SRequireImport(swingjs.jquery.JQueryUI.class) + + +All files in the javajs/util directory will be moved to j2s/JU by a build script. + diff --git a/src/javajs/api/BytePoster.java b/src/javajs/api/BytePoster.java new file mode 100644 index 0000000..0b0f29a --- /dev/null +++ b/src/javajs/api/BytePoster.java @@ -0,0 +1,7 @@ +package javajs.api; + +public interface BytePoster { + + String postByteArray(String fileName, byte[] bytes); + +} diff --git a/src/javajs/api/EigenInterface.java b/src/javajs/api/EigenInterface.java new file mode 100644 index 0000000..4a1b592 --- /dev/null +++ b/src/javajs/api/EigenInterface.java @@ -0,0 +1,15 @@ +package javajs.api; + +import javajs.util.V3; + +public interface EigenInterface { + + EigenInterface setM(double[][] n); + + double[] getEigenvalues(); + + void fillFloatArrays(V3[] eigenVectors, float[] eigenValues); + + float[][] getEigenvectorsFloatTransposed(); + +} diff --git a/src/javajs/api/GenericBinaryDocument.java b/src/javajs/api/GenericBinaryDocument.java new file mode 100644 index 0000000..823bd2a --- /dev/null +++ b/src/javajs/api/GenericBinaryDocument.java @@ -0,0 +1,40 @@ +package javajs.api; + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.InputStream; +import java.util.Map; + + +import javajs.util.SB; + +public interface GenericBinaryDocument extends GenericBinaryDocumentReader { + + GenericBinaryDocument setStream(BufferedInputStream bis, boolean isBigEndian); + + void setStreamData(DataInputStream dataInputStream, boolean isBigEndian); + + long getPosition(); + + SB getAllDataFiles(String binaryFileList, String firstFile); + + void getAllDataMapped(String replace, String string, Map fileData); + + int swapBytesI(int nx); + + short swapBytesS(short s); + + void seek(long i); + + void setOutputChannel(GenericOutputChannel out); + + InputStream getInputStream(); + + int readIntLE() throws Exception; + + int readByteArray(byte[] b, int off, int len) throws Exception; + + + void close(); + +} diff --git a/src/javajs/api/GenericBinaryDocumentReader.java b/src/javajs/api/GenericBinaryDocumentReader.java new file mode 100644 index 0000000..aea6f63 --- /dev/null +++ b/src/javajs/api/GenericBinaryDocumentReader.java @@ -0,0 +1,25 @@ +package javajs.api; + +public interface GenericBinaryDocumentReader { + + byte readByte() throws Exception; + + byte[] readBytes(int n) throws Exception; + + int readUInt8() throws Exception; + + int readInt() throws Exception; + + short readShort() throws Exception; + + int readUnsignedShort() throws Exception; + + long readLong() throws Exception; + + float readFloat() throws Exception; + + double readDouble() throws Exception; + + String readString(int i) throws Exception; + +} diff --git a/src/javajs/api/GenericCifDataParser.java b/src/javajs/api/GenericCifDataParser.java new file mode 100644 index 0000000..81a71f7 --- /dev/null +++ b/src/javajs/api/GenericCifDataParser.java @@ -0,0 +1,45 @@ +package javajs.api; + +import java.io.BufferedReader; +import java.util.Map; + + +public interface GenericCifDataParser { + + static final int NONE = -1; + + String fullTrim(String str); + + Map getAllCifData(); + + boolean getData() throws Exception; + + String getColumnName(int i); + + int getColumnCount(); + + String getFileHeader(); + + Object peekToken() throws Exception; + + Object getTokenPeeked(); + + Object getColumnData(int i); + + Object getNextDataToken() throws Exception; + + String getNextToken() throws Exception; + + void parseDataBlockParameters(String[] fields, String key, String data, int[] key2col, int[] col2key) throws Exception; + + String readLine(); + + GenericCifDataParser set(GenericLineReader reader, BufferedReader br, boolean debugging); + + String toUnicode(String data); + + String skipLoop(boolean doReport) throws Exception; + + String fixKey(String key); + +} diff --git a/src/javajs/api/GenericColor.java b/src/javajs/api/GenericColor.java new file mode 100644 index 0000000..57cf169 --- /dev/null +++ b/src/javajs/api/GenericColor.java @@ -0,0 +1,19 @@ +package javajs.api; + +/** + * GenericColor allows both java.awt.Color and javajs.awt.Color to be + * handled by methods that need not distinguish between them. It is used + * in the javajs package for the background color of a javajs.swing.JComponent + * + * @author hansonr + * + */ +public interface GenericColor { + + int getRGB(); + + int getOpacity255(); + + void setOpacity255(int a); + +} diff --git a/src/javajs/api/GenericImageDialog.java b/src/javajs/api/GenericImageDialog.java new file mode 100644 index 0000000..54d8503 --- /dev/null +++ b/src/javajs/api/GenericImageDialog.java @@ -0,0 +1,9 @@ +package javajs.api; + +public interface GenericImageDialog { + + void closeMe(); + + void setImage(Object image); + +} diff --git a/src/javajs/api/GenericImageEncoder.java b/src/javajs/api/GenericImageEncoder.java new file mode 100644 index 0000000..9aa7477 --- /dev/null +++ b/src/javajs/api/GenericImageEncoder.java @@ -0,0 +1,12 @@ +package javajs.api; + +import java.util.Map; + +import javajs.util.OC; + +public interface GenericImageEncoder { + + public boolean createImage(String type, OC out, + Map params) throws Exception; + +} diff --git a/src/javajs/api/GenericLineReader.java b/src/javajs/api/GenericLineReader.java new file mode 100644 index 0000000..f113a8b --- /dev/null +++ b/src/javajs/api/GenericLineReader.java @@ -0,0 +1,5 @@ +package javajs.api; + +public interface GenericLineReader { + public String readNextLine() throws Exception; +} diff --git a/src/javajs/api/GenericOutputChannel.java b/src/javajs/api/GenericOutputChannel.java new file mode 100644 index 0000000..73649d7 --- /dev/null +++ b/src/javajs/api/GenericOutputChannel.java @@ -0,0 +1,22 @@ +package javajs.api; + +public interface GenericOutputChannel { + + boolean isBigEndian(); + + void writeByteAsInt(int b); + + void write(byte[] b, int off, int n); + + void writeInt(int i); + + void reset(); + + String closeChannel(); + + void writeLong(long b); + + void writeShort(short i); + + +} diff --git a/src/javajs/api/GenericZipInputStream.java b/src/javajs/api/GenericZipInputStream.java new file mode 100644 index 0000000..e2e8018 --- /dev/null +++ b/src/javajs/api/GenericZipInputStream.java @@ -0,0 +1,13 @@ +package javajs.api; + + +import java.io.InputStream; +import java.util.zip.ZipInputStream; + +public class GenericZipInputStream extends ZipInputStream implements ZInputStream { + + public GenericZipInputStream(InputStream in) { + super(in); + } + +} \ No newline at end of file diff --git a/src/javajs/api/GenericZipTools.java b/src/javajs/api/GenericZipTools.java new file mode 100644 index 0000000..a0b0993 --- /dev/null +++ b/src/javajs/api/GenericZipTools.java @@ -0,0 +1,45 @@ +package javajs.api; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; + +import java.util.Map; + + +public interface GenericZipTools { + + public ZInputStream newZipInputStream(InputStream is); + + public String getZipDirectoryAsStringAndClose(BufferedInputStream t); + + public InputStream newGZIPInputStream(InputStream bis) throws IOException; + + public Object getZipFileDirectory(BufferedInputStream bis, + String[] subFileList, int listPtr, boolean asBufferedInputStream); + + public String[] getZipDirectoryAndClose(BufferedInputStream t, + String manifestID); + + public void getAllZipData(InputStream bis, String[] subFileList, + String replace, String binaryFileList, String exclude, + Map fileData); + + public Object getZipFileContentsAsBytes(BufferedInputStream bis, + String[] subFileList, int i); + + public void addZipEntry(Object zos, String fileName) throws IOException; + + public void closeZipEntry(Object zos) throws IOException; + + public Object getZipOutputStream(Object bos); + + public int getCrcValue(byte[] bytes); + + public void readFileAsMap(BufferedInputStream is, Map bdata, String name); + + public String cacheZipContents(BufferedInputStream bis, String shortName, + Map cache, boolean asByteArray); + + BufferedInputStream getUnGzippedInputStream(byte[] bytes); +} diff --git a/src/javajs/api/Interface.java b/src/javajs/api/Interface.java new file mode 100644 index 0000000..418d528 --- /dev/null +++ b/src/javajs/api/Interface.java @@ -0,0 +1,54 @@ +/* $RCSfile$ + * $Author$ + * $Date$ + * $Revision$ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2006 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package javajs.api; + +public class Interface { + + public static Object getInterface(String name) { + try { + Class x = Class.forName(name); + return (x == null ? null : x.newInstance()); + } catch (Exception e) { + System.out.println("Interface.getInterface Error creating instance for " + name + ": \n" + e); + return null; + } + } + + public static Object getInstanceWithParams(String name, Class[] classes, Object... params) { + try { + Class cl = Class.forName(name); + return cl.getConstructor(classes).newInstance(params); + } catch (Exception e) { + System.out.println("Interface.getInterfaceWithParams Error creating instance for " + name + ": \n" + e); + return null; + } + } + + +} diff --git a/src/javajs/api/JSFunction.java b/src/javajs/api/JSFunction.java new file mode 100644 index 0000000..1c6c108 --- /dev/null +++ b/src/javajs/api/JSFunction.java @@ -0,0 +1,12 @@ +package javajs.api; + +/** + * A flag that this object is really a JavaScript function that, for example, + * might be called from setTimeout(). + * + * @author Bob Hanson + * + */ +public interface JSFunction { + +} diff --git a/src/javajs/api/JSInterface.java b/src/javajs/api/JSInterface.java new file mode 100644 index 0000000..d824543 --- /dev/null +++ b/src/javajs/api/JSInterface.java @@ -0,0 +1,29 @@ +package javajs.api; + +/** + * called by JSmol JavaScript methods using + * + * this._applet.xxxx() + * + */ +public interface JSInterface { + + int cacheFileByName(String fileName, boolean isAdd); + void cachePut(String key, Object data); + void destroy(); + String getFullName(); + void openFileAsyncSpecial(String fileName, int flags); + boolean processMouseEvent(int id, int x, int y, int modifiers, long time); + void processTwoPointGesture(float[][][] touches); + void setDisplay(Object canvas); + void setScreenDimension(int width, int height); + boolean setStatusDragDropped(int mode, int x, int y, String fileName); + void startHoverWatcher(boolean enable); + + // these are not general methods +//Object getGLmolView(); +//String loadInlineString(String mol, String script, boolean isAppend); +//String openFile(String fileName); + +} + diff --git a/src/javajs/api/JSONEncodable.java b/src/javajs/api/JSONEncodable.java new file mode 100644 index 0000000..b013279 --- /dev/null +++ b/src/javajs/api/JSONEncodable.java @@ -0,0 +1,7 @@ +package javajs.api; + +public interface JSONEncodable { + + String toJSON(); + +} diff --git a/src/javajs/api/ResettableStream.java b/src/javajs/api/ResettableStream.java new file mode 100644 index 0000000..5ded55f --- /dev/null +++ b/src/javajs/api/ResettableStream.java @@ -0,0 +1,7 @@ +package javajs.api; + +public interface ResettableStream { + + void resetStream(); + +} diff --git a/src/javajs/api/ZInputStream.java b/src/javajs/api/ZInputStream.java new file mode 100644 index 0000000..a3310f6 --- /dev/null +++ b/src/javajs/api/ZInputStream.java @@ -0,0 +1,6 @@ +package javajs.api; + +public interface ZInputStream { + // placeholder for ZipInputStream not requiring direct access to java.util.zip + +} diff --git a/src/javajs/api/js/J2SObjectInterface.java b/src/javajs/api/js/J2SObjectInterface.java new file mode 100644 index 0000000..fbe126d --- /dev/null +++ b/src/javajs/api/js/J2SObjectInterface.java @@ -0,0 +1,12 @@ +package javajs.api.js; + +/** + * methods in JSmol JavaScript accessed in Jmol (aka J2S) + */ +public interface J2SObjectInterface { + + Object _doAjax(Object url, String postOut, Object bytesOrStringOut, boolean isBinary); + + void _apply(Object func, Object data); + +} diff --git a/src/javajs/api/js/JSInterface.java b/src/javajs/api/js/JSInterface.java new file mode 100644 index 0000000..9b26631 --- /dev/null +++ b/src/javajs/api/js/JSInterface.java @@ -0,0 +1,41 @@ +package javajs.api.js; + +/** + * called by JSmol JavaScript methods using + * + * this._applet.xxxx() + * + */ +public interface JSInterface { + + int cacheFileByName(String fileName, boolean isAdd); + + void cachePut(String key, Object data); + + void destroy(); + + String getFullName(); + + void openFileAsyncSpecial(String fileName, int flags); + + boolean processMouseEvent(int id, int x, int y, int modifiers, long time); + + void processTwoPointGesture(float[][][] touches); + + void setDisplay(Object canvas); + + void setScreenDimension(int width, int height); + + boolean setStatusDragDropped(int mode, int x, int y, String fileName); + + void startHoverWatcher(boolean enable); + + + + // these are not general methods + // Object getGLmolView(); + // String loadInlineString(String mol, String script, boolean isAppend); + // String openFile(String fileName); + +} + diff --git a/src/javajs/export/PDFCreator.java b/src/javajs/export/PDFCreator.java new file mode 100644 index 0000000..8306d94 --- /dev/null +++ b/src/javajs/export/PDFCreator.java @@ -0,0 +1,367 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2009-06-30 18:58:33 -0500 (Tue, 30 Jun 2009) $ + * $Revision: 11158 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2002-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.export; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Hashtable; +import java.util.Map; +import java.util.Map.Entry; + +import javajs.util.Lst; +import javajs.util.SB; + + + +public class PDFCreator { + + private OutputStream os; + private Lst indirectObjects; + private PDFObject root; + private PDFObject graphics; +// private PDFObject pageResources; +// private PDFObject graphicsResources; + + private int pt; + private int xrefPt; + private int count; + + private int height; + private int width; + + private Mapfonts; + + public PDFCreator() { + // for Class.forName + } + + public void setOutputStream(OutputStream os) { + this.os = os; + } + + public void newDocument(int paperWidth, int paperHeight, boolean isLandscape) { + width = (isLandscape ? paperHeight : paperWidth); + height = (isLandscape ? paperWidth : paperHeight); + System.out.println("Creating PDF with width=" + width + " and height=" + height); + fonts = new Hashtable(); + indirectObjects = new Lst(); + //graphicsResources = newObject(null); + //pageResources = newObject(null); // will set this to compressed stream later + root = newObject("Catalog"); + PDFObject pages = newObject("Pages"); + PDFObject page = newObject("Page"); + PDFObject pageContents = newObject(null); + graphics = newObject("XObject"); + + root.addDef("Pages", pages.getRef()); + pages.addDef("Count", "1"); + pages.addDef("Kids", "[ " + page.getRef() +" ]"); + page.addDef("Parent", pages.getRef()); + page.addDef("MediaBox", "[ 0 0 " + paperWidth + " " + paperHeight + " ]"); + if (isLandscape) + page.addDef("Rotate", "90"); + + pageContents.addDef("Length", "?"); + pageContents.append((isLandscape ? "q 0 1 1 0 0 0 " : "q 1 0 0 -1 0 "+(paperHeight))+" cm /" + graphics.getID() + " Do Q"); + page.addDef("Contents", pageContents.getRef()); + addProcSet(page); + addProcSet(graphics); + // will add fonts as well as they are needed + graphics.addDef("Subtype", "/Form"); + graphics.addDef("FormType", "1"); + graphics.addDef("BBox", "[0 0 " + width + " " + height + "]"); + graphics.addDef("Matrix", "[1 0 0 1 0 0]"); + graphics.addDef("Length", "?"); + page.addResource("XObject", graphics.getID(), graphics.getRef()); + g("q 1 w 1 J 1 j 10 M []0 d q "); // line width 1, line cap circle, line join circle, miter limit 10, solid + clip(0, 0, width, height); + } + + private void addProcSet(PDFObject o) { + o.addResource(null, "ProcSet", "[/PDF /Text /ImageB /ImageC /ImageI]"); + } + + private void clip(int x1, int y1, int x2, int y2) { + moveto(x1, y1); + lineto(x2, y1); + lineto(x2, y2); + lineto(x1, y2); + g("h W n"); + } + + public void moveto(int x, int y) { + g(x + " " + y + " m"); + } + + public void lineto(int x, int y) { + g(x + " " + y + " l"); + } + + private PDFObject newObject(String type) { + PDFObject o = new PDFObject(++count); + if (type != null) + o.addDef("Type", "/" + type); + indirectObjects.addLast(o); + return o; + } + + public void addInfo(Map data) { + Hashtable info = new Hashtable(); + for (Entry e: data.entrySet()) { + String value = "(" + e.getValue().replace(')','_').replace('(','_')+ ")"; + info.put(e.getKey(), value); + } + root.addDef("Info", info); + } + + private PDFObject addFontResource(String fname) { + PDFObject f = newObject("Font"); + fonts.put(fname, f); + f.addDef("BaseFont", fname); + f.addDef("Encoding", "/WinAnsiEncoding"); + f.addDef("Subtype", "/Type1"); + graphics.addResource("Font", f.getID(), f.getRef()); + return f; + } + + private Map images; + + public void addImageResource(Object newImage, int width, int height, int[] buffer, boolean isRGB) { + PDFObject imageObj = newObject("XObject"); + if (images == null) + images = new Hashtable(); + images.put(newImage, imageObj); + imageObj.addDef("Subtype", "/Image"); + imageObj.addDef("Length", "?"); + imageObj.addDef("ColorSpace", isRGB ? "/DeviceRGB" : "/DeviceGray"); + imageObj.addDef("BitsPerComponent", "8"); + imageObj.addDef("Width", "" + width); + imageObj.addDef("Height", "" + height); + graphics.addResource("XObject", imageObj.getID(), imageObj.getRef()); + int n = buffer.length; + byte[] stream = new byte[n * (isRGB ? 3 : 1)]; + if (isRGB) { + for (int i = 0, pt = 0; i < n; i++) { + stream[pt++] = (byte) ((buffer[i] >> 16) & 0xFF); + stream[pt++] = (byte) ((buffer[i] >> 8) & 0xFF); + stream[pt++] = (byte) (buffer[i] & 0xFF); + } + } else { + for (int i = 0; i < n; i++) + stream[i] = (byte) buffer[i]; + } + imageObj.setStream(stream); + graphics.addResource("XObject", imageObj.getID(), imageObj.getRef()); + } + + public void g(String cmd) { + graphics.append(cmd).appendC('\n'); + } + + private void output(String s) throws IOException { + byte[] b = s.getBytes(); + os.write(b, 0, b.length); + pt += b.length; + } + + public void closeDocument() throws IOException { + g("Q Q"); + outputHeader(); + writeObjects(); + writeXRefTable(); + writeTrailer(); + os.flush(); + os.close(); + } + + private void outputHeader() throws IOException { + output("%PDF-1.3\n%"); + byte[] b = new byte[] {-1, -1, -1, -1}; + os.write(b, 0, b.length); + pt += 4; + output("\n"); + } + + private void writeTrailer() throws IOException { + PDFObject trailer = new PDFObject(-2); + output("trailer"); + trailer.addDef("Size", "" + indirectObjects.size()); + trailer.addDef("Root", root.getRef()); + trailer.output(os); + output("startxref\n"); + output("" + xrefPt + "\n"); + output("%%EOF\n"); + } + + /** + * Write Font objects first. + * + * @throws IOException + */ + private void writeObjects() throws IOException { + int nObj = indirectObjects.size(); + for (int i = 0; i < nObj; i++) { + PDFObject o = indirectObjects.get(i); + if (!o.isFont()) + continue; + o.pt = pt; + pt += o.output(os); + } + for (int i = 0; i < nObj; i++) { + PDFObject o = indirectObjects.get(i); + if (o.isFont()) + continue; + o.pt = pt; + pt += o.output(os); + } + } + + private void writeXRefTable() throws IOException { + xrefPt = pt; + int nObj = indirectObjects.size(); + SB sb = new SB(); + // note trailing space, needed because \n is just one character + sb.append("xref\n0 " + (nObj + 1) + + "\n0000000000 65535 f\r\n"); + for (int i = 0; i < nObj; i++) { + PDFObject o = indirectObjects.get(i); + String s = "0000000000" + o.pt; + sb.append(s.substring(s.length() - 10)); + sb.append(" 00000 n\r\n"); + } + output(sb.toString()); + } + + public boolean canDoLineTo() { + return true; + } + + public void fill() { + g("f"); + } + + public void stroke() { + g("S"); + } + + public void doCircle(int x, int y, int r, boolean doFill) { + double d = r*4*(Math.sqrt(2)-1)/3; + double dx = x; + double dy = y; + g((dx + r) + " " + dy + " m"); + g((dx + r) + " " + (dy + d) + " " + (dx + d) + " " + (dy + r) + " " + (dx) + " " + (dy + r) + " " + " c"); + g((dx - d) + " " + (dy + r) + " " + (dx - r) + " " + (dy + d) + " " + (dx - r) + " " + (dy) + " c"); + g((dx - r) + " " + (dy - d) + " " + (dx - d) + " " + (dy - r) + " " + (dx) + " " + (dy - r) + " c"); + g((dx + d) + " " + (dy - r) + " " + (dx + r) + " " + (dy - d) + " " + (dx + r) + " " + (dy) + " c"); + g(doFill ? "f" : "s"); + } + + public void doPolygon(int[] axPoints, int[] ayPoints, int nPoints, boolean doFill) { + moveto(axPoints[0], ayPoints[0]); + for (int i = 1; i < nPoints; i++) + lineto(axPoints[i], ayPoints[i]); + g(doFill ? "f" : "s"); + } + + public void doRect(int x, int y, int width, int height, boolean doFill) { + g(x + " " + y + " " + width + " " + height + " re " + (doFill ? "f" : "s")); + } + + public void drawImage(Object image, int destX0, int destY0, + int destX1, int destY1, int srcX0, int srcY0, int srcX1, int srcY1) { + PDFObject imageObj = images.get(image); + if (imageObj == null) + return; + g("q"); + clip(destX0, destY0, destX1, destY1); + double iw = Double.parseDouble((String) imageObj.getDef("Width")); + double ih = Double.parseDouble((String) imageObj.getDef("Height")); + double dw = (destX1 - destX0 + 1); + double dh = (destY1 - destY0 + 1); + double sw = (srcX1 - srcX0 + 1); + double sh = (srcY1 - srcY0 + 1); + double scaleX = dw / sw; + double scaleY = dh / sh; + double transX = destX0 - srcX0 * scaleX; + double transY = destY0 + (ih - srcY0) * scaleY; + g(scaleX*iw + " 0 0 " + -scaleY*ih + " " + transX + " " + transY + " cm"); + g("/" + imageObj.getID() + " Do"); + g("Q"); + } + + public void drawStringRotated(String s, int x, int y, int angle) { + g("q " + getRotation(angle) + " " + x + " " + y + + " cm BT(" + s + ")Tj ET Q"); + } + + public String getRotation(int angle) { + float cos = 0, sin = 0; + switch (angle) { + case 0: + cos = 1; + break; + case 90: + sin = 1; + break; + case -90: + sin = -1; + break; + case 180: + cos = -1; + break; + default: + float a = (float) (angle * (Math.PI / 180)); + cos = (float) Math.cos(a); + sin = (float) Math.sin(a); + if (Math.abs(cos) < 0.0001) + cos = 0; + if (Math.abs(sin) < 0.0001) + sin = 0; + } + return cos + " " + sin + " " + sin + " " + -cos; + } + + public void setColor(float[] rgb, boolean isFill) { + g(rgb[0] + " " + rgb[1] + " " + rgb[2] + (isFill ? " rg" : " RG")); + } + + public void setFont(String fname, float size) { + PDFObject f = fonts.get(fname); + if (f == null) + f = addFontResource(fname); + g("/" + f.getID() + " " + size + " Tf"); + } + + public void setLineWidth(float width) { + g(width + " w"); + } + + public void translateScale(float x, float y, float scale) { + g(scale + " 0 0 " + scale + " " + x + " " + y + " cm"); + } + +} diff --git a/src/javajs/export/PDFObject.java b/src/javajs/export/PDFObject.java new file mode 100644 index 0000000..176dc19 --- /dev/null +++ b/src/javajs/export/PDFObject.java @@ -0,0 +1,152 @@ +package javajs.export; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Hashtable; +import java.util.Map; +import java.util.Map.Entry; +import java.util.zip.Deflater; +import java.util.zip.DeflaterOutputStream; + +import javajs.util.SB; + + +/** + * A rudimentary class for working with PDF document creation. + * Written from scratch based on PDF Reference 13. + * + * @author hansonr Bob Hanson hansonr@stolaf.edu 10/28/2013 + * + */ +class PDFObject extends SB { + private Map dictionary; + private byte[] stream; + private int index; + String type; + int len; + int pt; + + PDFObject(int index) { + this.index = index; + } + + String getRef() { + return index + " 0 R"; + } + + String getID() { + return type.substring(0, 1) + index; + } + + boolean isFont() { + return "Font".equals(type); + } + + void setStream(byte[] stream) { + this.stream = stream; + } + + Object getDef(String key) { + return dictionary.get(key); + } + + void addDef(String key, Object value) { + if (dictionary == null) + dictionary = new Hashtable(); + dictionary.put(key, value); + if (key.equals("Type")) + type = ((String) value).substring(1); + } + + void setAsStream() { + stream = toBytes(0, -1); + setLength(0); + } + + int output(OutputStream os) throws IOException { + if (index > 0) { + String s = index + " 0 obj\n"; + write(os, s.getBytes(), 0); + } + int streamLen = 0; + if (dictionary != null) { + if (dictionary.containsKey("Length")) { + if (stream == null) + setAsStream(); + streamLen = stream.length; + boolean doDeflate = (streamLen > 1000); + if (doDeflate) { + Deflater deflater = new Deflater(9); + ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024); + DeflaterOutputStream compBytes = new DeflaterOutputStream(outBytes, + deflater); + compBytes.write(stream, 0, streamLen); + compBytes.finish(); + stream = outBytes.toByteArray(); + dictionary.put("Filter", "/FlateDecode"); + streamLen = stream.length; + } + dictionary.put("Length", "" + streamLen); + } + write(os, getDictionaryText(dictionary, "\n").getBytes(), 0); + } + if (length() > 0) + write(os, this.toString().getBytes(), 0); + if (stream != null) { + write(os, "stream\r\n".getBytes(), 0); + write(os, stream, streamLen); + write(os, "\r\nendstream\r\n".getBytes(), 0); + } + if (index > 0) + write(os, "endobj\n".getBytes(), 0); + return len; + } + + private void write(OutputStream os, byte[] bytes, int nBytes) throws IOException { + if (nBytes == 0) + nBytes = bytes.length; + len += nBytes; + os.write(bytes, 0, nBytes); + } + + @SuppressWarnings("unchecked") + private String getDictionaryText(Map d, String nl) { + SB sb = new SB(); + sb.append("<<"); + if (d.containsKey("Type")) + sb.append("/Type").appendO(d.get("Type")); + for (Entry e : d.entrySet()) { + String s = e.getKey(); + if (s.equals("Type") || s.startsWith("!")) + continue; + sb.append("/" + s); + Object o = e.getValue(); + if (o instanceof Map) { + sb.append((getDictionaryText((Map) o, ""))); + continue; + } + s = (String) e.getValue(); + if (!s.startsWith("/")) + sb.append(" "); + sb.appendO(s); + } + return (sb.length() > 3 ? sb.append(">>").append(nl).toString() : ""); + } + + @SuppressWarnings("unchecked") + private Map createSubdict(Map d0, String dict) { + Map d = (Map) d0.get(dict); + if (d == null) + d0.put(dict, d = new Hashtable()); + return d; + } + + void addResource(String type, String key, String value) { + Map r = createSubdict(dictionary, "Resources"); + if (type != null) + r = createSubdict(r, type); + r.put(key, value); + } + +} diff --git a/src/javajs/img/BMPDecoder.java b/src/javajs/img/BMPDecoder.java new file mode 100644 index 0000000..4c94c9b --- /dev/null +++ b/src/javajs/img/BMPDecoder.java @@ -0,0 +1,211 @@ +/* $RCSfile$ + * $Author: nicove $ + * $Date: 2007-03-30 12:26:16 -0500 (Fri, 30 Mar 2007) $ + * $Revision: 7275 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2002-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.img; + +import java.io.BufferedInputStream; +import java.io.IOException; + +import javajs.util.Rdr; + +/** + * src: http://www.javaworld.com/article/2077542/learn-java/java-tip-43--how-to- + * read-8--and-24-bit-microsoft-windows-bitmaps-in-java-applications.html + * + * see also: http://en.wikipedia.org/wiki/BMP_file_format + * + * Modified by Bob Hanson hansonr@stolaf.edu + * + * @author Bob Hanson (hansonr@stolaf.edu) + * + */ +public class BMPDecoder { + + public BMPDecoder() { + // for reflection + } + + private BufferedInputStream bis; + + /** + * original comment: + * + * loadbitmap() method converted from Windows C code. Reads only uncompressed + * 24- and 8-bit images. Tested with images saved using Microsoft Paint in + * Windows 95. If the image is not a 24- or 8-bit image, the program refuses + * to even try. I guess one could include 4-bit images by masking the byte by + * first 1100 and then 0011. I am not really interested in such images. If a + * compressed image is attempted, the routine will probably fail by generating + * an IOException. Look for variable ncompression to be different from 0 to + * indicate compression is present. + * + * @param bytes + * @return [image byte array, width, height] + */ + public Object[] decodeWindowsBMP(byte[] bytes) { + try { + bis = Rdr.getBIS(bytes); + temp = new byte[4]; + // read BITMAPFILEHEADER + if (readByte() != 'B' || readByte() != 'M') + return null; + readInt(); // file size; ignored + readShort(); // reserved + readShort(); // reserved + readInt(); // ptr to pixel array; ignored + int imageWidth, imageHeight, bitsPerPixel, nColors = 0, imageSize = 0; + // read BITMAP header + int headerSize = readInt(); + switch (headerSize) { + case 12: + // BITMAPCOREHEADER + imageWidth = readShort(); + imageHeight = readShort(); + readShort(); // planes + bitsPerPixel = readShort(); + break; + case 40: + // BITMAPINFOHEADER + imageWidth = readInt(); + imageHeight = readInt(); + readShort(); // planes + bitsPerPixel = readShort(); + int ncompression = readInt(); + if (ncompression != 0) { + System.out.println("BMP Compression is :" + ncompression + + " -- aborting"); + return null; + } + imageSize = readInt(); + readInt(); // hres + readInt(); // vres + nColors = readInt(); + readInt(); // colors used + break; + default: + System.out.println("BMP Header unrecognized, length=" + headerSize + + " -- aborting"); + return null; + } + boolean isYReversed = (imageHeight < 0); + if (isYReversed) + imageHeight = -imageHeight; + int nPixels = imageHeight * imageWidth; + int bytesPerPixel = bitsPerPixel / 8; + nColors = (nColors > 0 ? nColors : 1 << bitsPerPixel); + int npad = (bytesPerPixel == 4 ? 0 + : imageSize == 0 ? 4 - (imageWidth % 4) : (imageSize / imageHeight) + - imageWidth * bytesPerPixel) % 4; + int[] palette; + int[] buf = new int[nPixels]; + int dpt = (isYReversed ? imageWidth : -imageWidth); + int pt0 = (isYReversed ? 0 : nPixels + dpt); + int pt1 = (isYReversed ? nPixels : dpt); + switch (bitsPerPixel) { + case 32: + case 24: + for (int pt = pt0; pt != pt1; pt += dpt, pad(npad)) + for (int i = 0; i < imageWidth; i++) + buf[pt + i] = readColor(bytesPerPixel); + break; + case 8: + palette = new int[nColors]; + for (int i = 0; i < nColors; i++) + palette[i] = readColor(4); + for (int pt = pt0; pt != pt1; pt += dpt, pad(npad)) + for (int i = 0; i < imageWidth; i++) + buf[pt + i] = palette[readByte()]; + break; + case 4: + npad = (4 - (((imageWidth + 1) / 2) % 4)) % 4; + palette = new int[nColors]; + for (int i = 0; i < nColors; i++) + palette[i] = readColor(4); + int b4 = 0; + for (int pt = pt0; pt != pt1; pt += dpt, pad(npad)) + for (int i = 0, shift = 4; i < imageWidth; i++, shift = 4 - shift) + buf[pt + i] = palette[((shift == 4 ? (b4 = readByte()) : b4) >> shift) & 0xF]; + break; + case 1: + int color1 = readColor(3); + int color2 = readColor(3); + npad = (4 - (((imageWidth + 7) / 8) % 4)) % 4; + int b = 0; + for (int pt = pt0; pt != pt1; pt += dpt, pad(npad)) + for (int i = 0, bpt = -1; i < imageWidth; i++, bpt--) { + if (bpt < 0) { + b = readByte(); + bpt = 7; + } + buf[pt + i] = ((b & (1 << bpt)) == 0 ? color1 : color2); + } + break; + case 64: + case 2: + default: + System.out + .println("Not a 32-, 24-, 8-, 4-, or 1-bit Windows Bitmap, aborting..."); + return null; + } + return new Object[] { buf, Integer.valueOf(imageWidth), + Integer.valueOf(imageHeight) }; + } catch (Exception e) { + System.out.println("Caught exception in loadbitmap!"); + } + return null; + } + + private boolean pad(int npad) throws IOException { + for (int i = 0; i < npad; i++) + readByte(); + return true; + } + + private byte[] temp; + + private int readColor(int n) throws IOException { + bis.read(temp, 0, n); + return 0xff << 24 | ((temp[2] & 0xff) << 16) + | ((temp[1] & 0xff) << 8) | temp[0] & 0xff; + } + + private int readInt() throws IOException { + bis.read(temp, 0, 4); + return ((temp[3] & 0xff) << 24) | ((temp[2] & 0xff) << 16) + | ((temp[1] & 0xff) << 8) | temp[0] & 0xff; + } + + private int readShort() throws IOException { + bis.read(temp, 0, 2); + return ((temp[1] & 0xff) << 8) | temp[0] & 0xff; + } + + private int readByte() throws IOException { + bis.read(temp, 0, 1); + return temp[0] & 0xff; + } + +} diff --git a/src/javajs/img/CRCEncoder.java b/src/javajs/img/CRCEncoder.java new file mode 100644 index 0000000..77ed11e --- /dev/null +++ b/src/javajs/img/CRCEncoder.java @@ -0,0 +1,110 @@ +package javajs.img; + +import java.util.zip.CRC32; + + +import javajs.util.AU; + +abstract class CRCEncoder extends ImageEncoder { + + protected int startPos, bytePos; + + private CRC32 crc; + protected byte[] pngBytes; + protected int dataLen; + private byte[] int2 = new byte[2]; + private byte[] int4 = new byte[4]; + + CRCEncoder() { + pngBytes = new byte[250]; + crc = new CRC32(); + } + + protected void setData(byte[] b, int pt) { + pngBytes = b; + dataLen = b.length; + startPos = bytePos = pt; + } + + protected byte[] getBytes() { + return (dataLen == pngBytes.length ? pngBytes : AU.arrayCopyByte( + pngBytes, dataLen)); + } + + protected void writeCRC() { + crc.reset(); + crc.update(pngBytes, startPos, bytePos - startPos); + writeInt4((int) crc.getValue()); + } + + /** + * Write a two-byte integer into the pngBytes array at a given position. + * + * @param n The integer to be written into pngBytes. + */ + protected void writeInt2(int n) { + int2[0] = (byte) ((n >> 8) & 0xff); + int2[1] = (byte) (n & 0xff); + writeBytes(int2); + } + + /** + * Write a four-byte integer into the pngBytes array at a given position. + * + * @param n The integer to be written into pngBytes. + */ + protected void writeInt4(int n) { + getInt4(n, int4); + writeBytes(int4); + } + + protected static void getInt4(int n, byte[] int4) { + int4[0] = (byte) ((n >> 24) & 0xff); + int4[1] = (byte) ((n >> 16) & 0xff); + int4[2] = (byte) ((n >> 8) & 0xff); + int4[3] = (byte) (n & 0xff); + } + + /** + * Write a single byte into the pngBytes array at a given position. + * + * @param b The byte to be written into pngBytes. + */ + protected void writeByte(int b) { + byte[] temp = { + (byte) b + }; + writeBytes(temp); + } + + /** + * Write a string into the pngBytes array at a given position. + * This uses the getBytes method, so the encoding used will + * be its default. + * + * @param s The string to be written into pngBytes. + * @see java.lang.String#getBytes() + */ + protected void writeString(String s) { + writeBytes(s.getBytes()); + } + + /** + * Write an array of bytes into the pngBytes array. + * Both dataLen and bytePos are updated. If we don't have + * enough room, this is certainly in image data writing, + * so we add just enough for CRC END CRC + * + * @param data + * The data to be written into pngBytes. + */ + protected void writeBytes(byte[] data) { + int newPos = bytePos + data.length; + dataLen = Math.max(dataLen, newPos); + if (newPos > pngBytes.length) + pngBytes = AU.arrayCopyByte(pngBytes, newPos + 16); + System.arraycopy(data, 0, pngBytes, bytePos, data.length); + bytePos = newPos; + } + +} diff --git a/src/javajs/img/GifEncoder.java b/src/javajs/img/GifEncoder.java new file mode 100644 index 0000000..287f9c3 --- /dev/null +++ b/src/javajs/img/GifEncoder.java @@ -0,0 +1,1142 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2007-06-02 12:14:13 -0500 (Sat, 02 Jun 2007) $ + * $Revision: 7831 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2000-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +// Final encoding code from http://acme.com/resources/classes/Acme/JPM/Encoders/GifEncoder.java +// +// GifEncoder - write out an image as a GIF +// +// +// Transparency handling and variable bit size courtesy of Jack Palevich. +// +// Copyright (C)1996,1998 by Jef Poskanzer . All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +// SUCH DAMAGE. +// +// Visit the ACME Labs Java page for up-to-date versions of this and other +// fine Java utilities: http://www.acme.com/java/ +// +/// Write out an image as a GIF. +//

+// Fetch the software.
+// Fetch the entire Acme package. +//

+// @see ToGif + +package javajs.img; + +import javajs.util.CU; +import javajs.util.Lst; +import javajs.util.M3; +import javajs.util.P3; + +import java.util.Hashtable; +import java.util.Map; +import java.io.IOException; + +/** + * + * GifEncoder extensively adapted for Jmol by Bob Hanson + * + * Color quantization roughly follows the GIMP method + * "dither Floyd-Steinberg (normal)" but with some twists. (For example, we + * exclude the background color.) + * + * Note that although GIMP code annotation refers to "median-cut", it is really + * using MEAN-cut. That is what I use here as well. + * + * -- commented code allows visualization of the color space using Jmol. Very + * enlightening! + * + * -- much simplified interface with ImageEncoder + * + * -- uses simple Hashtable with Integer() to catalog colors + * + * -- allows progressive production of animated GIF via Jmol CAPTURE command + * + * -- uses general purpose javajs.util.OutputChannel for byte-handling options + * such as posting to a server, writing to disk, and retrieving bytes. + * + * -- allows JavaScript port + * + * -- Bob Hanson, first try: 24 Sep 2013; final coding: 9 Nov 2014 + * + * + * @author Bob Hanson hansonr@stolaf.edu + */ + +public class GifEncoder extends ImageEncoder { + + private Map params; + private P3[] palette; + private int backgroundColor; + + private boolean interlaced; + private boolean addHeader = true; + private boolean addImage = true; + private boolean addTrailer = true; + private boolean isTransparent; + private boolean floydSteinberg = true; + private boolean capturing; + private boolean looping; + + private int delayTime100ths = -1; + private int bitsPerPixel = 1; + + private int byteCount; + + /** + * we allow for animated GIF by being able to re-enter the code with different + * parameters held in params + * + * + */ + @Override + protected void setParams(Map params) { + this.params = params; + Integer ic = (Integer) params.get("transparentColor"); + if (ic == null) { + ic = (Integer) params.get("backgroundColor"); + if (ic != null) + backgroundColor = ic.intValue(); + } else { + backgroundColor = ic.intValue(); + isTransparent = true; + } + + interlaced = (Boolean.TRUE == params.get("interlaced")); + if (params.containsKey("captureRootExt") // file0000.gif + || !params.containsKey("captureMode")) // animated gif + return; + interlaced = false; + capturing = true; + try { + byteCount = ((Integer) params.get("captureByteCount")).intValue(); + } catch (Exception e) { + // ignore + } + switch ("maec" + .indexOf(((String) params.get("captureMode")).substring(0, 1))) { + case 0: //"movie" + params.put("captureMode", "add"); + addImage = false; + addTrailer = false; + break; + case 1: // add + addHeader = false; + addTrailer = false; + int fps = Math.abs(((Integer) params.get("captureFps")).intValue()); + delayTime100ths = (fps == 0 ? 0 : 100 / fps); + looping = (Boolean.FALSE != params.get("captureLooping")); + break; + case 2: // end + addHeader = false; + addImage = false; + break; + case 3: // cancel + addHeader = false; + addImage = false; + out.cancel(); + break; + } + } + + @Override + protected void generate() throws IOException { + if (addHeader) + writeHeader(); + addHeader = false; // only one header + if (addImage) { + createPalette(); + writeGraphicControlExtension(); + if (delayTime100ths >= 0 && looping) + writeNetscapeLoopExtension(); + writeImage(); + } + } + + @Override + protected void close() { + if (addTrailer) { + writeTrailer(); + } else { + doClose = false; + } + if (capturing) + params.put("captureByteCount", Integer.valueOf(byteCount)); + } + + ////////////// 256-color quantization ////////////// + + /** + * a color point in normalized L*a*b space with a flag indicating whether it + * is the background color + */ + private class ColorItem extends P3 { + /** + * + */ + protected boolean isBackground; + + ColorItem(int rgb, boolean isBackground) { + this.isBackground = isBackground; + setT(toLABnorm(rgb)); + } + } + + /** + * A list of normalized L*a*b points with an index and a center and volume + * + */ + private class ColorCell extends Lst { + + /** + * + */ + private static final long serialVersionUID = 1L; + protected int index; + protected P3 center; + + private float volume; + + ColorCell(int index) { + this.index = index; + } + + /** + * @param doVisualize + * debugging only + * @return volume in normalized L*a*b space + */ + public float getVolume(boolean doVisualize) { + if (volume != 0) + return volume; + if (size() < 2) + return -1; + //if (true) + //return lst.size(); + //float d; + float maxx = -Integer.MAX_VALUE; + float minx = Integer.MAX_VALUE; + float maxy = -Integer.MAX_VALUE; + float miny = Integer.MAX_VALUE; + float maxz = -Integer.MAX_VALUE; + float minz = Integer.MAX_VALUE; + int n = size(); + for (int i = n; --i >= 0;) { + P3 xyz = get(i); + if (xyz.x < minx) + minx = xyz.x; + if (xyz.y < miny) + miny = xyz.y; + if (xyz.z < minz) + minz = xyz.z; + if (xyz.x > maxx) + maxx = xyz.x; + if (xyz.y > maxy) + maxy = xyz.y; + if (xyz.z > maxz) + maxz = xyz.z; + } + float dx = (maxx - minx); + float dy = (maxy - miny); + float dz = (maxz - minz); + // Jmol visualization only + // if (doVisualize) { + // P3 ptRGB = toRGB(center); + // drawPt(index, -size(), ptRGB); + // //for (int i = n; --i >= 0;) + // //drawPt(index, i, toRGB(get(i))); + // P3 pt0 = toRGB(P3.new3(Math.max(minx, 0), Math.max(miny, 0), + // Math.max(minz, 0))); + // P3 pt1 = toRGB(P3.new3(Math.min(maxx, 100), Math.min(maxy, 100), + // Math.min(maxz, 100))); + // rgbToXyz(pt0, pt0); + // xyzToLab(pt0, pt0); + // rgbToXyz(pt1, pt1); + // xyzToLab(pt1, pt1); + // System.out.println("boundbox corners " + pt0 + " " + pt1); + // System.out.println("draw d" + index + " boundbox color " + ptRGB + // + " mesh nofill"); + // } + return volume = dx * dx + dy * dy + dz * dz; + } + + // // Jmol visualization only + // private void drawPt(int index, int i, P3 rgb) { + // boolean isMain = (i < 0); + // P3 lab = rgbToXyz(rgb, null); + // xyzToLab(lab, lab); + // System.out.println("draw d" + index + (isMain ? "_" : "_" + i) + " width " + // + (isMain ? 1.0 : 0.2) + " " + lab + // + " color " + rgb + (isMain ? " '" + -i + "'" : "")); + // } + + /** + * Set the average normalized L*a*b value for this cell and return its RGB point + * + * @return RGB point + * + */ + protected P3 setColor() { + int count = size(); + center = new P3(); + for (int i = count; --i >= 0;) + center.add(get(i)); + center.scale(1f / count); + // Jmol visualization only + //volume = 0; + //getVolume(true); + return toRGB(center); + } + + /** + * use median_cut algorithm to split the cell, creating a doubly linked + * list. + * + * Paul Heckbert, MIT thesis COLOR IMAGE QUANTIZATION FOR FRAME BUFFER + * DISPLAY https://www.cs.cmu.edu/~ph/ciq_thesis + * + * except, as in GIMP, we use center (not median) here. + * + * @param cells + * @return true if split + */ + protected boolean splitCell(Lst cells) { + int n = size(); + if (n < 2) + return false; + int newIndex = cells.size(); + ColorCell newCell = new ColorCell(newIndex); + cells.addLast(newCell); + float[][] ranges = new float[3][3]; + for (int ic = 0; ic < 3; ic++) { + float low = Float.MAX_VALUE; + float high = -Float.MAX_VALUE; + for (int i = n; --i >= 0;) { + P3 lab = get(i); + float v = (ic == 0 ? lab.x : ic == 1 ? lab.y : lab.z); + if (low > v) + low = v; + if (high < v) + high = v; + } + ranges[0][ic] = low; + ranges[1][ic] = high; + ranges[2][ic] = high - low; + } + float[] r = ranges[2]; + int mode = (r[0] >= r[1] ? (r[0] >= r[2] ? 0 : 2) : r[1] >= r[2] ? 1 : 2); + float val = ranges[0][mode] + ranges[2][mode] / 2; + volume = 0; // recalculate volume if needed + switch (mode) { + case 0: + for (int i = n; --i >= 0;) + if (get(i).x >= val) + newCell.addLast(removeItemAt(i)); + break; + case 1: + for (int i = n; --i >= 0;) + if (get(i).y >= val) + newCell.addLast(removeItemAt(i)); + break; + case 2: + for (int i = size(); --i >= 0;) + if (get(i).z >= val) + newCell.addLast(removeItemAt(i)); + break; + } + return true; + } + } + + /** + * Quantize all colors and create the final palette; + * replace pixels[] with an array of color indices. + * + */ + private void createPalette() { + + // catalog all pixel colors + + Lst tempColors = new Lst(); + Map ciHash = new Hashtable(); + for (int i = 0, n = pixels.length; i < n; i++) { + int rgb = pixels[i]; + Integer key = Integer.valueOf(rgb); + ColorItem item = ciHash.get(key); + if (item == null) { + item = new ColorItem(rgb, rgb == backgroundColor); + ciHash.put(key, item); + tempColors.addLast(item); + } + } + int nColors = tempColors.size(); + System.out.println("GIF total image colors: " + nColors); + ciHash = null; + + // create a set of <= 256 color cells + + Lst cells = quantizeColors(tempColors); + nColors = cells.size(); + System.out.println("GIF final color count: " + nColors); + + // generate the palette and map each cell's rgb color to itself + + Map colorMap = new Hashtable(); + bitsPerPixel = (nColors <= 2 ? 1 : nColors <= 4 ? 2 : nColors <= 16 ? 4 : 8); + palette = new P3[1 << bitsPerPixel]; + for (int i = 0; i < nColors; i++) { + ColorCell c = cells.get(i); + colorMap.put( + Integer.valueOf(CU.colorPtToFFRGB(palette[i] = c.setColor())), c); + } + + // index all pixels to a pallete color + + pixels = indexPixels(cells, colorMap); + } + + /** + * Quantize colors by generating a set of cells in normalized L*a*b space + * containing all colors. Start with just two cells -- fixed background color + * and all others. Keep splitting cells while there are fewer than 256 and + * some with multiple colors in them. + * + * It is possible that we will end up with fewer than 256 colors. + * + * @param tempColors + * @return final list of colors + */ + private Lst quantizeColors(Lst tempColors) { + int n = tempColors.size(); + Lst cells = new Lst(); + ColorCell cc = new ColorCell(0); + cc.addLast(new ColorItem(backgroundColor, true)); + cells.addLast(cc); + cc = new ColorCell(1); + if (n > 256) + cells.addLast(cc); + for (int i = 0; i < n; i++) { + ColorItem c = tempColors.get(i); + if (c.isBackground) + continue; + cc.addLast(c); + if (n <= 256) { + cells.addLast(cc); + cc = new ColorCell(cells.size()); + } + } + tempColors.clear(); + if (n > 256) + while ((n = cells.size()) < 256) { + float maxVol = 0; + ColorCell maxCell = null; + for (int i = n; --i >= 1;) { + ColorCell c = cells.get(i); + float v = c.getVolume(false); + if (v > maxVol) { + maxVol = v; + maxCell = c; + } + } + if (maxCell == null || !maxCell.splitCell(cells)) + break; + } + return cells; + } + + /** + * + * Assign all colors to their closest approximation and return an array of + * color indexes. + * + * Uses Floyd-Steinberg dithering, finding the closest known color and then + * spreading out the error over four leading pixels. Limits error to +/- 75 + * percent in normalized L*a*b space. + * + * @param cells + * quantized color cells + * @param colorMap + * map of quantized rgb to its cell + * @return array of color indexes, one for each pixel + * + */ + private int[] indexPixels(Lst cells, + Map colorMap) { + // We need a strip only width+2 wide to process all the errors. + // Errors are added to the next pixel and the next row's pixels + // only through p + width + 1: + // p +1 + // +w-1 +w +w+1 + // so including p as well, we need a total of width + 2 errors. + // + // as p moves through the pixels, we just use mod to cycle through + // this strip. + // + int w2 = width + 2; + P3[] errors = new P3[w2]; + // We should replace, not overwrite, pixels + // as this may be the raw canvas.buf32. + int[] newPixels = new int[pixels.length]; + P3 err = new P3(); + P3 lab; + int rgb; + Map nearestCell = new Hashtable(); + for (int i = 0, p = 0; i < height; ++i) { + boolean notLastRow = (i != height - 1); + for (int j = 0; j < width; ++j, p++) { + if (pixels[p] == backgroundColor) { + // leave as 0 + continue; + } + P3 pe = errors[p % w2]; + if (pe == null || pe.x == Float.MAX_VALUE) { + lab = null; + rgb = pixels[p]; + } else { + lab = toLABnorm(pixels[p]); + err = pe; + // important not to round the clamp here -- full floating precision + err.x = clamp(err.x, -75, 75); + err.y = clamp(err.y, -75, 75); + err.z = clamp(err.z, -75, 75); + lab.add(err); + rgb = CU.colorPtToFFRGB(toRGB(lab)); + } + Integer key = Integer.valueOf(rgb); + ColorCell cell = colorMap.get(key); + if (cell == null) { + // critical to generate normalized L*a*b from RGB here for nearestCell mapping. + // otherwise future RGB keys may match the wrong cell + lab = toLABnorm(rgb); + cell = nearestCell.get(key); + if (cell == null) { + // find nearest cell + float maxerr = Float.MAX_VALUE; + // skip 0 0 0 + for (int ib = cells.size(); --ib >= 1;) { + ColorCell c = cells.get(ib); + err.sub2(lab, c.center); + float d = err.lengthSquared(); + if (d < maxerr) { + maxerr = d; + cell = c; + } + } + nearestCell.put(key, cell); + } + if (floydSteinberg) { + // dither + err.sub2(lab, cell.center); + boolean notLastCol = (j < width - 1); + if (notLastCol) + addError(err, 7, errors, p + 1, w2); + if (notLastRow) { + if (j > 0) + addError(err, 3, errors, p + width - 1, w2); + addError(err, 5, errors, p + width, w2); + if (notLastCol) + addError(err, 1, errors, p + width + 1, w2); + } + } + err.x = Float.MAX_VALUE; // used; flag for resetting to 0 + } + newPixels[p] = cell.index; + } + } + return newPixels; + } + + private void addError(P3 err, int f, P3[] errors, int p, int w2) { + // GIMP will allow changing the background color. + if (pixels[p] == backgroundColor) + return; + p %= w2; + P3 errp = errors[p]; + if (errp == null) + errp = errors[p] = new P3(); + else if (errp.x == Float.MAX_VALUE) // reuse + errp.set(0, 0, 0); + errp.scaleAdd2(f / 16f, err, errp); + } + + ///////////////////////// CIE L*a*b / XYZ / sRGB conversion methods ///////// + + // these could be static, but that just makes for more JavaScript code + + protected P3 toLABnorm(int rgb) { + P3 lab = CU.colorPtFromInt(rgb, null); + rgbToXyz(lab, lab); + xyzToLab(lab, lab); + // normalize to 0-100 + lab.y = (lab.y + 86.185f) / (98.254f + 86.185f) * 100f; + lab.z = (lab.z + 107.863f) / (94.482f + 107.863f) * 100f; + return lab; + } + + protected P3 toRGB(P3 lab) { + P3 xyz = P3.newP(lab); + // normalized to 0-100 + xyz.y = xyz.y / 100f * (98.254f + 86.185f) - 86.185f; + xyz.z = xyz.z / 100f * (94.482f + 107.863f) - 107.863f; + labToXyz(xyz, xyz); + return xyzToRgb(xyz, xyz); + } + + private static M3 xyz2rgb; + private static M3 rgb2xyz; + + static { + rgb2xyz = M3.newA9(new float[] { 0.4124f, 0.3576f, 0.1805f, 0.2126f, + 0.7152f, 0.0722f, 0.0193f, 0.1192f, 0.9505f }); + + xyz2rgb = M3.newA9(new float[] { 3.2406f, -1.5372f, -0.4986f, -0.9689f, + 1.8758f, 0.0415f, 0.0557f, -0.2040f, 1.0570f }); + } + + public P3 rgbToXyz(P3 rgb, P3 xyz) { + // http://en.wikipedia.org/wiki/CIE_1931_color_space + // http://rsb.info.nih.gov/ij/plugins/download/Color_Space_Converter.java + if (xyz == null) + xyz = new P3(); + xyz.x = sxyz(rgb.x); + xyz.y = sxyz(rgb.y); + xyz.z = sxyz(rgb.z); + rgb2xyz.rotate(xyz); + return xyz; + } + + private float sxyz(float x) { + x /= 255; + return (float) (x <= 0.04045 ? x / 12.92 : Math.pow(((x + 0.055) / 1.055), + 2.4)) * 100; + } + + public P3 xyzToRgb(P3 xyz, P3 rgb) { + // http://en.wikipedia.org/wiki/CIE_1931_color_space + // http://rsb.info.nih.gov/ij/plugins/download/Color_Space_Converter.java + if (rgb == null) + rgb = new P3(); + rgb.setT(xyz); + rgb.scale(0.01f); + xyz2rgb.rotate(rgb); + rgb.x = clamp(srgb(rgb.x), 0, 255); + rgb.y = clamp(srgb(rgb.y), 0, 255); + rgb.z = clamp(srgb(rgb.z), 0, 255); + return rgb; + } + + private float srgb(float x) { + return (float) (x > 0.0031308f ? (1.055 * Math.pow(x, 1.0 / 2.4)) - 0.055 + : x * 12.92) * 255; + } + + public P3 xyzToLab(P3 xyz, P3 lab) { + // http://en.wikipedia.org/wiki/Lab_color_space + // http://rsb.info.nih.gov/ij/plugins/download/Color_Space_Converter.java + // Lab([0..100], [-86.185..98.254], [-107.863..94.482]) + // XYZn = D65 = {95.0429, 100.0, 108.8900}; + if (lab == null) + lab = new P3(); + float x = flab(xyz.x / 95.0429f); + float y = flab(xyz.y / 100); + float z = flab(xyz.z / 108.89f); + lab.x = (116 * y) - 16; + lab.y = 500 * (x - y); + lab.z = 200 * (y - z); + return lab; + } + + private float flab(float t) { + return (float) (t > 8.85645168E-3 /* (24/116)^3 */? Math.pow(t, + 0.333333333) : 7.78703704 /* 1/3*116/24*116/24 */* t + 0.137931034 /* 16/116 */ + ); + } + + public P3 labToXyz(P3 lab, P3 xyz) { + // http://en.wikipedia.org/wiki/Lab_color_space + // http://rsb.info.nih.gov/ij/plugins/download/Color_Space_Converter.java + // XYZn = D65 = {95.0429, 100.0, 108.8900}; + if (xyz == null) + xyz = new P3(); + + xyz.setT(lab); + float y = (xyz.x + 16) / 116; + float x = xyz.y / 500 + y; + float z = y - xyz.z / 200; + xyz.x = fxyz(x) * 95.0429f; + xyz.y = fxyz(y) * 100; + xyz.z = fxyz(z) * 108.89f; + + return xyz; + } + + private float fxyz(float t) { + return (float) (t > 0.206896552 /* (24/116) */? t * t * t + : 0.128418549 /* 3*24/116*24/116 */* (t - 0.137931034 /* 16/116 */)); + } + + private float clamp(float c, float min, float max) { + c = (c < min ? min : c > max ? max : c); + return (min == 0 ? Math.round(c) : c); + } + + ///////////////////////// GifEncoder writing methods //////////////////////// + + /** + * includes logical screen descriptor + * + * @throws IOException + */ + private void writeHeader() throws IOException { + putString("GIF89a"); + putWord(width); + putWord(height); + putByte(0); // no global color table -- using local instead + putByte(0); // no background + putByte(0); // no pixel aspect ratio given + } + + private void writeGraphicControlExtension() { + if (isTransparent || delayTime100ths >= 0) { + putByte(0x21); // graphic control extension + putByte(0xf9); // graphic control label + putByte(4); // block size + putByte((isTransparent ? 9 : 0) | (delayTime100ths > 0 ? 2 : 0)); // packed bytes + putWord(delayTime100ths > 0 ? delayTime100ths : 0); + putByte(0); // transparent index + putByte(0); // end-of-block + } + } + + // see http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension + // +---------------+ + // 0 | 0x21 | Extension Label + // +---------------+ + // 1 | 0xFF | Application Extension Label + // +---------------+ + // 2 | 0x0B | Block Size + // +---------------+ + // 3 | | + // +- -+ + // 4 | | + // +- -+ + // 5 | | + // +- -+ + // 6 | | + // +- NETSCAPE -+ Application Identifier (8 bytes) + // 7 | | + // +- -+ + // 8 | | + // +- -+ + // 9 | | + // +- -+ + // 10 | | + // +---------------+ + // 11 | | + // +- -+ + // 12 | 2.0 | Application Authentication Code (3 bytes) + // +- -+ + // 13 | | + // +===============+ --+ + // 14 | 0x03 | Sub-block Data Size | + // +---------------+ | + // 15 | 0x01 | Sub-block ID | + // +---------------+ | Application Data Sub-block + // 16 | | | + // +- -+ Loop Count (2 bytes) | + // 17 | | | + // +===============+ --+ + // 18 | 0x00 | Block Terminator + // +---------------+ + + private void writeNetscapeLoopExtension() { + putByte(0x21); // graphic control extension + putByte(0xff); // netscape loop extension + putByte(0x0B); // block size + putString("NETSCAPE2.0"); + putByte(3); + putByte(1); + putWord(0); // loop indefinitely + putByte(0); // end-of-block + + } + + private int initCodeSize; + private int curpt; + + private void writeImage() { + putByte(0x2C); + putWord(0); //left + putWord(0); //top + putWord(width); + putWord(height); + + // = LISx xZZZ + + // L Local Color Table Flag + // I Interlace Flag + // S Sort Flag + // x Reserved + // ZZZ Size of Local Color Table + + int packedFields = 0x80 | (interlaced ? 0x40 : 0) | (bitsPerPixel - 1); + putByte(packedFields); + int colorMapSize = 1 << bitsPerPixel; + P3 p = new P3(); + for (int i = 0; i < colorMapSize; i++) { + if (palette[i] != null) + p = palette[i]; + putByte((int) p.x); + putByte((int) p.y); + putByte((int) p.z); + } + putByte(initCodeSize = (bitsPerPixel <= 1 ? 2 : bitsPerPixel)); + compress(); + putByte(0); + } + + private void writeTrailer() { + // Write the GIF file terminator + putByte(0x3B); + } + + ///// compression routines ///// + + private static final int EOF = -1; + + // Return the next pixel from the image + private int nextPixel() { + if (countDown-- == 0) + return EOF; + int colorIndex = pixels[curpt]; + // Bump the current X position + ++curx; + if (curx == width) { + // If we are at the end of a scan line, set curx back to the beginning + // If we are interlaced, bump the cury to the appropriate spot, + // otherwise, just increment it. + curx = 0; + if (interlaced) + updateY(INTERLACE_PARAMS[pass], INTERLACE_PARAMS[pass + 4]); + else + ++cury; + } + curpt = cury * width + curx; + return colorIndex & 0xff; + } + + private static final int[] INTERLACE_PARAMS = { 8, 8, 4, 2, 4, 2, 1, 0 }; + + /** + * + * Group 1 : Every 8th. row, starting with row 0. (Pass 1) + * + * Group 2 : Every 8th. row, starting with row 4. (Pass 2) + * + * Group 3 : Every 4th. row, starting with row 2. (Pass 3) + * + * Group 4 : Every 2nd. row, starting with row 1. (Pass 4) + * + * @param yNext + * @param yNew + */ + private void updateY(int yNext, int yNew) { + cury += yNext; + if (yNew >= 0 && cury >= height) { + cury = yNew; + ++pass; + } + } + + // Write out a word to the GIF file + private void putWord(int w) { + putByte(w); + putByte(w >> 8); + } + + // GIFCOMPR.C - GIF Image compression routines + // + // Lempel-Ziv compression based on 'compress'. GIF modifications by + // David Rowley (mgardi@watdcsu.waterloo.edu) + + // General DEFINEs + + private static final int BITS = 12; + + private static final int HSIZE = 5003; // 80% occupancy + + // GIF Image compression - modified 'compress' + // + // Based on: compress.c - File compression ala IEEE Computer, June 1984. + // + // By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) + // Jim McKie (decvax!mcvax!jim) + // Steve Davies (decvax!vax135!petsd!peora!srd) + // Ken Turkowski (decvax!decwrl!turtlevax!ken) + // James A. Woods (decvax!ihnp4!ames!jaw) + // Joe Orost (decvax!vax135!petsd!joe) + + private int nBits; // number of bits/code + private int maxbits = BITS; // user settable max # bits/code + private int maxcode; // maximum code, given n_bits + private int maxmaxcode = 1 << BITS; // should NEVER generate this code + + private final static int MAXCODE(int nBits) { + return (1 << nBits) - 1; + } + + private int[] htab = new int[HSIZE]; + private int[] codetab = new int[HSIZE]; + + private int hsize = HSIZE; // for dynamic table sizing + + private int freeEnt = 0; // first unused entry + + // block compression parameters -- after all codes are used up, + // and compression rate changes, start over. + private boolean clearFlag = false; + + // Algorithm: use open addressing double hashing (no chaining) on the + // prefix code / next character combination. We do a variant of Knuth's + // algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime + // secondary probe. Here, the modular division first probe is gives way + // to a faster exclusive-or manipulation. Also do block compression with + // an adaptive reset, whereby the code table is cleared when the compression + // ratio decreases, but after the table fills. The variable-length output + // codes are re-sized at this point, and a special CLEAR code is generated + // for the decompressor. Late addition: construct the table according to + // file size for noticeable speed improvement on small files. Please direct + // questions about this implementation to ames!jaw. + + private int clearCode; + private int EOFCode; + + private int countDown; + private int pass = 0; + private int curx, cury; + + private void compress() { + + // Calculate number of bits we are expecting + countDown = width * height; + + // Indicate which pass we are on (if interlace) + pass = 0; + // Set up the current x and y position + curx = 0; + cury = 0; + + // Set up the necessary values + clearFlag = false; + nBits = initCodeSize + 1; + maxcode = MAXCODE(nBits); + + clearCode = 1 << initCodeSize; + EOFCode = clearCode + 1; + freeEnt = clearCode + 2; + + // Set up the 'byte output' routine + bufPt = 0; + + int ent = nextPixel(); + + int hshift = 0; + int fcode; + for (fcode = hsize; fcode < 65536; fcode *= 2) + ++hshift; + hshift = 8 - hshift; // set hash code range bound + + int hsizeReg = hsize; + clearHash(hsizeReg); // clear hash table + + output(clearCode); + + int c; + outer_loop: while ((c = nextPixel()) != EOF) { + fcode = (c << maxbits) + ent; + int i = (c << hshift) ^ ent; // xor hashing + + if (htab[i] == fcode) { + ent = codetab[i]; + continue; + } else if (htab[i] >= 0) // non-empty slot + { + int disp = hsizeReg - i; // secondary hash (after G. Knott) + if (i == 0) + disp = 1; + do { + if ((i -= disp) < 0) + i += hsizeReg; + + if (htab[i] == fcode) { + ent = codetab[i]; + continue outer_loop; + } + } while (htab[i] >= 0); + } + output(ent); + ent = c; + if (freeEnt < maxmaxcode) { + codetab[i] = freeEnt++; // code -> hashtable + htab[i] = fcode; + } else { + clearBlock(); + } + } + // Put out the final code. + output(ent); + output(EOFCode); + } + + // output + // + // Output the given code. + // Inputs: + // code: A n_bits-bit integer. If == -1, then EOF. This assumes + // that n_bits =< wordsize - 1. + // Outputs: + // Outputs code to the file. + // Assumptions: + // Chars are 8 bits long. + // Algorithm: + // Maintain a BITS character long buffer (so that 8 codes will + // fit in it exactly). Use the VAX insv instruction to insert each + // code in turn. When the buffer fills up empty it and start over. + + private int curAccum = 0; + private int curBits = 0; + + private int masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, + 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, + 0x7FFF, 0xFFFF }; + + private void output(int code) { + curAccum &= masks[curBits]; + + if (curBits > 0) + curAccum |= (code << curBits); + else + curAccum = code; + + curBits += nBits; + + while (curBits >= 8) { + byteOut((byte) (curAccum & 0xff)); + curAccum >>= 8; + curBits -= 8; + } + + // If the next entry is going to be too big for the code size, + // then increase it, if possible. + if (freeEnt > maxcode || clearFlag) { + if (clearFlag) { + maxcode = MAXCODE(nBits = initCodeSize + 1); + clearFlag = false; + } else { + ++nBits; + if (nBits == maxbits) + maxcode = maxmaxcode; + else + maxcode = MAXCODE(nBits); + } + } + + if (code == EOFCode) { + // At EOF, write the rest of the buffer. + while (curBits > 0) { + byteOut((byte) (curAccum & 0xff)); + curAccum >>= 8; + curBits -= 8; + } + flushBytes(); + } + } + + // Clear out the hash table + + // table clear for block compress + private void clearBlock() { + clearHash(hsize); + freeEnt = clearCode + 2; + clearFlag = true; + + output(clearCode); + } + + // reset code table + private void clearHash(int hsize) { + for (int i = 0; i < hsize; ++i) + htab[i] = -1; + } + + // GIF-specific routines (byte array buffer) + + // Number of bytes so far in this 'packet' + private int bufPt; + + // Define the storage for the packet accumulator + final private byte[] buf = new byte[256]; + + // Add a byte to the end of the current packet, and if it is 254 + // byte, flush the packet to disk. + private void byteOut(byte c) { + buf[bufPt++] = c; + if (bufPt >= 254) + flushBytes(); + } + + // Flush the packet to disk, and reset the accumulator + protected void flushBytes() { + if (bufPt > 0) { + putByte(bufPt); + out.write(buf, 0, bufPt); + byteCount += bufPt; + bufPt = 0; + } + } + +} diff --git a/src/javajs/img/ImageEncoder.java b/src/javajs/img/ImageEncoder.java new file mode 100644 index 0000000..7781202 --- /dev/null +++ b/src/javajs/img/ImageEncoder.java @@ -0,0 +1,127 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2007-06-02 12:14:13 -0500 (Sat, 02 Jun 2007) $ + * $Revision: 7831 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2000-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +// ImageEncoder - abstract class for writing out an image +// +// Copyright (C) 1996 by Jef Poskanzer . All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +// SUCH DAMAGE. +// +// Visit the ACME Labs Java page for up-to-date versions of this and other +// fine Java utilities: http://www.acme.com/java/ + +package javajs.img; + +import java.util.Map; + +import javajs.api.GenericImageEncoder; +import javajs.util.OC; + + + +/** + * Generic abstract image creator: + * + * (1) set parameters + * + * (2) encode the image bytes, if necessary + * + * (3) generate the image + * @author Bob Hanson hansonr@stolaf.edu + */ + +public abstract class ImageEncoder implements GenericImageEncoder { + + protected OC out; + + protected int width = -1; + protected int height = -1; + protected int quality = -1; + protected String date; + protected boolean logging; + protected boolean doClose = true; + + /** + * @param type + * @param out + * @param params + */ + @Override + public boolean createImage(String type, OC out, Map params) + throws Exception { + this.out = out; + logging = (Boolean.TRUE == params.get("logging")); + width = ((Integer) params.get("imageWidth")).intValue(); + height = ((Integer) params.get("imageHeight")).intValue(); + pixels = (int[]) params.get("imagePixels"); + date = (String) params.get("date"); + Integer q = (Integer) params.get("quality"); + quality = (q == null ? -1 : q.intValue()); + setParams(params); + generate(); + close(); // GIF will override this and not close + return doClose; + } + + abstract protected void setParams(Map params); + abstract protected void generate() throws Exception; + + protected int[] pixels; + + protected void putString(String s) { + byte[] b = s.getBytes(); + out.write(b, 0, b.length); + } + + protected void putByte(int b) { + out.writeByteAsInt(b); + } + + protected void close() { + // your responsibility to close the output channel + } + +} diff --git a/src/javajs/img/Jpg64Encoder.java b/src/javajs/img/Jpg64Encoder.java new file mode 100644 index 0000000..796972f --- /dev/null +++ b/src/javajs/img/Jpg64Encoder.java @@ -0,0 +1,64 @@ +// Version 1.0a +// Copyright (C) 1998, James R. Weeks and BioElectroMech. +// Visit BioElectroMech at www.obrador.com. Email James@obrador.com. + +// See license.txt for details about the allowed used of this software. +// This software is based in part on the work of the Independent JPEG Group. +// See IJGreadme.txt for details about the Independent JPEG Group's license. + +// This encoder is inspired by the Java Jpeg encoder by Florian Raemy, +// studwww.eurecom.fr/~raemy. +// It borrows a great deal of code and structure from the Independent +// Jpeg Group's Jpeg 6a library, Copyright Thomas G. Lane. +// See license.txt for details + +/* + * JpegEncoder and its associated classes are Copyright (c) 1998, James R. Weeks and BioElectroMech + * see(Jmol/src/com/obrador/license.txt) + * + * javajs.img.JpegEncoder.java was adapted by Bob Hanson + * for Jmol in the following ways: + * + * 1) minor coding efficiencies were made in some for() loops. + * 2) methods not used by Jmol were commented out + * 3) method and variable signatures were modified to provide + * more appropriate method privacy. + * 4) additions for Java2Script compatibility + * + * Original files are maintained in the Jmol.src.com.obrador package, but + * these original files are not distributed with Jmol. + * +*/ + +package javajs.img; + +import java.io.IOException; +import java.util.Map; + +import javajs.util.Base64; +import javajs.util.OC; + + +public class Jpg64Encoder extends JpgEncoder { + + private OC outTemp; + + @Override + protected void setParams(Map params) { + defaultQuality = 75; + outTemp = (OC) params.remove("outputChannelTemp"); + super.setParams(params); + } + + @Override + protected void generate() throws IOException { + OC out0 = out; + out = outTemp; + super.generate(); + byte[] bytes = Base64.getBytes64(out.toByteArray()); + outTemp = null; + out = out0; + out.write(bytes, 0, bytes.length); + } + +} diff --git a/src/javajs/img/JpgEncoder.java b/src/javajs/img/JpgEncoder.java new file mode 100644 index 0000000..4e49718 --- /dev/null +++ b/src/javajs/img/JpgEncoder.java @@ -0,0 +1,1208 @@ +// Version 1.0a +// Copyright (C) 1998, James R. Weeks and BioElectroMech. +// Visit BioElectroMech at www.obrador.com. Email James@obrador.com. + +// See license.txt for details about the allowed used of this software. +// This software is based in part on the work of the Independent JPEG Group. +// See IJGreadme.txt for details about the Independent JPEG Group's license. + +// This encoder is inspired by the Java Jpeg encoder by Florian Raemy, +// studwww.eurecom.fr/~raemy. +// It borrows a great deal of code and structure from the Independent +// Jpeg Group's Jpeg 6a library, Copyright Thomas G. Lane. +// See license.txt for details + +/* + * JpegEncoder and its associated classes are Copyright (c) 1998, James R. Weeks and BioElectroMech + * see(Jmol/src/com/obrador/license.txt) + * + * javjs.img.JpegEncoder.java was adapted by Bob Hanson + * + * for Jmol in the following ways: + * + * 1) minor coding efficiencies were made in some for() loops. + * 2) methods not used by Jmol were commented out + * 3) method and variable signatures were modified to provide + * more appropriate method privacy. + * 4) additions for Java2Script compatibility + * + * Original files are maintained in the Jmol.src.com.obrador package, but + * these original files are not distributed with Jmol. + * +*/ + +package javajs.img; + +import java.io.IOException; +import java.util.Map; + +import javajs.img.ImageEncoder; +import javajs.util.AU; +import javajs.util.OC; + +/** + * JpegEncoder - The JPEG main program which performs a jpeg compression of an + * image. + * + * A system to allow the full Jmol state -- regardless of length -- + * to be encoded in a set of APP1 (FFE1) tags. + * But we have to be careful about line ends for backward compatibility. + * This solution is not 100% effective, because some data lines may in principle be + * Very large and may not contain new lines for more than 65500 characters, + * But that would be very unusual. Perhaps a huge data set loaded from a + * string. Introduced in Jmol 12.1.36. Bob Hanson + * + * See org.com.obrador.license.txt + * + */ + +public class JpgEncoder extends ImageEncoder { + + // this string will GENERALLY appear at the end of lines and be escaped + private static final int CONTINUE_MAX = 65500; // some room to spare here. + private static final int CONTINUE_MAX_BUFFER = CONTINUE_MAX + 10; // never break up last 10 bytes + + private JpegObj jpegObj; + private Huffman huf; + private DCT dct; + protected int defaultQuality = 100; + private String applicationTag; + + public JpgEncoder() { + + } + + @Override + protected void setParams(Map params) { + if (quality <= 0) + quality = (params.containsKey("qualityJPG") ? ((Integer) params.get("qualityJPG")).intValue() : defaultQuality); + jpegObj = new JpegObj(); + jpegObj.comment = (String) params.get("comment"); + applicationTag = (String) params.get("jpgAppTag"); + } + + @Override + protected void generate() throws IOException { + jpegObj.imageWidth = width; + jpegObj.imageHeight = height; + dct = new DCT(quality); + huf = new Huffman(width, height); + if (jpegObj == null) + return; + jpegObj.getYCCArray(pixels); + String longState = writeHeaders(jpegObj, dct); + writeCompressedData(jpegObj, dct, huf); + writeMarker(eoi); + if (longState != null) { + byte[] b = longState.getBytes(); + out.write(b, 0, b.length); + } + } + + private void writeCompressedData(JpegObj jpegObj, DCT dct, Huffman huf) { + int i, j, r, c, a, b; + int comp, xpos, ypos, xblockoffset, yblockoffset; + float inputArray[][]; + float dctArray1[][] = new float[8][8]; + double dctArray2[][] = new double[8][8]; + int dctArray3[] = new int[8 * 8]; + + /* + * This method controls the compression of the image. + * Starting at the upper left of the image, it compresses 8x8 blocks + * of data until the entire image has been compressed. + */ + + int lastDCvalue[] = new int[jpegObj.numberOfComponents]; + //int zeroArray[] = new int[64]; // initialized to hold all zeros + //int Width = 0, Height = 0; + //int nothing = 0, not; + int minBlockWidth, minBlockHeight; + // This initial setting of MinBlockWidth and MinBlockHeight is done to + // ensure they start with values larger than will actually be the case. + minBlockWidth = ((huf.imageWidth % 8 != 0) ? (int) (Math + .floor(huf.imageWidth / 8.0) + 1) * 8 : huf.imageWidth); + minBlockHeight = ((huf.imageHeight % 8 != 0) ? (int) (Math + .floor(huf.imageHeight / 8.0) + 1) * 8 : huf.imageHeight); + for (comp = 0; comp < jpegObj.numberOfComponents; comp++) { + minBlockWidth = Math.min(minBlockWidth, jpegObj.blockWidth[comp]); + minBlockHeight = Math.min(minBlockHeight, jpegObj.blockHeight[comp]); + } + xpos = 0; + for (r = 0; r < minBlockHeight; r++) { + for (c = 0; c < minBlockWidth; c++) { + xpos = c * 8; + ypos = r * 8; + for (comp = 0; comp < jpegObj.numberOfComponents; comp++) { + //Width = JpegObj.BlockWidth[comp]; + //Height = JpegObj.BlockHeight[comp]; + inputArray = jpegObj.components[comp]; + int vsampF = jpegObj.vsampFactor[comp]; + int hsampF = jpegObj.hsampFactor[comp]; + int qNumber = jpegObj.qtableNumber[comp]; + int dcNumber = jpegObj.dctableNumber[comp]; + int acNumber = jpegObj.actableNumber[comp]; + + for (i = 0; i < vsampF; i++) { + for (j = 0; j < hsampF; j++) { + xblockoffset = j * 8; + yblockoffset = i * 8; + for (a = 0; a < 8; a++) { + for (b = 0; b < 8; b++) { + + // I believe this is where the dirty line at the bottom of + // the image is coming from. + // I need to do a check here to make sure I'm not reading past + // image data. + // This seems to not be a big issue right now. (04/04/98) + + dctArray1[a][b] = inputArray[ypos + yblockoffset + a][xpos + + xblockoffset + b]; + } + } + // The following code commented out because on some images this technique + // results in poor right and bottom borders. + // if ((!JpegObj.lastColumnIsDummy[comp] || c < Width - 1) && + // (!JpegObj.lastRowIsDummy[comp] || r < Height - 1)) { + dctArray2 = DCT.forwardDCT(dctArray1); + dctArray3 = DCT.quantizeBlock(dctArray2, dct.divisors[qNumber]); + // } + // else { + // zeroArray[0] = dctArray3[0]; + // zeroArray[0] = lastDCvalue[comp]; + // dctArray3 = zeroArray; + // } + huf.HuffmanBlockEncoder(out, dctArray3, lastDCvalue[comp], + dcNumber, acNumber); + lastDCvalue[comp] = dctArray3[0]; + } + } + } + } + } + huf.flushBuffer(out); + } + + private static byte[] eoi = { (byte) 0xFF, (byte) 0xD9 }; + + private static byte[] jfif = new byte[] { + /* JFIF[0] =*/(byte) 0xff, + /* JFIF[1] =*/(byte) 0xe0, + /* JFIF[2] =*/0, + /* JFIF[3] =*/16, + /* JFIF[4] =*/(byte) 0x4a, //'J' + /* JFIF[5] =*/(byte) 0x46, //'F' + /* JFIF[6] =*/(byte) 0x49, //'I' + /* JFIF[7] =*/(byte) 0x46, //'F' + /* JFIF[8] =*/0, + /* JFIF[9] =*/1, + /* JFIF[10] =*/0, + /* JFIF[11] =*/0, + /* JFIF[12] =*/0, + /* JFIF[13] =*/1, + /* JFIF[14] =*/0, + /* JFIF[15] =*/1, + /* JFIF[16] =*/0, + /* JFIF[17] =*/0 }; + + private static byte[] soi = { (byte) 0xFF, (byte) 0xD8 }; + + private String writeHeaders(JpegObj jpegObj, DCT dct) { + int i, j, index, offset; + int tempArray[]; + + // the SOI marker + writeMarker(soi); + + // The order of the following headers is quite inconsequential. + // the JFIF header + writeArray(jfif); + + // Comment Header + String comment = null; + if (jpegObj.comment != null && jpegObj.comment.length() > 0) + writeString(jpegObj.comment, (byte) 0xE1); // App data 1 + writeString( + "JPEG Encoder Copyright 1998, James R. Weeks and BioElectroMech.\n\n", + (byte) 0xFE); + + // The DQT header + // 0 is the luminance index and 1 is the chrominance index + byte dqt[] = new byte[134]; + dqt[0] = (byte) 0xFF; + dqt[1] = (byte) 0xDB; + dqt[2] = 0; + dqt[3] = (byte) 132; + offset = 4; + for (i = 0; i < 2; i++) { + dqt[offset++] = (byte) ((0 << 4) + i); + tempArray = dct.quantum[i]; + for (j = 0; j < 64; j++) { + dqt[offset++] = (byte) tempArray[Huffman.jpegNaturalOrder[j]]; + } + } + writeArray(dqt); + + // Start of Frame Header + byte sof[] = new byte[19]; + sof[0] = (byte) 0xFF; + sof[1] = (byte) 0xC0; + sof[2] = 0; + sof[3] = 17; + sof[4] = (byte) jpegObj.precision; + sof[5] = (byte) ((jpegObj.imageHeight >> 8) & 0xFF); + sof[6] = (byte) ((jpegObj.imageHeight) & 0xFF); + sof[7] = (byte) ((jpegObj.imageWidth >> 8) & 0xFF); + sof[8] = (byte) ((jpegObj.imageWidth) & 0xFF); + sof[9] = (byte) jpegObj.numberOfComponents; + index = 10; + for (i = 0; i < sof[9]; i++) { + sof[index++] = (byte) jpegObj.compID[i]; + sof[index++] = (byte) ((jpegObj.hsampFactor[i] << 4) + jpegObj.vsampFactor[i]); + sof[index++] = (byte) jpegObj.qtableNumber[i]; + } + writeArray(sof); + + WriteDHTHeader(Huffman.bitsDCluminance, Huffman.valDCluminance); + WriteDHTHeader(Huffman.bitsACluminance, Huffman.valACluminance); + WriteDHTHeader(Huffman.bitsDCchrominance, Huffman.valDCchrominance); + WriteDHTHeader(Huffman.bitsACchrominance, Huffman.valACchrominance); + + // Start of Scan Header + byte sos[] = new byte[14]; + sos[0] = (byte) 0xFF; + sos[1] = (byte) 0xDA; + sos[2] = 0; + sos[3] = 12; + sos[4] = (byte) jpegObj.numberOfComponents; + index = 5; + for (i = 0; i < sos[4]; i++) { + sos[index++] = (byte) jpegObj.compID[i]; + sos[index++] = (byte) ((jpegObj.dctableNumber[i] << 4) + jpegObj.actableNumber[i]); + } + sos[index++] = (byte) jpegObj.ss; + sos[index++] = (byte) jpegObj.se; + sos[index++] = (byte) ((jpegObj.ah << 4) + jpegObj.al); + writeArray(sos); + return comment; + } + + private void writeString(String s, byte id) { + int len = s.length(); + int i0 = 0; + String suffix = applicationTag; + while (i0 < len) { + int nBytes = len - i0; + if (nBytes > CONTINUE_MAX_BUFFER) { + nBytes = CONTINUE_MAX; + // but break only at line breaks + int pt = s.lastIndexOf('\n', i0 + nBytes); + if (pt > i0 + 1) + nBytes = pt - i0; + } + if (i0 + nBytes == len) + suffix = ""; + writeTag(nBytes + suffix.length(), id); + writeArray(s.substring(i0, i0 + nBytes).getBytes()); + if (suffix.length() > 0) + writeArray(suffix.getBytes()); + i0 += nBytes; + } + } + + private void writeTag(int length, byte id) { + length += 2; + byte com[] = new byte[4]; + com[0] = (byte) 0xFF; + com[1] = id; + com[2] = (byte) ((length >> 8) & 0xFF); + com[3] = (byte) (length & 0xFF); + writeArray(com); + } + + void WriteDHTHeader(int[] bits, int[] val) { + // hansonr@stolaf.edu: simplified code. + byte[] dht; + int bytes = 0; + for (int j = 1; j < 17; j++) + bytes += bits[j]; + dht = new byte[21 + bytes]; + dht[0] = (byte) 0xFF; + dht[1] = (byte) 0xC4; + int index = 4; + for (int j = 0; j < 17; j++) + dht[index++] = (byte) bits[j]; + for (int j = 0; j < bytes; j++) + dht[index++] = (byte) val[j]; + dht[2] = (byte) (((index - 2) >> 8) & 0xFF); + dht[3] = (byte) ((index - 2) & 0xFF); + writeArray(dht); + } + + void writeMarker(byte[] data) { + out.write(data, 0, 2); + } + + void writeArray(byte[] data) { + out.write(data, 0, data.length); + } + +} + +// This class incorporates quality scaling as implemented in the JPEG-6a +// library. + +/* + * DCT - A Java implementation of the Discreet Cosine Transform + */ + +class DCT { + + /** + * DCT Block Size - default 8 + */ + private final static int N = 8; + private final static int NN = N * N; + + /** + * Image Quality (0-100) - default 80 (good image / good compression) + */ + //public int QUALITY = 80; + + int[][] quantum = AU.newInt2(2); + double[][] divisors = AU.newDouble2(2); + + /** + * Quantitization Matrix for luminace. + */ + private int quantum_luminance[] = new int[NN]; + private double DivisorsLuminance[] = new double[NN]; + + /** + * Quantitization Matrix for chrominance. + */ + private int quantum_chrominance[] = new int[NN]; + private double DivisorsChrominance[] = new double[NN]; + + /** + * Constructs a new DCT object. Initializes the cosine transform matrix these + * are used when computing the DCT and it's inverse. This also initializes the + * run length counters and the ZigZag sequence. Note that the image quality + * can be worse than 25 however the image will be extemely pixelated, usually + * to a block size of N. + * + * @param quality + * The quality of the image (0 worst - 100 best) + * + */ + DCT(int quality) { + initMatrix(quality); + } + + /* + * This method sets up the quantization matrix for luminance and + * chrominance using the Quality parameter. + */ + private void initMatrix(int quality) { + // converting quality setting to that specified in the jpeg_quality_scaling + // method in the IJG Jpeg-6a C libraries + + quality = (quality < 1 ? 1 : quality > 100 ? 100 : quality); + quality = (quality < 50 ? 5000 / quality : 200 - quality * 2); + + // Creating the luminance matrix + + quantum_luminance[0] = 16; + quantum_luminance[1] = 11; + quantum_luminance[2] = 10; + quantum_luminance[3] = 16; + quantum_luminance[4] = 24; + quantum_luminance[5] = 40; + quantum_luminance[6] = 51; + quantum_luminance[7] = 61; + quantum_luminance[8] = 12; + quantum_luminance[9] = 12; + quantum_luminance[10] = 14; + quantum_luminance[11] = 19; + quantum_luminance[12] = 26; + quantum_luminance[13] = 58; + quantum_luminance[14] = 60; + quantum_luminance[15] = 55; + quantum_luminance[16] = 14; + quantum_luminance[17] = 13; + quantum_luminance[18] = 16; + quantum_luminance[19] = 24; + quantum_luminance[20] = 40; + quantum_luminance[21] = 57; + quantum_luminance[22] = 69; + quantum_luminance[23] = 56; + quantum_luminance[24] = 14; + quantum_luminance[25] = 17; + quantum_luminance[26] = 22; + quantum_luminance[27] = 29; + quantum_luminance[28] = 51; + quantum_luminance[29] = 87; + quantum_luminance[30] = 80; + quantum_luminance[31] = 62; + quantum_luminance[32] = 18; + quantum_luminance[33] = 22; + quantum_luminance[34] = 37; + quantum_luminance[35] = 56; + quantum_luminance[36] = 68; + quantum_luminance[37] = 109; + quantum_luminance[38] = 103; + quantum_luminance[39] = 77; + quantum_luminance[40] = 24; + quantum_luminance[41] = 35; + quantum_luminance[42] = 55; + quantum_luminance[43] = 64; + quantum_luminance[44] = 81; + quantum_luminance[45] = 104; + quantum_luminance[46] = 113; + quantum_luminance[47] = 92; + quantum_luminance[48] = 49; + quantum_luminance[49] = 64; + quantum_luminance[50] = 78; + quantum_luminance[51] = 87; + quantum_luminance[52] = 103; + quantum_luminance[53] = 121; + quantum_luminance[54] = 120; + quantum_luminance[55] = 101; + quantum_luminance[56] = 72; + quantum_luminance[57] = 92; + quantum_luminance[58] = 95; + quantum_luminance[59] = 98; + quantum_luminance[60] = 112; + quantum_luminance[61] = 100; + quantum_luminance[62] = 103; + quantum_luminance[63] = 99; + + AANscale(DivisorsLuminance, quantum_luminance, quality); + + // Creating the chrominance matrix + + for (int i = 4; i < 64; i++) + quantum_chrominance[i] = 99; + + quantum_chrominance[0] = 17; + quantum_chrominance[1] = 18; + quantum_chrominance[2] = 24; + quantum_chrominance[3] = 47; + + quantum_chrominance[8] = 18; + quantum_chrominance[9] = 21; + quantum_chrominance[10] = 26; + quantum_chrominance[11] = 66; + + quantum_chrominance[16] = 24; + quantum_chrominance[17] = 26; + quantum_chrominance[18] = 56; + + quantum_chrominance[24] = 47; + quantum_chrominance[25] = 66; + + AANscale(DivisorsChrominance, quantum_chrominance, quality); + + // quantum and Divisors are objects used to hold the appropriate matices + + quantum[0] = quantum_luminance; + quantum[1] = quantum_chrominance; + + divisors[0] = DivisorsLuminance; + divisors[1] = DivisorsChrominance; + + } + + private final static double[] AANscaleFactor = { 1.0, 1.387039845, + 1.306562965, 1.175875602, 1.0, 0.785694958, 0.541196100, 0.275899379 }; + + static private void AANscale(double[] divisors, int[] values, int quality) { + + for (int j = 0; j < 64; j++) { + int temp = (values[j] * quality + 50) / 100; + values[j] = (temp < 1 ? 1 : temp > 255 ? 255 : temp); + } + + for (int i = 0, index = 0; i < 8; i++) + for (int j = 0; j < 8; j++, index++) + // The divisors for the LL&M method (the slow integer method used in + // jpeg 6a library). This method is currently (04/04/98) incompletely + // implemented. + // DivisorsLuminance[index] = ((double) quantum_luminance[index]) << 3; + // The divisors for the AAN method (the float method used in jpeg 6a library. + divisors[index] = (0.125 / (values[index] * AANscaleFactor[i] * AANscaleFactor[j])); + } + + /* + * This method preforms forward DCT on a block of image data using + * the literal method specified for a 2-D Discrete Cosine Transform. + * It is included as a curiosity and can give you an idea of the + * difference in the compression result (the resulting image quality) + * by comparing its output to the output of the AAN method below. + * It is ridiculously inefficient. + */ + + // For now the final output is unusable. The associated quantization step + // needs some tweaking. If you get this part working, please let me know. + /* + public double[][] forwardDCTExtreme(float input[][]) + { + double output[][] = new double[N][N]; + int v, u, x, y; + for (v = 0; v < 8; v++) { + for (u = 0; u < 8; u++) { + for (x = 0; x < 8; x++) { + for (y = 0; y < 8; y++) { + output[v][u] += input[x][y] * + Math.cos(((double)(2*x + 1)*(double)u*Math.PI)/16)* + Math.cos(((double)(2*y + 1)*(double)v*Math.PI)/16); + } + } + output[v][u] *= (0.25)*((u == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0)*((v == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0); + } + } + return output; + } + + */ + /* + * This method preforms a DCT on a block of image data using the AAN + * method as implemented in the IJG Jpeg-6a library. + */ + static double[][] forwardDCT(float input[][]) { + double output[][] = new double[N][N]; + double tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + double tmp10, tmp11, tmp12, tmp13; + double z1, z2, z3, z4, z5, z11, z13; + // Subtracts 128 from the input values + for (int i = 0; i < 8; i++) + for (int j = 0; j < 8; j++) + output[i][j] = (input[i][j] - 128.0); + // input[i][j] -= 128; + + for (int i = 0; i < 8; i++) { + tmp0 = output[i][0] + output[i][7]; + tmp7 = output[i][0] - output[i][7]; + tmp1 = output[i][1] + output[i][6]; + tmp6 = output[i][1] - output[i][6]; + tmp2 = output[i][2] + output[i][5]; + tmp5 = output[i][2] - output[i][5]; + tmp3 = output[i][3] + output[i][4]; + tmp4 = output[i][3] - output[i][4]; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + output[i][0] = tmp10 + tmp11; + output[i][4] = tmp10 - tmp11; + + z1 = (tmp12 + tmp13) * 0.707106781; + output[i][2] = tmp13 + z1; + output[i][6] = tmp13 - z1; + + tmp10 = tmp4 + tmp5; + tmp11 = tmp5 + tmp6; + tmp12 = tmp6 + tmp7; + + z5 = (tmp10 - tmp12) * 0.382683433; + z2 = 0.541196100 * tmp10 + z5; + z4 = 1.306562965 * tmp12 + z5; + z3 = tmp11 * 0.707106781; + + z11 = tmp7 + z3; + z13 = tmp7 - z3; + + output[i][5] = z13 + z2; + output[i][3] = z13 - z2; + output[i][1] = z11 + z4; + output[i][7] = z11 - z4; + } + + for (int i = 0; i < 8; i++) { + tmp0 = output[0][i] + output[7][i]; + tmp7 = output[0][i] - output[7][i]; + tmp1 = output[1][i] + output[6][i]; + tmp6 = output[1][i] - output[6][i]; + tmp2 = output[2][i] + output[5][i]; + tmp5 = output[2][i] - output[5][i]; + tmp3 = output[3][i] + output[4][i]; + tmp4 = output[3][i] - output[4][i]; + + tmp10 = tmp0 + tmp3; + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + output[0][i] = tmp10 + tmp11; + output[4][i] = tmp10 - tmp11; + + z1 = (tmp12 + tmp13) * 0.707106781; + output[2][i] = tmp13 + z1; + output[6][i] = tmp13 - z1; + + tmp10 = tmp4 + tmp5; + tmp11 = tmp5 + tmp6; + tmp12 = tmp6 + tmp7; + + z5 = (tmp10 - tmp12) * 0.382683433; + z2 = 0.541196100 * tmp10 + z5; + z4 = 1.306562965 * tmp12 + z5; + z3 = tmp11 * 0.707106781; + + z11 = tmp7 + z3; + z13 = tmp7 - z3; + + output[5][i] = z13 + z2; + output[3][i] = z13 - z2; + output[1][i] = z11 + z4; + output[7][i] = z11 - z4; + } + + return output; + } + + /* + * This method quantitizes data and rounds it to the nearest integer. + */ + static int[] quantizeBlock(double inputData[][], double[] divisorsCode) { + int outputData[] = new int[NN]; + for (int i = 0, index = 0; i < 8; i++) + for (int j = 0; j < 8; j++, index++) + // The second line results in significantly better compression. + outputData[index] = (int) (Math.round(inputData[i][j] + * divisorsCode[index])); + // outputData[index] = (int)(((inputData[i][j] * (((double[]) (Divisors[code]))[index])) + 16384.5) -16384); + return outputData; + } + + /* + * This is the method for quantizing a block DCT'ed with forwardDCTExtreme + * This method quantitizes data and rounds it to the nearest integer. + */ + + /* + + public double[][] forwardDCTExtreme(float input[][]) + { + double output[][] = new double[N][N]; + int v, u, x, y; + for (v = 0; v < 8; v++) { + for (u = 0; u < 8; u++) { + for (x = 0; x < 8; x++) { + for (y = 0; y < 8; y++) { + output[v][u] += input[x][y] * + Math.cos(((double)(2*x + 1)*(double)u*Math.PI)/16)* + Math.cos(((double)(2*y + 1)*(double)v*Math.PI)/16); + } + } + output[v][u] *= (0.25)*((u == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0)*((v == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0); + } + } + return output; + } + + */ + /* + public int[] quantizeBlockExtreme(double inputData[][], int code) + { + int outputData[] = new int[NN]; + int i, j; + int index; + index = 0; + for (i = 0; i < 8; i++) { + for (j = 0; j < 8; j++) { + outputData[index] = (int)(Math.round(inputData[i][j] / (((int[]) (quantum[code]))[index]))); + index++; + } + } + + return outputData; + } + */ +} + +// This class was modified by James R. Weeks on 3/27/98. +// It now incorporates Huffman table derivation as in the C jpeg library +// from the IJG, Jpeg-6a. + +class Huffman { + private int bufferPutBits, bufferPutBuffer; + int imageHeight; + int imageWidth; + private int dc_matrix0[][]; + private int ac_matrix0[][]; + private int dc_matrix1[][]; + private int ac_matrix1[][]; + private int[][][] dc_matrix; + private int[][][] ac_matrix; + //private int code; + int numOfDCTables; + int numOfACTables; + final static int[] bitsDCluminance = { 0x00, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, + 0, 0, 0, 0, 0 }; + final static int[] valDCluminance = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; + final static int[] bitsDCchrominance = { 0x01, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0 }; + final static int[] valDCchrominance = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; + final static int[] bitsACluminance = { 0x10, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, + 4, 0, 0, 1, 0x7d }; + final static int[] valACluminance = { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, + 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, + 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, + 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, + 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, + 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, + 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, + 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, + 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, + 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa }; + final static int[] bitsACchrominance = { 0x11, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, + 4, 4, 0, 1, 2, 0x77 }; + final static int[] valACchrominance = { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, + 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, + 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, + 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, + 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, + 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, + 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, + 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, + 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, + 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, + 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, + 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa }; + + /* + * jpegNaturalOrder[i] is the natural-order position of the i'th element + * of zigzag order. + */ + final static int[] jpegNaturalOrder = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, + 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, + 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, + 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, }; + + Huffman(int width, int height) { + initHuf(); + imageWidth = width; + imageHeight = height; + + } + + /** + * HuffmanBlockEncoder run length encodes and Huffman encodes the quantized + * data. + * + * @param out + * @param zigzag + * @param prec + * @param dcCode + * @param acCode + **/ + + void HuffmanBlockEncoder(OC out, int zigzag[], int prec, + int dcCode, int acCode) { + int temp, temp2, nbits, k, r, i; + + numOfDCTables = 2; + numOfACTables = 2; + + int[][] matrixDC = dc_matrix[dcCode]; + int[][] matrixAC = ac_matrix[acCode]; + + // The DC portion + + temp = temp2 = zigzag[0] - prec; + if (temp < 0) { + temp = -temp; + temp2--; + } + nbits = 0; + while (temp != 0) { + nbits++; + temp >>= 1; + } + // if (nbits > 11) nbits = 11; + bufferIt(out, matrixDC[nbits][0], matrixDC[nbits][1]); + // The arguments in bufferIt are code and size. + if (nbits != 0) { + bufferIt(out, temp2, nbits); + } + + // The AC portion + + r = 0; + + for (k = 1; k < 64; k++) { + if ((temp = zigzag[jpegNaturalOrder[k]]) == 0) { + r++; + } else { + while (r > 15) { + bufferIt(out, matrixAC[0xF0][0], matrixAC[0xF0][1]); + r -= 16; + } + temp2 = temp; + if (temp < 0) { + temp = -temp; + temp2--; + } + nbits = 1; + while ((temp >>= 1) != 0) { + nbits++; + } + i = (r << 4) + nbits; + bufferIt(out, matrixAC[i][0], matrixAC[i][1]); + bufferIt(out, temp2, nbits); + + r = 0; + } + } + + if (r > 0) { + bufferIt(out, matrixAC[0][0], matrixAC[0][1]); + } + + } + + // Uses an integer long (32 bits) buffer to store the Huffman encoded bits + // and sends them to out by the byte. + + void bufferIt(OC out, int code, int size) { + int putBuffer = code; + int putBits = bufferPutBits; + + putBuffer &= (1 << size) - 1; + putBits += size; + putBuffer <<= 24 - putBits; + putBuffer |= bufferPutBuffer; + + while (putBits >= 8) { + int c = ((putBuffer >> 16) & 0xFF); + out.writeByteAsInt(c); + if (c == 0xFF) { + out.writeByteAsInt(0); + } + putBuffer <<= 8; + putBits -= 8; + } + bufferPutBuffer = putBuffer; + bufferPutBits = putBits; + + } + + void flushBuffer(OC out) { + int putBuffer = bufferPutBuffer; + int putBits = bufferPutBits; + while (putBits >= 8) { + int c = ((putBuffer >> 16) & 0xFF); + out.writeByteAsInt(c); + if (c == 0xFF) { + out.writeByteAsInt(0); + } + putBuffer <<= 8; + putBits -= 8; + } + if (putBits > 0) { + int c = ((putBuffer >> 16) & 0xFF); + out.writeByteAsInt(c); + } + } + + /* + * Initialisation of the Huffman codes for Luminance and Chrominance. + * This code results in the same tables created in the IJG Jpeg-6a + * library. + */ + + private void initHuf() { + dc_matrix0 = new int[12][2]; + dc_matrix1 = new int[12][2]; + ac_matrix0 = new int[255][2]; + ac_matrix1 = new int[255][2]; + dc_matrix = AU.newInt3(2, -1); + ac_matrix = AU.newInt3(2, -1); + int p, l, i, lastp, si, code; + int[] huffsize = new int[257]; + int[] huffcode = new int[257]; + + /* + * init of the DC values for the chrominance + * [][0] is the code [][1] is the number of bit + */ + + p = 0; + for (l = 1; l <= 16; l++) { + // for (i = 1; i <= bitsDCchrominance[l]; i++) + for (i = bitsDCchrominance[l]; --i >= 0;) { + huffsize[p++] = l; //that's an "el", not a "one" + } + } + huffsize[p] = 0; + lastp = p; + + code = 0; + si = huffsize[0]; + p = 0; + while (huffsize[p] != 0) { + while (huffsize[p] == si) { + huffcode[p++] = code; + code++; + } + code <<= 1; + si++; + } + + for (p = 0; p < lastp; p++) { + dc_matrix1[valDCchrominance[p]][0] = huffcode[p]; + dc_matrix1[valDCchrominance[p]][1] = huffsize[p]; + } + + /* + * Init of the AC huffman code for the chrominance + * matrix [][][0] is the code & matrix[][][1] is the number of bit needed + */ + + p = 0; + for (l = 1; l <= 16; l++) { + for (i = bitsACchrominance[l]; --i >= 0;) + // for (i = 1; i <= bitsACchrominance[l]; i++) + { + huffsize[p++] = l; + } + } + huffsize[p] = 0; + lastp = p; + + code = 0; + si = huffsize[0]; + p = 0; + while (huffsize[p] != 0) { + while (huffsize[p] == si) { + huffcode[p++] = code; + code++; + } + code <<= 1; + si++; + } + + for (p = 0; p < lastp; p++) { + ac_matrix1[valACchrominance[p]][0] = huffcode[p]; + ac_matrix1[valACchrominance[p]][1] = huffsize[p]; + } + + /* + * init of the DC values for the luminance + * [][0] is the code [][1] is the number of bit + */ + p = 0; + for (l = 1; l <= 16; l++) { + // for (i = 1; i <= bitsDCluminance[l]; i++) + for (i = bitsDCluminance[l]; --i >= 0;) { + huffsize[p++] = l; + } + } + huffsize[p] = 0; + lastp = p; + + code = 0; + si = huffsize[0]; + p = 0; + while (huffsize[p] != 0) { + while (huffsize[p] == si) { + huffcode[p++] = code; + code++; + } + code <<= 1; + si++; + } + + for (p = 0; p < lastp; p++) { + dc_matrix0[valDCluminance[p]][0] = huffcode[p]; + dc_matrix0[valDCluminance[p]][1] = huffsize[p]; + } + + /* + * Init of the AC huffman code for luminance + * matrix [][][0] is the code & matrix[][][1] is the number of bit + */ + + p = 0; + for (l = 1; l <= 16; l++) { + // for (i = 1; i <= bitsACluminance[l]; i++) + for (i = bitsACluminance[l]; --i >= 0;) { + huffsize[p++] = l; + } + } + huffsize[p] = 0; + lastp = p; + + code = 0; + si = huffsize[0]; + p = 0; + while (huffsize[p] != 0) { + while (huffsize[p] == si) { + huffcode[p++] = code; + code++; + } + code <<= 1; + si++; + } + for (int q = 0; q < lastp; q++) { + ac_matrix0[valACluminance[q]][0] = huffcode[q]; + ac_matrix0[valACluminance[q]][1] = huffsize[q]; + } + + dc_matrix[0] = dc_matrix0; + dc_matrix[1] = dc_matrix1; + ac_matrix[0] = ac_matrix0; + ac_matrix[1] = ac_matrix1; + } + +} + +/* + * JpegInfo - Given an image, sets default information about it and divides + * it into its constituant components, downsizing those that need to be. + */ + +class JpegObj { + String comment; + int imageHeight; + int imageWidth; + int blockWidth[]; + int blockHeight[]; + + int precision = 8; + int numberOfComponents = 3; + float[][][] components; + int[] compID = { 1, 2, 3 }; + int[] hsampFactor = { 1, 1, 1 }; + int[] vsampFactor = { 1, 1, 1 }; + int[] qtableNumber = { 0, 1, 1 }; + int[] dctableNumber = { 0, 1, 1 }; + int[] actableNumber = { 0, 1, 1 }; + private boolean[] lastColumnIsDummy = { false, false, false }; + private boolean[] lastRowIsDummy = { false, false, false }; + int ss = 0; + int se = 63; + int ah = 0; + int al = 0; + private int compWidth[]; + private int compHeight[]; + private int maxHsampFactor; + private int maxVsampFactor; + + public JpegObj() { + components = AU.newFloat3(numberOfComponents, -1); + compWidth = new int[numberOfComponents]; + compHeight = new int[numberOfComponents]; + blockWidth = new int[numberOfComponents]; + blockHeight = new int[numberOfComponents]; + } + + /* + * This method creates and fills three arrays, Y, Cb, and Cr using the + * input image. + */ + + void getYCCArray(int[] pixels) { + // In order to minimize the chance that grabPixels will throw an exception + // it may be necessary to grab some pixels every few scanlines and process + // those before going for more. The time expense may be prohibitive. + // However, for a situation where memory overhead is a concern, this may be + // the only choice. + maxHsampFactor = 1; + maxVsampFactor = 1; + for (int y = 0; y < numberOfComponents; y++) { + maxHsampFactor = Math.max(maxHsampFactor, hsampFactor[y]); + maxVsampFactor = Math.max(maxVsampFactor, vsampFactor[y]); + } + for (int y = 0; y < numberOfComponents; y++) { + compWidth[y] = (((imageWidth % 8 != 0) ? ((int) Math + .ceil(imageWidth / 8.0)) * 8 : imageWidth) / maxHsampFactor) + * hsampFactor[y]; + if (compWidth[y] != ((imageWidth / maxHsampFactor) * hsampFactor[y])) { + lastColumnIsDummy[y] = true; + } + // results in a multiple of 8 for compWidth + // this will make the rest of the program fail for the unlikely + // event that someone tries to compress an 16 x 16 pixel image + // which would of course be worse than pointless + blockWidth[y] = (int) Math.ceil(compWidth[y] / 8.0); + compHeight[y] = (((imageHeight % 8 != 0) ? ((int) Math + .ceil(imageHeight / 8.0)) * 8 : imageHeight) / maxVsampFactor) + * vsampFactor[y]; + if (compHeight[y] != ((imageHeight / maxVsampFactor) * vsampFactor[y])) { + lastRowIsDummy[y] = true; + } + blockHeight[y] = (int) Math.ceil(compHeight[y] / 8.0); + } + float Y[][] = new float[compHeight[0]][compWidth[0]]; + float Cr1[][] = new float[compHeight[0]][compWidth[0]]; + float Cb1[][] = new float[compHeight[0]][compWidth[0]]; + //float Cb2[][] = new float[compHeight[1]][compWidth[1]]; + //float Cr2[][] = new float[compHeight[2]][compWidth[2]]; + for (int pt = 0, y = 0; y < imageHeight; ++y) { + for (int x = 0; x < imageWidth; ++x, pt++) { + int p = pixels[pt]; + int r = ((p >> 16) & 0xff); + int g = ((p >> 8) & 0xff); + int b = (p & 0xff); + // The following three lines are a more correct color conversion but + // the current conversion technique is sufficient and results in a higher + // compression rate. + // Y[y][x] = 16 + (float)(0.8588*(0.299 * (float)r + 0.587 * (float)g + 0.114 * (float)b )); + // Cb1[y][x] = 128 + (float)(0.8784*(-0.16874 * (float)r - 0.33126 * (float)g + 0.5 * (float)b)); + // Cr1[y][x] = 128 + (float)(0.8784*(0.5 * (float)r - 0.41869 * (float)g - 0.08131 * (float)b)); + Y[y][x] = (float) ((0.299 * r + 0.587 * g + 0.114 * b)); + Cb1[y][x] = 128 + (float) ((-0.16874 * r - 0.33126 * g + 0.5 * b)); + Cr1[y][x] = 128 + (float) ((0.5 * r - 0.41869 * g - 0.08131 * b)); + } + } + + // Need a way to set the H and V sample factors before allowing downsampling. + // For now (04/04/98) downsampling must be hard coded. + // Until a better downsampler is implemented, this will not be done. + // Downsampling is currently supported. The downsampling method here + // is a simple box filter. + + components[0] = Y; + // Cb2 = DownSample(Cb1, 1); + components[1] = Cb1; + // Cr2 = DownSample(Cr1, 2); + components[2] = Cr1; + } + /* + float[][] DownSample(float[][] C, int comp) + { + int inrow, incol; + int outrow, outcol; + float output[][]; + int bias; + inrow = 0; + incol = 0; + int cHeight = compHeight[comp]; + int cWidth = compWidth[comp]; + output = new float[cHeight][cWidth]; + + for (outrow = 0; outrow < cHeight; outrow++) { + bias = 1; + for (outcol = 0; outcol < cWidth; outcol++) { + output[outrow][outcol] = (C[inrow][incol++] + C[inrow++][incol--] + + C[inrow][incol++] + C[inrow--][incol++] + bias)/(float)4.0; + bias ^= 3; + } + inrow += 2; + incol = 0; + } + return output; + } + */ + +} diff --git a/src/javajs/img/PdfEncoder.java b/src/javajs/img/PdfEncoder.java new file mode 100644 index 0000000..7dfd4b8 --- /dev/null +++ b/src/javajs/img/PdfEncoder.java @@ -0,0 +1,108 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2009-06-30 18:58:33 -0500 (Tue, 30 Jun 2009) $ + * $Revision: 11158 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2002-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.img; + +import java.util.Hashtable; +import java.util.Map; + +import javajs.export.PDFCreator; + +/** + * A relatively primitive PDF generator that just makes a document with an image + * in it. + * + */ +public class PdfEncoder extends ImageEncoder { + + private boolean isLandscape; + private PDFCreator pdf; + private String comment; + + public PdfEncoder() { + // for Class.forName + } + + @Override + protected void setParams(Map params) { + isLandscape = (quality > 1); + comment = "Jmol " + (String) params.get("comment"); + } + + @Override + protected void generate() throws Exception { + pdf = new PDFCreator(); + int pageWidth = 8 * 72; + int pageHeight = 11 * 72; + pdf.setOutputStream(out); + pdf.newDocument(pageWidth, pageHeight, isLandscape); // A4 or Letter + addMyImage(pageWidth, pageHeight); + Map ht = new Hashtable(); + if (comment != null) + ht.put("Producer", comment); + ht.put("Author", "JMol"); + ht.put("CreationDate", date); + pdf.addInfo(ht); + pdf.closeDocument(); + } + + + /** + * centered on the page + * + * @param pageWidth + * @param pageHeight + */ + private void addMyImage(int pageWidth, int pageHeight) { + pdf.addImageResource("img1", width, height, pixels, true); + int w = (isLandscape ? pageHeight : pageWidth); + int h = (isLandscape ? pageWidth : pageHeight); + int iw = width; + int ih = height; + if (iw > 0.9 * w) { + ih = (int) (ih * 0.9 * w / iw); + iw = (int) (w * 0.9); + } + if (ih > 0.9 * h) { + iw = (int) (iw * 0.9 * h / ih); + ih = (int) (h * 0.9); + } + int x = 0; + int y = 0; + int x1 = iw; + int y1 = ih; + if (w > iw) { + x = (w - iw) / 2; + x1 = iw + x; + } + if (h > ih) { + y = (h - ih) / 2; + y1 = ih + y; + } + pdf.drawImage("img1", x, y, x1, y1, 0, 0, width, height); + } + +} diff --git a/src/javajs/img/PngEncoder.java b/src/javajs/img/PngEncoder.java new file mode 100644 index 0000000..2dc8485 --- /dev/null +++ b/src/javajs/img/PngEncoder.java @@ -0,0 +1,480 @@ +/* $RCSfile$ + * $Author: nicove $ + * $Date: 2007-03-30 12:26:16 -0500 (Fri, 30 Mar 2007) $ + * $Revision: 7275 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2002-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.img; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Map; +import java.util.zip.Deflater; +import java.util.zip.DeflaterOutputStream; + + + +/** + * + * Modified by Bob Hanson hansonr@stolaf.edu to be a subclass of ImageEncoder + * and to use javajs.util.OutputChannel instead of just returning bytes. Also includes: + * + * -- JavaScript-compatible image processing + * + * -- transparent background option + * + * -- more efficient calculation of needs for pngBytes + * + * -- option to use pre-created PNGJ image data (3/19/14; Jmol 14.1.12) + * + * -- PNGJ format: + * + * // IHDR chunk + * + * // tEXt chunk "Jmol type - <0000000pt>+<000000len>" + * + * // tEXt chunk "Software - Jmol " + * + * // tEXt chunk "Creation Time - " + * + * // tRNS chunk transparent color, if desired + * + * // IDAT chunk (image data) + * + * // IEND chunk + * + * // [JMOL ZIP FILE APPENDIX] + * + * Original Comment: + * + * PngEncoder takes a Java Image object and creates a byte string which can be + * saved as a PNG file. The Image is presumed to use the DirectColorModel. + * + * Thanks to Jay Denny at KeyPoint Software http://www.keypoint.com/ who let me + * develop this code on company time. + * + * You may contact me with (probably very-much-needed) improvements, comments, + * and bug fixes at: + * + * david@catcode.com + * + * @author J. David Eisenberg + * @author http://catcode.com/pngencoder/ + * @author Christian Ribeaud (christian.ribeaud@genedata.com) + * @author Bob Hanson (hansonr@stolaf.edu) + * + * @version 1.4, 31 March 2000 + */ +public class PngEncoder extends CRCEncoder { + + /** Constants for filters */ + public static final int FILTER_NONE = 0; + public static final int FILTER_SUB = 1; + public static final int FILTER_UP = 2; + public static final int FILTER_LAST = 2; + + private static final int PT_FIRST_TAG = 37; + + private boolean encodeAlpha; + private int filter = FILTER_NONE; + private int bytesPerPixel; + private int compressionLevel; + private String type; + private Integer transparentColor; + + private byte[] appData; + private String appPrefix; + private String comment; + private byte[] bytes; + + + public PngEncoder() { + super(); + } + + @Override + protected void setParams(Map params) { + if (quality < 0) + quality = (params.containsKey("qualityPNG") ? ((Integer) params + .get("qualityPNG")).intValue() : 2); + if (quality > 9) + quality = 9; + encodeAlpha = false; + filter = FILTER_NONE; + compressionLevel = quality; + transparentColor = (Integer) params.get("transparentColor"); + comment = (String) params.get("comment"); + type = (params.get("type") + "0000").substring(0, 4); + bytes = (byte[]) params.get("pngImgData"); + appData = (byte[]) params.get("pngAppData"); + appPrefix = (String) params.get("pngAppPrefix"); + } + + + + @Override + protected void generate() throws IOException { + if (bytes == null) { + if (!pngEncode()) { + out.cancel(); + return; + } + bytes = getBytes(); + } else { + dataLen = bytes.length; + } + int len = dataLen; + if (appData != null) { + setJmolTypeText(appPrefix, bytes, len, appData.length, + type); + out.write(bytes, 0, len); + len = (bytes = appData).length; + } + out.write(bytes, 0, len); + } + + + /** + * Creates an array of bytes that is the PNG equivalent of the current image, + * specifying whether to encode alpha or not. + * + * @return true if successful + * + */ + private boolean pngEncode() { + + byte[] pngIdBytes = { -119, 80, 78, 71, 13, 10, 26, 10 }; + + writeBytes(pngIdBytes); + //hdrPos = bytePos; + writeHeader(); + writeText(getApplicationText(appPrefix, type, 0, 0)); + + writeText("Software\0" + comment); + writeText("Creation Time\0" + date); + + if (!encodeAlpha && transparentColor != null) + writeTransparentColor(transparentColor.intValue()); + //dataPos = bytePos; + return writeImageData(); + } + + /** + * Fill in the Jmol type text area with number of bytes of PNG data and number + * of bytes of Jmol state data and fix checksum. + * + * If we do not do this, then the checksum will be wrong, and Jmol and some + * other programs may not be able to read the PNG image. + * + * This was corrected for Jmol 12.3.30. Between 12.3.7 and 12.3.29, PNG files + * created by Jmol have incorrect checksums. + * + * @param prefix + * + * @param b + * @param nPNG + * @param nState + * @param type + */ + private static void setJmolTypeText(String prefix, byte[] b, int nPNG, int nState, String type) { + String s = "tEXt" + getApplicationText(prefix, type, nPNG, nState); + CRCEncoder encoder = new PngEncoder(); + byte[] test = s.substring(0, 4 + prefix.length()).getBytes(); + for (int i = test.length; -- i >= 0;) + if (b[i + PT_FIRST_TAG] != test[i]) { + System.out.println("image is not of the right form; appending data, but not adding tEXt tag."); + return; + } + encoder.setData(b, PT_FIRST_TAG); + encoder.writeString(s); + encoder.writeCRC(); + } + + /** + * Generate the PNGJ directory identifier: + * + * xxxxxxxxx\0ttttiiiiiiiii+ddddddddd + * + * where + * + * xxxxxxxxx is a unique 9-character software identifier + * tttt is a four-byte software-specific type indicator (PNG0, PNGJ, PNGT, etc.) + * iiiiiiiii is the file pointer to the start of app data + * ddddddddd is the length of the app data + * + * @param prefix up to 9 characters to allow software to recognize itself + * @param type PNGx, where x is J or T for Jmol; original type "PNG" is now "PNG0" + * @param nPNG + * @param nData + * @return + */ + private static String getApplicationText(String prefix, String type, + int nPNG, int nData) { + String sPNG = "000000000" + nPNG; + sPNG = sPNG.substring(sPNG.length() - 9); + String sData = "000000000" + nData; + sData = sData.substring(sData.length() - 9); + if (prefix == null) + prefix = "#SwingJS."; + if (prefix.length() < 9) + prefix = (prefix + "........."); + if (prefix.length() > 9) + prefix = prefix.substring(0, 9); + return prefix + "\0" + type + sPNG + "+" + sData; + } + + // /** + // * Set the filter to use + // * + // * @param whichFilter from constant list + // */ + // public void setFilter(int whichFilter) { + // this.filter = (whichFilter <= FILTER_LAST ? whichFilter : FILTER_NONE); + // } + + // /** + // * Retrieve filtering scheme + // * + // * @return int (see constant list) + // */ + // public int getFilter() { + // return filter; + // } + + // /** + // * Set the compression level to use + // * + // * @param level 0 through 9 + // */ + // public void setCompressionLevel(int level) { + // if ((level >= 0) && (level <= 9)) { + // this.compressionLevel = level; + // } + // } + + // /** + // * Retrieve compression level + // * + // * @return int in range 0-9 + // */ + // public int getCompressionLevel() { + // return compressionLevel; + // } + + /** + * Write a PNG "IHDR" chunk into the pngBytes array. + */ + private void writeHeader() { + + writeInt4(13); + startPos = bytePos; + writeString("IHDR"); + writeInt4(width); + writeInt4(height); + writeByte(8); // bit depth + writeByte(encodeAlpha ? 6 : 2); // color type or direct model + writeByte(0); // compression method + writeByte(0); // filter method + writeByte(0); // no interlace + writeCRC(); + } + + private void writeText(String msg) { + writeInt4(msg.length()); + startPos = bytePos; + writeString("tEXt" + msg); + writeCRC(); + } + + /** + * Write a PNG "tRNS" chunk into the pngBytes array. + * + * @param icolor + */ + private void writeTransparentColor(int icolor) { + + writeInt4(6); + startPos = bytePos; + writeString("tRNS"); + writeInt2((icolor >> 16) & 0xFF); + writeInt2((icolor >> 8) & 0xFF); + writeInt2(icolor & 0xFF); + writeCRC(); + } + + private byte[] scanLines; // the scan lines to be compressed + private int byteWidth; // width * bytesPerPixel + + //private int hdrPos, dataPos, endPos; + //private byte[] priorRow; + //private byte[] leftBytes; + + + /** + * Write the image data into the pngBytes array. This will write one or more + * PNG "IDAT" chunks. In order to conserve memory, this method grabs as many + * rows as will fit into 32K bytes, or the whole image; whichever is less. + * + * + * @return true if no errors; false if error grabbing pixels + */ + private boolean writeImageData() { + + bytesPerPixel = (encodeAlpha ? 4 : 3); + byteWidth = width * bytesPerPixel; + + int scanWidth = byteWidth + 1; // the added 1 is for the filter byte + + //boolean doFilter = (filter != FILTER_NONE); + + int rowsLeft = height; // number of rows remaining to write + //int startRow = 0; // starting row to process this time through + int nRows; // how many rows to grab at a time + + int scanPos; // where we are in the scan lines + + Deflater deflater = new Deflater(compressionLevel); + ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024); + + DeflaterOutputStream compBytes = new DeflaterOutputStream(outBytes, + deflater); + + int pt = 0; // overall image byte pointer + + // Jmol note: The entire image has been stored in pixels[] already + + try { + while (rowsLeft > 0) { + nRows = Math.max(1, Math.min(32767 / scanWidth, rowsLeft)); + scanLines = new byte[scanWidth * nRows]; + // if (doFilter) + // switch (filter) { + // case FILTER_SUB: + // leftBytes = new byte[16]; + // break; + // case FILTER_UP: + // priorRow = new byte[scanWidth - 1]; + // break; + // } + int nPixels = width * nRows; + scanPos = 0; + //startPos = 1; + for (int i = 0; i < nPixels; i++, pt++) { + if (i % width == 0) { + scanLines[scanPos++] = (byte) filter; + //startPos = scanPos; + } + scanLines[scanPos++] = (byte) ((pixels[pt] >> 16) & 0xff); + scanLines[scanPos++] = (byte) ((pixels[pt] >> 8) & 0xff); + scanLines[scanPos++] = (byte) ((pixels[pt]) & 0xff); + if (encodeAlpha) { + scanLines[scanPos++] = (byte) ((pixels[pt] >> 24) & 0xff); + } + // if (doFilter && i % width == width - 1) { + // switch (filter) { + // case FILTER_SUB: + // filterSub(); + // break; + // case FILTER_UP: + // filterUp(); + // break; + // } + // } + } + + /* + * Write these lines to the output area + */ + compBytes.write(scanLines, 0, scanPos); + + //startRow += nRows; + rowsLeft -= nRows; + } + compBytes.close(); + + /* + * Write the compressed bytes + */ + byte[] compressedLines = outBytes.toByteArray(); + writeInt4(compressedLines.length); + startPos = bytePos; + writeString("IDAT"); + writeBytes(compressedLines); + writeCRC(); + writeEnd(); + deflater.finish(); + return true; + } catch (IOException e) { + System.err.println(e.toString()); + return false; + } + } + + /** + * Write a PNG "IEND" chunk into the pngBytes array. + */ + private void writeEnd() { + writeInt4(0); + startPos = bytePos; + writeString("IEND"); + writeCRC(); + } + + ///** + //* Perform "sub" filtering on the given row. + //* Uses temporary array leftBytes to store the original values + //* of the previous pixels. The array is 16 bytes long, which + //* will easily hold two-byte samples plus two-byte alpha. + //* + //*/ + //private void filterSub() { + // int offset = bytesPerPixel; + // int actualStart = startPos + offset; + // int leftInsert = offset; + // int leftExtract = 0; + // //byte current_byte; + // + // for (int i = actualStart; i < startPos + byteWidth; i++) { + // leftBytes[leftInsert] = scanLines[i]; + // scanLines[i] = (byte) ((scanLines[i] - leftBytes[leftExtract]) % 256); + // leftInsert = (leftInsert + 1) % 0x0f; + // leftExtract = (leftExtract + 1) % 0x0f; + // } + //} + // + ///** + //* Perform "up" filtering on the given row. Side effect: refills the prior row + //* with current row + //* + //*/ + //private void filterUp() { + // int nBytes = width * bytesPerPixel; + // for (int i = 0; i < nBytes; i++) { + // int pt = startPos + i; + // byte b = scanLines[pt]; + // scanLines[pt] = (byte) ((scanLines[pt] - priorRow[i]) % 256); + // priorRow[i] = b; + // } + //} + +} diff --git a/src/javajs/img/PpmEncoder.java b/src/javajs/img/PpmEncoder.java new file mode 100644 index 0000000..4bdc980 --- /dev/null +++ b/src/javajs/img/PpmEncoder.java @@ -0,0 +1,60 @@ +// PpmEncoder - write out an image as a PPM +// +// Copyright (C)1996,1998 by Jef Poskanzer . All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +// SUCH DAMAGE. +// +// Visit the ACME Labs Java page for up-to-date versions of this and other +// fine Java utilities: http://www.acme.com/java/ + +package javajs.img; + +import java.util.Map; + + +/** + * see http://netpbm.sourceforge.net/doc/ppm.html + */ +public class PpmEncoder extends ImageEncoder { + + @Override + protected void setParams(Map params) { + // no params + } + + @Override + protected void generate() { + putString("P6\n"); + putString(width + " " + height + "\n"); + putString("255\n"); + byte[] ppmPixels = new byte[width * 3]; + for (int pt = 0, row = 0; row < height; ++row) { + for (int col = 0, j = 0; col < width; ++col, pt++) { + int p = pixels[pt]; + ppmPixels[j++] = (byte) ((p >> 16) & 0xff); + ppmPixels[j++] = (byte) ((p >> 8) & 0xff); + ppmPixels[j++] = (byte) (p & 0xff); + } + out.write(ppmPixels, 0, ppmPixels.length); + } + } +} diff --git a/src/javajs/util/A4.java b/src/javajs/util/A4.java new file mode 100644 index 0000000..df173a6 --- /dev/null +++ b/src/javajs/util/A4.java @@ -0,0 +1,258 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + +import java.io.Serializable; + +import javajs.api.JSONEncodable; +import javajs.util.T3; + + + +/** + * A 4 element axis angle represented by single precision floating point + * x,y,z,angle components. An axis angle is a rotation of angle (radians) about + * the vector (x,y,z). + * + * @version specification 1.1, implementation $Revision: 1.9 $, $Date: + * 2006/07/28 17:01:32 $ + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 + * for unique constructor and method names + * for the optimization of compiled JavaScript using Java2Script. + * + */ +public class A4 implements JSONEncodable, Serializable { + + /* + * I assumed that the length of the axis vector is not significant. + */ + + /** + * + */ + private static final long serialVersionUID = 1L; + +/** + * The x coordinate. + */ + public float x; + + /** + * The y coordinate. + */ + public float y; + + /** + * The z coordinate. + */ + public float z; + + /** + * The angle. + */ + public float angle; + + /** + * Constructs and initializes a AxisAngle4f to (0,0,1,0). + */ + public A4() { + z = 1.0f; + } + + /** + * Constructs and initializes an AxisAngle4f from the specified x, y, z, and + * angle. + * + * @param x + * the x coordinate + * @param y + * the y coordinate + * @param z + * the z coordinate + * @param angle + * the angle. + * @return a + */ + public static A4 new4(float x, float y, float z, float angle) { + A4 a = new A4(); + a.set4(x, y, z, angle); + return a; + } + + /** + * Constructs and initializes a AxisAngle4f from the specified AxisAngle4f. + * + * @param a1 + * the AxisAngle4f containing the initialization x y z angle data + * @return a + */ + public static A4 newAA(A4 a1) { + A4 a = new A4(); + a.set4(a1.x, a1.y, a1.z, a1.angle); + return a; + } + + /** + * Constructs and initializes an AxisAngle4f from the specified axis and + * angle. + * + * @param axis + * the axis + * @param angle + * the angle + * @return a + */ + public static A4 newVA(V3 axis, float angle) { + A4 a = new A4(); + a.setVA(axis, angle); + return a; + } + + /** + * Sets the value of this AxisAngle4f to the specified axis and angle. + * + * @param axis + * the axis + * @param angle + * the angle + * @since Java 3D 1.2 + */ + public final void setVA(V3 axis, float angle) { + x = axis.x; + y = axis.y; + z = axis.z; + this.angle = angle; + } + + /** + * Sets the value of this axis angle to the specified x,y,z,angle. + * + * @param x + * the x coordinate + * @param y + * the y coordinate + * @param z + * the z coordinate + * @param angle + * the angle + */ + public final void set4(float x, float y, float z, float angle) { + this.x = x; + this.y = y; + this.z = z; + this.angle = angle; + } + + /** + * Sets the value of this axis angle to the value of axis angle t1. + * + * @param a + * the axis angle to be copied + */ + public final void setAA(A4 a) { + x = a.x; + y = a.y; + z = a.z; + angle = a.angle; + } + + + /** + * Sets the value of this axis-angle to the rotational component of the passed + * matrix. + * + * @param m1 + * the matrix3f + */ + public final void setM(M3 m1) { + setFromMat(m1.m00, m1.m01, m1.m02, m1.m10, m1.m11, m1.m12, m1.m20, m1.m21, + m1.m22); + } + + // helper method + private void setFromMat(double m00, double m01, double m02, double m10, + double m11, double m12, double m20, double m21, + double m22) { + // assuming M is normalized. + + double cos = (m00 + m11 + m22 - 1.0) * 0.5; + x = (float) (m21 - m12); + y = (float) (m02 - m20); + z = (float) (m10 - m01); + double sin = 0.5 * Math.sqrt(x * x + y * y + z * z); + if (sin == 0 && cos == 1) { + x = y = 0; + z = 1; + angle = 0; + } else { + angle = (float) Math.atan2(sin, cos); + } + + // no need to normalize + // x /= n; + // y /= n; + // z /= n; + } + + /** + * Returns a hash number based on the data values in this object. Two + * different AxisAngle4f objects with identical data values (ie, returns true + * for equals(AxisAngle4f) ) will return the same hash number. Two vectors + * with different data members may return the same hash value, although this + * is not likely. + */ + @Override + public int hashCode() { + return T3.floatToIntBits(x) ^ T3.floatToIntBits(y) + ^ T3.floatToIntBits(z) ^ T3.floatToIntBits(angle); + } + + /** + * Returns true if the Object o is of type AxisAngle4f and all of the data + * members of o1 are equal to the corresponding data members in this + * AxisAngle4f. + * + * @param o + * the object with which the comparison is made. + * @return T/F + */ + @Override + public boolean equals(Object o) { + if (!(o instanceof A4)) + return false; + A4 a1 = (A4) o; + return x == a1.x && y == a1.y && z == a1.z && angle == a1.angle; + } + + /** + * Returns a string that contains the values of this AxisAngle4f. The form is + * (x,y,z,angle). + * + * @return the String representation + */ + @Override + public String toString() { + return "(" + x + ", " + y + ", " + z + ", " + angle + ")"; + } + + @Override + public String toJSON() { + return "[" + x + "," + y + "," + z + "," + (float) (angle * 180.0 / Math.PI) + "]"; + } +} diff --git a/src/javajs/util/AU.java b/src/javajs/util/AU.java new file mode 100644 index 0000000..cd4ed03 --- /dev/null +++ b/src/javajs/util/AU.java @@ -0,0 +1,551 @@ +/* $RCSfile$ + * $Author: egonw $ + * $Date: 2005-11-10 09:52:44 -0600 (Thu, 10 Nov 2005) $ + * $Revision: 4255 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2003-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.util; + +// 4/23/15 BH getComponentType fix + +import java.lang.reflect.Array; + +import java.util.Arrays; +import java.util.Hashtable; +import java.util.Map; + + +final public class AU { + + /** + * Very important that this not be used with Int32Array or Float32Array, + * because it is not initialize to all zeros in MSIE 9. + * + * @param array + * @param minimumLength + * @return array + */ + public static Object ensureLength(Object array, int minimumLength) { + return (array != null && getLength(array) >= minimumLength ? array + : arrayCopyObject(array, minimumLength)); + } + + public static String[] ensureLengthS(String[] array, int minimumLength) { + return (array != null && array.length >= minimumLength ? array + : arrayCopyS(array, minimumLength)); + } + + public static float[] ensureLengthA(float[] array, int minimumLength) { + return (array != null && array.length >= minimumLength ? array: arrayCopyF(array, minimumLength)); + } + + public static int[] ensureLengthI(int[] array, int minimumLength) { + return (array != null && array.length >= minimumLength ? array : arrayCopyI(array, minimumLength)); + } + + public static short[] ensureLengthShort(short[] array, int minimumLength) { + return (array != null && array.length >= minimumLength ? array : arrayCopyShort(array, minimumLength)); + } + + public static byte[] ensureLengthByte(byte[] array, int minimumLength) { + return (array != null && array.length >= minimumLength ? array : arrayCopyByte(array, minimumLength)); + } + + /** + * Very important that this not be used with Int32Array or Float32Array, + * because it is not initialized to all zeros in MSIE 9. + * + * @param array + * @return array + */ + public static Object doubleLength(Object array) { + return arrayCopyObject(array, (array == null ? 16 : 2 * getLength(array))); + } + + public static String[] doubleLengthS(String[] array) { + return arrayCopyS(array, (array == null ? 16 : 2 * array.length)); + } + + public static float[] doubleLengthF(float[] array) { + return arrayCopyF(array, (array == null ? 16 : 2 * array.length)); + } + + public static int[] doubleLengthI(int[] array) { + return arrayCopyI(array, (array == null ? 16 : 2 * array.length)); + } + + public static short[] doubleLengthShort(short[] array) { + return arrayCopyShort(array, (array == null ? 16 : 2 * array.length)); + } + + public static byte[] doubleLengthByte(byte[] array) { + return arrayCopyByte(array, (array == null ? 16 : 2 * array.length)); + } + + public static boolean[] doubleLengthBool(boolean[] array) { + return arrayCopyBool(array, (array == null ? 16 : 2 * array.length)); + } + + public static Object deleteElements(Object array, int firstElement, + int nElements) { + if (nElements == 0 || array == null) + return array; + int oldLength = getLength(array); + if (firstElement >= oldLength) + return array; + int n = oldLength - (firstElement + nElements); + if (n < 0) + n = 0; + Object t = newInstanceO(array, firstElement + n); + if (firstElement > 0) + System.arraycopy(array, 0, t, 0, firstElement); + if (n > 0) + System.arraycopy(array, firstElement + nElements, t, firstElement, n); + return t; + } + + /** + * note -- cannot copy if array is null! does not copy if length is unchanged + * + * @param array + * @param newLength + * @return array + */ + public static Object arrayCopyObject(Object array, int newLength) { + int oldLength = (array == null ? -1 : getLength(array)); + if (newLength < 0) newLength = oldLength; + if (newLength == oldLength) + return array; + /** + * @j2sNative + * + * if (Clazz.newArray$ && newLength < oldLength) return Clazz.newArray$(-1, array, 0, newLength); + */ + {} + Object t = newInstanceO(array, newLength); + if (oldLength > 0) + System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength + : newLength); + return t; + + } + + /** + * Very important that this not be used with Int32Array or Float32Array, + * because those need to be initialized to all zeros in MSIE 9, and + * MSIE 9 cannot distinguish Int32Array or Float32Array from Array. + * + * @param array + * @param n + * @return array + */ + private static Object newInstanceO(Object array, int n) { + return Array.newInstance(array.getClass().getComponentType(), n); + } + + public static int getLength(Object array) { + /** + * @j2sNative + * + * return array.length + * + */ + { + return Array.getLength(array); + } + } + + public static String[] arrayCopyS(String[] array, int newLength) { + int oldLength = (array == null ? -1 : array.length); + if (newLength < 0) newLength = oldLength; + /** + * @j2sNative + * + * if (Clazz.newArray$ && newLength < oldLength) return Clazz.newArray$(-1, array, 0, newLength); + */ + {} + String[] t = new String[newLength]; + if (array != null) { + System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength + : newLength); + } + return t; + } + + public static int[][] arrayCopyII(int[][] array, int newLength) { + int[][] t = newInt2(newLength); + if (array != null) { + int oldLength = array.length; + System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength + : newLength); + } + return t; + } + + public static T3[] arrayCopyPt(T3[] array, int newLength) { + if (newLength < 0) + newLength = array.length; + T3[] t = new T3[newLength]; + if (array != null) { + int oldLength = array.length; + System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength + : newLength); + } + return t; + } + + public static float[] arrayCopyF(float[] array, int newLength) { + int oldLength = (array == null ? -1 : array.length); + if (newLength < 0) newLength = oldLength; + /** + * @j2sNative + * + * if (Clazz.newArray$ && newLength < oldLength) return Clazz.newArray$(-1, array, 0, newLength); + */ + {} + float[] t = new float[newLength]; + if (array != null) { + System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength + : newLength); + } + return t; + } + + public static int[] arrayCopyI(int[] array, int newLength) { + int oldLength = (array == null ? -1 : array.length); + if (newLength < 0) newLength = oldLength; + /** + * @j2sNative + * + * if (Clazz.newArray$ && newLength < oldLength) return Clazz.newArray$(-1, array, 0, newLength); + */ + {} + int[] t = new int[newLength]; + if (array != null) { + System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength + : newLength); + } + return t; + } + + /** + * a specialized method that allows copying from a starting point either + * to the end or to the middle (color schemes, especially) + * @param array + * @param i0 + * @param n + * @return array or null + */ + public static int[] arrayCopyRangeI(int[] array, int i0, int n) { + if (array == null) + return null; + int oldLength = array.length; + if (n == -1) n = oldLength; + if (n == -2) n = oldLength / 2; + /** + * @j2sNative + * + * if (Clazz.newArray$) return Clazz.newArray$(-1, array, i0, n); + * + */ + { + } + n -= i0; + int[] t = new int[n]; + System.arraycopy(array, i0, t, 0, n); + return t; + } + + public static int[] arrayCopyRangeRevI(int[] array, int i0, int n) { + if (array == null) + return null; + /** + * @j2sNative + * + * if (Clazz.newArray$) return Clazz.newArray$(-1, array, i0, n).reverse(); + */ + { + } + int[] t = arrayCopyRangeI(array, i0, n); + if (n < 0) + n = array.length; + for (int i = n / 2; --i >= 0;) + swapInt(t, i, n - 1 - i); + return t; + } + + public static short[] arrayCopyShort(short[] array, int newLength) { + int oldLength = (array == null ? -1 : array.length); + if (newLength < 0) newLength = oldLength; + /** + * @j2sNative + * + * if (Clazz.newArray$ && newLength < oldLength) return Clazz.newArray$(-1, array, 0, newLength); + */ + {} + short[] t = new short[newLength]; + if (array != null) { + System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength + : newLength); + } + return t; + } + + public static byte[] arrayCopyByte(byte[] array, int newLength) { + int oldLength = (array == null ? -1 : array.length); + if (newLength < 0) newLength = oldLength; + /** + * @j2sNative + * + * if (Clazz.newArray$ && newLength < oldLength) return Clazz.newArray$(-1, array, 0, newLength); + */ + {} + byte[] t = new byte[newLength]; + if (array != null) { + System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength + : newLength); + } + return t; + } + + public static boolean[] arrayCopyBool(boolean[] array, int newLength) { + int oldLength = (array == null ? -1 : array.length); + if (newLength < 0) newLength = oldLength; + /** + * @j2sNative + * + * if (Clazz.newArray$ && newLength < oldLength) return Clazz.newArray$(-1, array, 0, newLength); + */ + {} + boolean[] t = new boolean[newLength]; + if (array != null) { + System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength + : newLength); + } + return t; + } + + public static void swapInt(int[] array, int indexA, int indexB) { + int t = array[indexA]; + array[indexA] = array[indexB]; + array[indexB] = t; + } + + /* + public static void swap(short[] array, int indexA, int indexB) { + short t = array[indexA]; + array[indexA] = array[indexB]; + array[indexB] = t; + } + + public static void swap(float[] array, int indexA, int indexB) { + float t = array[indexA]; + array[indexA] = array[indexB]; + array[indexB] = t; + } + */ + + public static String dumpArray(String msg, float[][] A, int x1, int x2, int y1, int y2) { + String s = "dumpArray: " + msg + "\n"; + for (int x = x1; x <= x2; x++) + s += "\t*" + x + "*"; + for (int y = y2; y >= y1; y--) { + s += "\n*" + y + "*"; + for (int x = x1; x <= x2; x++) + s += "\t" + (x < A.length && y < A[x].length ? A[x][y] : Float.NaN); + } + return s; + } + + public static String dumpIntArray(int[] A, int n) { + String str = ""; + for (int i = 0; i < n; i++) + str += " " + A[i]; + return str; + } + + public static String sortedItem(Lst v, int n) { + if (v.size() == 0) + return null; + if (v.size() == 1) + return v.get(0); + String[] keys = v.toArray(new String[v.size()]); + Arrays.sort(keys); + return keys[n % keys.length]; + } + + /** + * Helper method for creating a List[] without warnings. + * + * @param Type of objects in the list. + * @param size Array size. + * @return Array of List + */ + @SuppressWarnings("unchecked") + public static Lst[] createArrayOfArrayList(int size) { + return new Lst[size]; + } + + /** + * Helper method for creating a Map[] without warnings. + * + * @param Type of object for the keys in the map. + * @param Type of object for the values in the map. + * @param size Array size. + * @return Array of Map + */ + @SuppressWarnings("unchecked") + public static Map[] createArrayOfHashtable(int size) { + return new Hashtable[size]; + } + + public static void swap(Object[] o, int i, int j) { + Object oi = o[i]; + o[i] = o[j]; + o[j] = oi; + } + + public static float[][] newFloat2(int n) { + return new float[n][]; + } + + public static boolean[][] newBool2(int n) { + return new boolean[n][]; + } + + public static int[][] newInt2(int n) { + return new int[n][]; + } + + public static int[][][] newInt3(int nx, int ny) { + return (ny < 0 ? new int[nx][][] : new int[nx][ny][]); + } + + public static float[][][] newFloat3(int nx, int ny) { + return (ny < 0 ? new float[nx][][] : new float[nx][ny][]); + } + + public static int[][][][] newInt4(int n) { + return new int[n][][][]; + } + + public static short[][] newShort2(int n) { + return new short[n][]; + } + + public static byte[][] newByte2(int n) { + return new byte[n][]; + } + + public static double[][] newDouble2(int n) { + return new double[n][]; + } + + /** + * remove all keys from a map that start with given root + * @param map + * @param root + * @return number removed + */ + public static int removeMapKeys(Map map, String root) { + Lst list = new Lst(); + for (String key: map.keySet()) + if (key.startsWith(root)) + list.addLast(key); + for (int i = list.size(); --i >= 0;) + map.remove(list.get(i)); + return list.size(); + } + + public static boolean isAS(Object x) { + return x instanceof String[]; + } + + public static boolean isASS(Object x) { + return x instanceof String[][]; + } + + public static boolean isAP(Object x) { + return x instanceof T3[]; + } + + public static boolean isAF(Object x) { + return x instanceof float[]; + } + + public static boolean isAFloat(Object x) { + return x instanceof Float[]; + } + + public static boolean isAD(Object x) { + return x instanceof double[]; + } + + public static boolean isADD(Object x) { + return x instanceof double[][]; + } + + public static boolean isAB(Object x) { + return x instanceof byte[]; + } + + public static boolean isAI(Object x) { + return x instanceof int[]; + } + + public static boolean isAII(Object x) { + return (x instanceof int[][]); + } + + public static boolean isAFF(Object x) { + return x instanceof float[][]; + } + + public static boolean isAFFF(Object x) { + return x instanceof float[][][]; + } + + /** + * Ensure that we have signed and not unsigned bytes coming out of any + * process, but particularly out of file reading. + * + * @param b + * @return b + */ + public static byte[] ensureSignedBytes(byte[] b) { + if (b != null) { + /** + * @j2sNative + * + * for (var i = b.length; --i >= 0;) { var j = b[i] & + * 0xFF; if (j >= 0x80) j -= 0x100; b[i] = j; } + * + */ + { + } + } + return b; + } + + +} diff --git a/src/javajs/util/AjaxURLConnection.java b/src/javajs/util/AjaxURLConnection.java new file mode 100644 index 0000000..cef98c8 --- /dev/null +++ b/src/javajs/util/AjaxURLConnection.java @@ -0,0 +1,122 @@ +package javajs.util; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import javajs.api.ResettableStream; +import javajs.api.js.J2SObjectInterface; + +/** + * + * A method to allow a JavaScript Ajax + * + */ +public class AjaxURLConnection extends URLConnection { + + protected AjaxURLConnection(URL url) { + super(url); + } + + byte[] bytesOut; + String postOut = ""; + + /** + * + * doAjax() is where the synchronous call to AJAX is to happen. or at least + * where we wait for the asynchronous call to return. This method should fill + * the dataIn field with either a string or byte array, or null if you want to + * throw an error. + * + * url, bytesOut, and postOut are all available for use + * + * the method is "private", but in JavaScript that can still be overloaded. + * Just set something to org.jmol.awtjs.JmolURLConnection.prototype.doAjax + * + * @return file data as a javajs.util.SB or byte[] depending upon the file + * type. + * + * + */ + @SuppressWarnings("null") + private Object doAjax(boolean isBinary) { + J2SObjectInterface jmol = null; + /** + * @j2sNative + * + * jmol = J2S || Jmol; + * + */ + { + } + return jmol._doAjax(url, postOut, bytesOut, isBinary); + } + + @Override + public void connect() throws IOException { + // not expected to be used. + } + + public void outputBytes(byte[] bytes) { + // type = "application/octet-stream;"; + bytesOut = bytes; + } + + public void outputString(String post) { + postOut = post; + // type = "application/x-www-form-urlencoded"; + } + + @Override + public InputStream getInputStream() { + BufferedInputStream bis = getAttachedStreamData(url); + if (bis != null) + return bis; + Object o = doAjax(true); + return ( + AU.isAB(o) ? Rdr.getBIS((byte[]) o) + : o instanceof SB ? Rdr.getBIS(Rdr.getBytesFromSB((SB) o)) + : o instanceof String ? Rdr.getBIS(((String) o).getBytes()) + : bis + ); + } + @SuppressWarnings("unused") + /** + * J2S will attach a BufferedInputStream to any URL that is + * retrieved using a ClassLoader. This improves performance by + * not going back to the server every time a second time, since + * the first time in Java is usually just to see if it exists. + * + * This stream can be re-used, but it has to be reset. Java for some + * reason does not allow a BufferedInputStream to fully reset its + * inner streams. We enable that by force-casting the stream as a + * javax.io stream and then applying resetStream() to that. + * + * + * @param url + * @return + */ + public static BufferedInputStream getAttachedStreamData(URL url) { + BufferedInputStream bis = null; + /** + * @j2sNative + * + * bis = url._streamData; + */ + { + } + if (bis != null) + ((ResettableStream) (Object) bis).resetStream(); + return bis; + } + + /** + * @return javajs.util.SB or byte[], depending upon the file type + */ + public Object getContents() { + return doAjax(false); + } + +} diff --git a/src/javajs/util/AjaxURLStreamHandler.java b/src/javajs/util/AjaxURLStreamHandler.java new file mode 100644 index 0000000..3d6d4d7 --- /dev/null +++ b/src/javajs/util/AjaxURLStreamHandler.java @@ -0,0 +1,53 @@ +package javajs.util; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + + + +/** + * + * A method to allow a JavaScript AJAX adapter to + * deliver web content to JSmol. This handler is just a formality. + * + */ +public class AjaxURLStreamHandler extends URLStreamHandler { + + String protocol; + + public AjaxURLStreamHandler(String protocol) { + this.protocol = protocol; + } + + @Override + protected URLConnection openConnection(URL url) throws IOException { + return new AjaxURLConnection(url); + } + + + @Override + protected String toExternalForm(URL u) { + SB result = new SB(); + result.append(u.getProtocol()); + result.append(":"); + if (u.getAuthority() != null && u.getAuthority().length() > 0) { + result.append("//"); + result.append(u.getAuthority()); + } + if (u.getPath() != null) { + result.append(u.getPath()); + } + if (u.getQuery() != null) { + result.append("?"); + result.append(u.getQuery()); + } + if (u.getRef() != null) { + result.append("#"); + result.append(u.getRef()); + } + return result.toString(); + } + +} diff --git a/src/javajs/util/AjaxURLStreamHandlerFactory.java b/src/javajs/util/AjaxURLStreamHandlerFactory.java new file mode 100644 index 0000000..ef903a2 --- /dev/null +++ b/src/javajs/util/AjaxURLStreamHandlerFactory.java @@ -0,0 +1,31 @@ +package javajs.util; + +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; +import java.util.Hashtable; +import java.util.Map; + + +/** + * + * For handling URL file IO via AJAX in JavaScript version + * + */ + +public class AjaxURLStreamHandlerFactory implements URLStreamHandlerFactory { + + Map htFactories = new Hashtable(); + + public AjaxURLStreamHandlerFactory() { + // for reflection; + } + + @Override + public URLStreamHandler createURLStreamHandler(String protocol) { + AjaxURLStreamHandler fac = htFactories.get(protocol); + if (fac == null) + htFactories.put(protocol, fac = new AjaxURLStreamHandler(protocol)); + return (fac.protocol == null ? null : fac); + } + +} diff --git a/src/javajs/util/ArrayDataReader.java b/src/javajs/util/ArrayDataReader.java new file mode 100644 index 0000000..737a6ab --- /dev/null +++ b/src/javajs/util/ArrayDataReader.java @@ -0,0 +1,57 @@ +package javajs.util; + +import java.io.IOException; + + + + + + +/** + * + * ArrayDataReader subclasses BufferedReader and overrides its + * read, readLine, mark, and reset methods so that JmolAdapter + * works with String[] arrays without any further adaptation. + * + */ + +public class ArrayDataReader extends DataReader { + private String[] data; + private int pt; + private int len; + + public ArrayDataReader() { + super(); + } + + @Override + public DataReader setData(Object data) { + this.data = (String[]) data; + len = this.data.length; + return this; + } + + @Override + public int read(char[] buf, int off, int len) throws IOException { + return readBuf(buf, off, len); + } + + @Override + public String readLine() { + return (pt < len ? data[pt++] : null); + } + + /** + * + * @param ptr + */ + public void mark(long ptr) { + //ignore ptr. + ptMark = pt; + } + + @Override + public void reset() { + pt = ptMark; + } +} \ No newline at end of file diff --git a/src/javajs/util/BArray.java b/src/javajs/util/BArray.java new file mode 100644 index 0000000..f09d272 --- /dev/null +++ b/src/javajs/util/BArray.java @@ -0,0 +1,33 @@ +package javajs.util; + +public class BArray { + public byte[] data; + + public BArray(byte[] data) { + this.data = data; + } + + @Override + public boolean equals(Object o) { + if (o instanceof BArray) { + byte[] d = ((BArray) o).data; + if (d.length == data.length){ + for (int i = 0; i < d.length; i++) + if (d[i] != data[i]) + return false; + return true; + } + } + return false; + } + + @Override + public int hashCode() { + return data.hashCode(); + } + + @Override + public String toString() { + return new String(data); + } +} diff --git a/src/javajs/util/BC.java b/src/javajs/util/BC.java new file mode 100644 index 0000000..df9c9f9 --- /dev/null +++ b/src/javajs/util/BC.java @@ -0,0 +1,187 @@ +package javajs.util; + +/** + * byte converter + * + * + * @author Bob Hanson hansonr@stolaf.edu + * + */ +public class BC { + + public BC() { + // unnecessary to instantialize unless subclassed + } + + public static float bytesToFloat(byte[] bytes, int j, boolean isBigEndian) throws Exception { + return intToFloat(bytesToInt(bytes, j, isBigEndian)); + } + + public static int bytesToShort(byte[] bytes, int j, boolean isBigEndian) { + int n = (isBigEndian ? (bytes[j + 1] & 0xff) | (bytes[j] & 0xff) << 8 + : (bytes[j++] & 0xff) | (bytes[j++] & 0xff) << 8); + return (n > 0x7FFF ? n - 0x10000 : n); + } + + public static int bytesToInt(byte[] bytes, int j, boolean isBigEndian) { + int n = (isBigEndian ? (bytes[j + 3] & 0xff) | (bytes[j + 2] & 0xff) << 8 + | (bytes[j + 1] & 0xff) << 16 | (bytes[j] & 0xff) << 24 + : (bytes[j++] & 0xff) | (bytes[j++] & 0xff) << 8 + | (bytes[j++] & 0xff) << 16 | (bytes[j++] & 0xff) << 24); + /** + * @j2sNative + * + * return (n > 0x7FFFFFFF ? n - 0x100000000 : n); + * + */ + { + return n; + } + } + + public static int intToSignedInt(int n) { + /** + * @j2sNative + * + * return (n > 0x7FFFFFFF ? n - 0x100000000 : n); + * + */ + { + return n; + } + } + public static float intToFloat(int x) throws Exception { + /** + * see http://en.wikipedia.org/wiki/Binary32 + * + * [sign] [8 bits power] [23 bits fraction] + * 0x80000000 0x7F800000 0x7FFFFF + * + * (untested) + * + * @j2sNative + * + * if (x == 0) return 0; + * var o = javajs.util.BC; + * if (o.fracIEEE == null) + * o.setFracIEEE(); + * var m = ((x & 0x7F800000) >> 23); + * return ((x & 0x80000000) == 0 ? 1 : -1) * o.shiftIEEE((x & 0x7FFFFF) | 0x800000, m - 149); + * + */ + { + return Float.intBitsToFloat(x); + } + } + + /** + * see http://en.wikipedia.org/wiki/Binary64 + * + * not concerning ourselves with very small or very large numbers and getting + * this exactly right. Just need a float here. + * + * @param bytes + * @param j + * @param isBigEndian + * @return float + */ + public static float bytesToDoubleToFloat(byte[] bytes, int j, boolean isBigEndian) { + { + // IEEE754: sign (1 bit), exponent (11 bits), fraction (52 bits). + // seeeeeee eeeeffff ffffffff ffffffff ffffffff xxxxxxxx xxxxxxxx xxxxxxxx + // b1 b2 b3 b4 b5 ---------float ignores---- + + if (fracIEEE == null) + setFracIEEE(); + + /** + * @j2sNative + * var o = javajs.util.BC; + * var b1, b2, b3, b4, b5; + * + * if (isBigEndian) { + * b1 = bytes[j] & 0xFF; + * b2 = bytes[j + 1] & 0xFF; + * b3 = bytes[j + 2] & 0xFF; + * b4 = bytes[j + 3] & 0xFF; + * b5 = bytes[j + 4] & 0xFF; + * } else { + * b1 = bytes[j + 7] & 0xFF; + * b2 = bytes[j + 6] & 0xFF; + * b3 = bytes[j + 5] & 0xFF; + * b4 = bytes[j + 4] & 0xFF; + * b5 = bytes[j + 3] & 0xFF; + * } + * var s = ((b1 & 0x80) == 0 ? 1 : -1); + * var e = (((b1 & 0x7F) << 4) | (b2 >> 4)) - 1026; + * b2 = (b2 & 0xF) | 0x10; + * return s * (o.shiftIEEE(b2, e) + o.shiftIEEE(b3, e - 8) + o.shiftIEEE(b4, e - 16) + * + o.shiftIEEE(b5, e - 24)); + */ + { + double d; + + if (isBigEndian) + d = Double.longBitsToDouble((((long) bytes[j]) & 0xff) << 56 + | (((long) bytes[j + 1]) & 0xff) << 48 + | (((long) bytes[j + 2]) & 0xff) << 40 + | (((long) bytes[j + 3]) & 0xff) << 32 + | (((long) bytes[j + 4]) & 0xff) << 24 + | (((long) bytes[j + 5]) & 0xff) << 16 + | (((long) bytes[j + 6]) & 0xff) << 8 + | (((long) bytes[7]) & 0xff)); + else + d = Double.longBitsToDouble((((long) bytes[j + 7]) & 0xff) << 56 + | (((long) bytes[j + 6]) & 0xff) << 48 + | (((long) bytes[j + 5]) & 0xff) << 40 + | (((long) bytes[j + 4]) & 0xff) << 32 + | (((long) bytes[j + 3]) & 0xff) << 24 + | (((long) bytes[j + 2]) & 0xff) << 16 + | (((long) bytes[j + 1]) & 0xff) << 8 + | (((long) bytes[j]) & 0xff)); + return (float) d; + } + + } + } + + private static float[] fracIEEE; + + private static void setFracIEEE() { + fracIEEE = new float[270]; + for (int i = 0; i < 270; i++) + fracIEEE[i] = (float) Math.pow(2, i - 141); + // System.out.println(fracIEEE[0] + " " + Parser.FLOAT_MIN_SAFE); + // System.out.println(fracIEEE[269] + " " + Float.MAX_VALUE); + } + + /** + * only concerned about reasonable float values here -- private but not designated; called by JavaScript + * + * @param f + * @param i + * @return f * 2^i + */ + static double shiftIEEE(double f, int i) { + if (f == 0 || i < -140) + return 0; + if (i > 128) + return Float.MAX_VALUE; + return f * fracIEEE[i + 140]; + } + +// static { +// setFracIEEE(); +// for (int i = -50; i < 50; i++) { +// float f = i * (float) (Math.random() * Math.pow(2, Math.random() * 100 - 50)); +// int x = Float.floatToIntBits(f); +// int m = ((x & 0x7F800000) >> 23); +// float f1 = (float) (f == 0 ? 0 : ((x & 0x80000000) == 0 ? 1 : -1) * shiftIEEE((x & 0x7FFFFF) | 0x800000, m - 149)); +// System.out.println(f + " " + f1); +// } +// System.out.println("binarydo"); +// } + + + +} diff --git a/src/javajs/util/BS.java b/src/javajs/util/BS.java new file mode 100644 index 0000000..0bfa5a6 --- /dev/null +++ b/src/javajs/util/BS.java @@ -0,0 +1,960 @@ +/* + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright 1995-2007 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package javajs.util; + +import javajs.api.JSONEncodable; + + + +/** + * + * a fast 32-bit BitSet optimized for Java2Script -- about 25 times faster than + * java.util.BitSet + * + * @author Bob Hanson hansonr@stolaf.edu + * + * Additions by Bob Hanson to allow for JavaScript mix of int/long Note + * that Firefox (Sept 2012) does not really treat "Int32Array" as such, + * because any element can be pushed into being a 64-bit number, which + * really isn't because the last 8 bits are not usable. + * + * This class implements a vector of bits that grows as needed. Each + * component of the bit set has a {@code boolean} value. The bits of a + * {@code BitSet} are indexed by nonnegative integers. Individual + * indexed bits can be examined, set, or cleared. One {@code BitSet} may + * be used to modify the contents of another {@code BitSet} through + * logical AND, logical inclusive OR, and logical exclusive OR + * operations. + * + *

+ * By default, all bits in the set initially have the value {@code + * false}. + * + *

+ * Every bit set has a current size, which is the number of bits of + * space currently in use by the bit set. Note that the size is related + * to the implementation of a bit set, so it may change with + * implementation. The length of a bit set relates to logical length of + * a bit set and is defined independently of implementation. + * + *

+ * Unless otherwise noted, passing a null parameter to any of the + * methods in a {@code BitSet} will result in a {@code + * NullPointerException}. + * + *

+ * A {@code BitSet} is not safe for multithreaded use without external + * synchronization. + * + * @author Arthur van Hoff + * @author Michael McCloskey + * @author Martin Buchholz + * @since JDK1.0 + */ +public class BS implements Cloneable, JSONEncodable { + /* + * BitSets are packed into arrays of "wrangeords." + * + * An int, which consists of 32 bits, requiring 5 address bits, is used for + * the JavaScript port. + */ + private final static int ADDRESS_BITS_PER_WORD = 5; + private final static int BITS_PER_WORD = 1 << ADDRESS_BITS_PER_WORD; + protected final static int BIT_INDEX_MASK = BITS_PER_WORD - 1; + + /* Used to shift left or right for a partial word mask */ + protected static final int WORD_MASK = 0xffffffff; + + + /** + * The internal field corresponding to the serialField "bits". + */ + protected int[] words; + + /** + * The number of words in the logical size of this BitSet. + */ + protected transient int wordsInUse = 0; + + /** + * Whether the size of "words" is user-specified. If so, we assume the user + * knows what he's doing and try harder to preserve it. + */ + private transient boolean sizeIsSticky = false; + + /* use serialVersionUID from JDK 1.0.2 for interoperability */ + //private static final long serialVersionUID = 7997698588986878753L; + + /** + * Given a bit index, return word index containing it. + * @param bitIndex + * @return b + */ + protected static int wordIndex(int bitIndex) { + return bitIndex >> ADDRESS_BITS_PER_WORD; + } + + /** + * Sets the field wordsInUse to the logical size in words of the bit set. + * WARNING:This method assumes that the number of words actually in use is + * less than or equal to the current value of wordsInUse! + */ + protected void recalculateWordsInUse() { + // Traverse the bitset until a used word is found + int i; + for (i = wordsInUse - 1; i >= 0; i--) + if (words[i] != 0) + break; + + wordsInUse = i + 1; // The new logical size + } + + /** + * Creates a new bit set. All bits are initially {@code false}. + */ + public BS() { + initWords(BITS_PER_WORD); + sizeIsSticky = false; + } + + /** + * Creates a bit set whose initial size is large enough to explicitly + * represent bits with indices in the range {@code 0} through {@code nbits-1}. + * All bits are initially {@code false}. + * + * @param nbits + * the initial size of the bit set + * @return bs + * @throws NegativeArraySizeException + * if the specified initial size is negative + */ + public static BS newN(int nbits) { + BS bs = new BS(); + bs.init(nbits); + return bs; + } + + protected void init(int nbits) { + // nbits can't be negative; size 0 is OK + if (nbits < 0) + throw new NegativeArraySizeException("nbits < 0: " + nbits); + initWords(nbits); + sizeIsSticky = true; + } + + private void initWords(int nbits) { + words = new int[wordIndex(nbits - 1) + 1]; + } + + /** + * Ensures that the BitSet can hold enough words. + * + * @param wordsRequired + * the minimum acceptable number of words. + */ + private void ensureCapacity(int wordsRequired) { + if (words.length < wordsRequired) { + // Allocate larger of doubled size or required size + int request = Math.max(2 * words.length, wordsRequired); + setLength(request); + sizeIsSticky = false; + } + } + + /** + * Ensures that the BitSet can accommodate a given wordIndex, temporarily + * violating the invariants. The caller must restore the invariants before + * returning to the user, possibly using recalculateWordsInUse(). + * + * @param wordIndex + * the index to be accommodated. + */ + protected void expandTo(int wordIndex) { + int wordsRequired = wordIndex + 1; + if (wordsInUse < wordsRequired) { + ensureCapacity(wordsRequired); + wordsInUse = wordsRequired; + } + } + + + /** + * Sets the bit at the specified index to {@code true}. + * + * @param bitIndex + * a bit index + * @throws IndexOutOfBoundsException + * if the specified index is negative + * @since JDK1.0 + */ + public void set(int bitIndex) { + if (bitIndex < 0) + throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex); + + int wordIndex = wordIndex(bitIndex); + expandTo(wordIndex); + + words[wordIndex] |= (1 << bitIndex); // Restores invariants + + } + + /** + * Sets the bit at the specified index to the specified value. + * + * @param bitIndex + * a bit index + * @param value + * a boolean value to set + * @throws IndexOutOfBoundsException + * if the specified index is negative + * @since 1.4 + */ + public void setBitTo(int bitIndex, boolean value) { + if (value) + set(bitIndex); + else + clear(bitIndex); + } + + /** + * Sets the bits from the specified {@code fromIndex} (inclusive) to the + * specified {@code toIndex} (exclusive) to {@code true}. + * + * @param fromIndex + * index of the first bit to be set + * @param toIndex + * index after the last bit to be set + * @throws IndexOutOfBoundsException + * if {@code fromIndex} is negative, or {@code toIndex} is negative, + * or {@code fromIndex} is larger than {@code toIndex} + * @since 1.4 + */ + public void setBits(int fromIndex, int toIndex) { + + if (fromIndex == toIndex) + return; + + // Increase capacity if necessary + int startWordIndex = wordIndex(fromIndex); + int endWordIndex = wordIndex(toIndex - 1); + expandTo(endWordIndex); + + int firstWordMask = WORD_MASK << fromIndex; + int lastWordMask = WORD_MASK >>> -toIndex; + if (startWordIndex == endWordIndex) { + // Case 1: One word + words[startWordIndex] |= (firstWordMask & lastWordMask); + } else { + // Case 2: Multiple words + // Handle first word + words[startWordIndex] |= firstWordMask; + + // Handle intermediate words, if any + for (int i = startWordIndex + 1; i < endWordIndex; i++) + words[i] = WORD_MASK; + + // Handle last word (restores invariants) + words[endWordIndex] |= lastWordMask; + } + } + + /** + * Sets the bit specified by the index to {@code false}. + * + * @param bitIndex + * the index of the bit to be cleared + * @throws IndexOutOfBoundsException + * if the specified index is negative + * @since JDK1.0 + */ + public void clear(int bitIndex) { + if (bitIndex < 0) + throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex); + + int wordIndex = wordIndex(bitIndex); + if (wordIndex >= wordsInUse) + return; + + words[wordIndex] &= ~(1 << bitIndex); + + recalculateWordsInUse(); + } + + /** + * Sets the bits from the specified {@code fromIndex} (inclusive) to the + * specified {@code toIndex} (exclusive) to {@code false}. + * + * @param fromIndex + * index of the first bit to be cleared + * @param toIndex + * index after the last bit to be cleared + * @throws IndexOutOfBoundsException + * if {@code fromIndex} is negative, or {@code toIndex} is negative, + * or {@code fromIndex} is larger than {@code toIndex} + * @since 1.4 + */ + public void clearBits(int fromIndex, int toIndex) { + if (fromIndex == toIndex) + return; + + int startWordIndex = wordIndex(fromIndex); + if (startWordIndex >= wordsInUse) + return; + + int endWordIndex = wordIndex(toIndex - 1); + if (endWordIndex >= wordsInUse) { + toIndex = length(); + endWordIndex = wordsInUse - 1; + } + + int firstWordMask = WORD_MASK << fromIndex; + int lastWordMask = WORD_MASK >>> -toIndex; + if (startWordIndex == endWordIndex) { + // Case 1: One word + words[startWordIndex] &= ~(firstWordMask & lastWordMask); + } else { + // Case 2: Multiple words + // Handle first word + words[startWordIndex] &= ~firstWordMask; + + // Handle intermediate words, if any + for (int i = startWordIndex + 1; i < endWordIndex; i++) + words[i] = 0; + + // Handle last word + words[endWordIndex] &= ~lastWordMask; + } + + recalculateWordsInUse(); + } + + /** + * Sets all of the bits in this BitSet to {@code false}. + * + * @since 1.4 + */ + public void clearAll() { + while (wordsInUse > 0) + words[--wordsInUse] = 0; + } + + /** + * Returns the value of the bit with the specified index. The value is {@code + * true} if the bit with the index {@code bitIndex} is currently set in this + * {@code BitSet}; otherwise, the result is {@code false}. + * + * @param bitIndex + * the bit index + * @return the value of the bit with the specified index + * @throws IndexOutOfBoundsException + * if the specified index is negative + */ + public boolean get(int bitIndex) { + if (bitIndex < 0) + throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex); + + int wordIndex = wordIndex(bitIndex); + return (wordIndex < wordsInUse) + && ((words[wordIndex] & (1 << bitIndex)) != 0); + } + + /** + * Returns the index of the first bit that is set to {@code true} that occurs + * on or after the specified starting index. If no such bit exists then + * {@code -1} is returned. + * + *

+ * To iterate over the {@code true} bits in a {@code BitSet}, use the + * following loop: + * + *

+   * @code
+   * for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
+   *     // operate on index i here
+   * }}
+   * 
+ * + * @param fromIndex + * the index to start checking from (inclusive) + * @return the index of the next set bit, or {@code -1} if there is no such + * bit + * @throws IndexOutOfBoundsException + * if the specified index is negative + * @since 1.4 + */ + public int nextSetBit(int fromIndex) { + if (fromIndex < 0) + throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex); + + int u = wordIndex(fromIndex); + if (u >= wordsInUse) + return -1; + + int word = words[u] & (WORD_MASK << fromIndex); + + while (true) { + if (word != 0) + return (u * BITS_PER_WORD) + Integer.numberOfTrailingZeros(word); + if (++u == wordsInUse) + return -1; + word = words[u]; + } + } + + /** + * Returns the index of the first bit that is set to {@code false} that occurs + * on or after the specified starting index. + * + * @param fromIndex + * the index to start checking from (inclusive) + * @return the index of the next clear bit + * @throws IndexOutOfBoundsException + * if the specified index is negative + * @since 1.4 + */ + public int nextClearBit(int fromIndex) { + // Neither spec nor implementation handle bitsets of maximal length. + // See 4816253. + if (fromIndex < 0) + throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex); + + int u = wordIndex(fromIndex); + if (u >= wordsInUse) + return fromIndex; + + int word = ~words[u] & (WORD_MASK << fromIndex); + + while (true) { + if (word != 0) + return (u * BITS_PER_WORD) + Integer.numberOfTrailingZeros(word); + if (++u == wordsInUse) + return wordsInUse * BITS_PER_WORD; + word = ~words[u]; + } + } + + /** + * Returns the "logical size" of this {@code BitSet}: the index of the highest + * set bit in the {@code BitSet} plus one. Returns zero if the {@code BitSet} + * contains no set bits. + * + * @return the logical size of this {@code BitSet} + * @since 1.2 + */ + public int length() { + if (wordsInUse == 0) + return 0; + + return BITS_PER_WORD * (wordsInUse - 1) + + (BITS_PER_WORD - Integer.numberOfLeadingZeros(words[wordsInUse - 1])); + } + + /** + * Returns true if this {@code BitSet} contains no bits that are set to + * {@code true}. + * + * @return boolean indicating whether this {@code BitSet} is empty + * @since 1.4 + */ + public boolean isEmpty() { + return wordsInUse == 0; + } + + /** + * Returns true if the specified {@code BitSet} has any bits set to {@code + * true} that are also set to {@code true} in this {@code BitSet}. + * + * @param set + * {@code BitSet} to intersect with + * @return boolean indicating whether this {@code BitSet} intersects the + * specified {@code BitSet} + * @since 1.4 + */ + public boolean intersects(BS set) { + for (int i = Math.min(wordsInUse, set.wordsInUse) - 1; i >= 0; i--) + if ((words[i] & set.words[i]) != 0) + return true; + return false; + } + + /** + * Returns the number of bits set to {@code true} in this {@code BitSet}. + * + * @return the number of bits set to {@code true} in this {@code BitSet} + * @since 1.4 + */ + public int cardinality() { + int sum = 0; + for (int i = 0; i < wordsInUse; i++) + sum += Integer.bitCount(words[i]); + return sum; + } + + /** + * Performs a logical AND of this target bit set with the argument bit + * set. This bit set is modified so that each bit in it has the value {@code + * true} if and only if it both initially had the value {@code true} and the + * corresponding bit in the bit set argument also had the value {@code true}. + * + * @param set + * a bit set + */ + public void and(BS set) { + if (this == set) + return; + + while (wordsInUse > set.wordsInUse) + words[--wordsInUse] = 0; + + // Perform logical AND on words in common + for (int i = 0; i < wordsInUse; i++) + words[i] &= set.words[i]; + + recalculateWordsInUse(); + } + + /** + * Performs a logical OR of this bit set with the bit set argument. + * This bit set is modified so that a bit in it has the value {@code true} if + * and only if it either already had the value {@code true} or the + * corresponding bit in the bit set argument has the value {@code true}. + * + * @param set + * a bit set + */ + public void or(BS set) { + if (this == set) + return; + + int wordsInCommon = Math.min(wordsInUse, set.wordsInUse); + + if (wordsInUse < set.wordsInUse) { + ensureCapacity(set.wordsInUse); + wordsInUse = set.wordsInUse; + } + + // Perform logical OR on words in common + for (int i = 0; i < wordsInCommon; i++) + words[i] |= set.words[i]; + + // Copy any remaining words + if (wordsInCommon < set.wordsInUse) + System.arraycopy(set.words, wordsInCommon, words, wordsInCommon, + wordsInUse - wordsInCommon); + + } + + /** + * Performs a logical XOR of this bit set with the bit set argument. + * This bit set is modified so that a bit in it has the value {@code true} if + * and only if one of the following statements holds: + *
    + *
  • The bit initially has the value {@code true}, and the corresponding bit + * in the argument has the value {@code false}. + *
  • The bit initially has the value {@code false}, and the corresponding + * bit in the argument has the value {@code true}. + *
+ * + * @param set + * a bit set + */ + public void xor(BS set) { + int wordsInCommon = Math.min(wordsInUse, set.wordsInUse); + + if (wordsInUse < set.wordsInUse) { + ensureCapacity(set.wordsInUse); + wordsInUse = set.wordsInUse; + } + + // Perform logical XOR on words in common + for (int i = 0; i < wordsInCommon; i++) + words[i] ^= set.words[i]; + + // Copy any remaining words + if (wordsInCommon < set.wordsInUse) + System.arraycopy(set.words, wordsInCommon, words, wordsInCommon, + set.wordsInUse - wordsInCommon); + + recalculateWordsInUse(); + } + + /** + * Clears all of the bits in this {@code BitSet} whose corresponding bit is + * set in the specified {@code BitSet}. + * + * @param set + * the {@code BitSet} with which to mask this {@code BitSet} + * @since 1.2 + */ + public void andNot(BS set) { + // Perform logical (a & !b) on words in common + for (int i = Math.min(wordsInUse, set.wordsInUse) - 1; i >= 0; i--) + words[i] &= ~set.words[i]; + + recalculateWordsInUse(); + } + + /** + * Returns a hash code value for this bit set. The hash code depends only on + * which bits have been set within this BitSet. The algorithm + * used to compute it may be described as follows. + *

+ * Suppose the bits in the BitSet were to be stored in an array + * of long integers called, say, words, in such a + * manner that bit k is set in the BitSet (for + * nonnegative values of k) if and only if the expression + * + *

+   * ((k >> 6) < words.length) && ((words[k >> 6] & (1 << (bit & 0x3F))) != 0)
+   * 
+ * + * is true. Then the following definition of the hashCode method + * would be a correct implementation of the actual algorithm: + * + *
+   * public int hashCode() {
+   *  long h = 1234;
+   *  for (int i = words.length; --i >= 0;) {
+   *    h ˆ= words[i] * (i + 1);
+   *  }
+   *  return (int) ((h >> 32) ˆ h);
+   * }
+   * 
+ * + * Note that the hash code values change if the set of bits is altered. + *

+ * Overrides the hashCode method of Object. + * + * @return a hash code value for this bit set. + */ + @Override + public int hashCode() { + long h = 1234; + for (int i = wordsInUse; --i >= 0;) + h ^= words[i] * (i + 1); + + return (int) ((h >> 32) ^ h); + } + + /** + * Returns the number of bits of space actually in use by this {@code BitSet} + * to represent bit values. The maximum element in the set is the size - 1st + * element. + * + * @return the number of bits currently in this bit set + */ + public int size() { + return words.length * BITS_PER_WORD; + } + + /** + * Compares this object against the specified object. The result is {@code + * true} if and only if the argument is not {@code null} and is a {@code + * Bitset} object that has exactly the same set of bits set to {@code true} as + * this bit set. That is, for every nonnegative {@code int} index {@code k}, + * + *

+   * ((BitSet) obj).get(k) == this.get(k)
+   * 
+ * + * must be true. The current sizes of the two bit sets are not compared. + * + * @param obj + * the object to compare with + * @return {@code true} if the objects are the same; {@code false} otherwise + * @see #size() + */ + @Override + public boolean equals(Object obj) { + if (!(obj instanceof BS)) + return false; + if (this == obj) + return true; + + BS set = (BS) obj; + + if (wordsInUse != set.wordsInUse) + return false; + + // Check words in use by both BitSets + for (int i = 0; i < wordsInUse; i++) + if (words[i] != set.words[i]) + return false; + + return true; + } + + /** + * Cloning this {@code BitSet} produces a new {@code BitSet} that is equal to + * it. The clone of the bit set is another bit set that has exactly the same + * bits set to {@code true} as this bit set. + * + * @return a clone of this bit set + * @see #size() + */ + @Override + public Object clone() { + if (!sizeIsSticky && wordsInUse != words.length) + setLength(wordsInUse); + return copy(this); + } + + /** + * Attempts to reduce internal storage used for the bits in this bit set. + * Calling this method may, but is not required to, affect the value returned + * by a subsequent call to the {@link #size()} method. + * @param n + */ + private void setLength(int n) { + /** + * @j2sNative + * if (n == this.words.length) return; + * if (n == this.wordsInUse) { + * this.words = Clazz.newArray$(-1, this.words, 0, n); + * return; + * } + */ + {} + int[] a = new int[n]; + System.arraycopy(words, 0, a, 0, wordsInUse); + words = a; + } + + /** + * Returns a string representation of this bit set. For every index for which + * this {@code BitSet} contains a bit in the set state, the decimal + * representation of that index is included in the result. Such indices are + * listed in order from lowest to highest, separated by ", " (a comma and + * a space) and surrounded by braces, resulting in the usual mathematical + * notation for a set of integers. + * + *

+ * Example: + * + *

+   * BitSet drPepper = new BitSet();
+   * 
+ * + * Now {@code drPepper.toString()} returns "{}". + *

+ * + *

+   * drPepper.set(2);
+   * 
+ * + * Now {@code drPepper.toString()} returns "{2}". + *

+ * + *

+   * drPepper.set(4);
+   * drPepper.set(10);
+   * 
+ * + * Now {@code drPepper.toString()} returns "{2, 4, 10}". + * + * @return a string representation of this bit set + */ + @Override + public String toString() { + return escape(this, '(', ')'); + } + + private final static int[] emptyBitmap = new int[0]; + + /** + * fast copy + * + * @param bitsetToCopy + * @return bs + */ + public static BS copy(BS bitsetToCopy) { + BS bs; + /** + * Clazz.clone will copy wordsInUse and sizeIsSticky, + * but just a pointer to the words array. + * + * @j2sNative + * + * bs = Clazz.clone(bitsetToCopy); + * + */ + { + bs = new BS(); + } + int wordCount = bitsetToCopy.wordsInUse; + if (wordCount == 0) { + bs.words = emptyBitmap; + } else { + + /** + * Clazz.clone will copy wordsInUse and sizeIsSticky, + * but just a pointer to the words array. + * + * @j2sNative + * + * bs.words = Clazz.newArray(-1, bitsetToCopy.words, 0, bs.wordsInUse = wordCount); + * + */ + { + bs.words = new int[bs.wordsInUse = wordCount]; + System.arraycopy(bitsetToCopy.words, 0, bs.words, 0, wordCount); + } + + } + return bs; + } + + /** + * + * @param max + * @return n bits below max + */ + public int cardinalityN(int max) { + int n = cardinality(); + for (int i = length(); --i >= max;) + if (get(i)) + n--; + return n; + } + + @Override + public String toJSON() { + + int numBits = (wordsInUse > 128 ? cardinality() : wordsInUse + * BITS_PER_WORD); + SB b = SB.newN(6 * numBits + 2); + b.appendC('['); + + int i = nextSetBit(0); + if (i != -1) { + b.appendI(i); + for (i = nextSetBit(i + 1); i >= 0; i = nextSetBit(i + 1)) { + int endOfRun = nextClearBit(i); + do { + b.append(", ").appendI(i); + } while (++i < endOfRun); + } + } + + b.appendC(']'); + return b.toString(); + } + + public static String escape(BS bs, char chOpen, char chClose) { + if (bs == null) + return chOpen + "{}" + chClose; + SB s = new SB(); + s.append(chOpen + "{"); + int imax = bs.length(); + int iLast = -1; + int iFirst = -2; + int i = -1; + while (++i <= imax) { + boolean isSet = bs.get(i); + if (i == imax || iLast >= 0 && !isSet) { + if (iLast >= 0 && iFirst != iLast) + s.append((iFirst == iLast - 1 ? " " : ":") + iLast); + if (i == imax) + break; + iLast = -1; + } + if (bs.get(i)) { + if (iLast < 0) { + s.append((iFirst == -2 ? "" : " ") + i); + iFirst = i; + } + iLast = i; + } + } + s.append("}").appendC(chClose); + return s.toString(); + } + + public static BS unescape(String str) { + char ch; + int len; + if (str == null || (len = (str = str.trim()).length()) < 4 + || str.equalsIgnoreCase("({null})") + || (ch = str.charAt(0)) != '(' && ch != '[' + || str.charAt(len - 1) != (ch == '(' ? ')' : ']') + || str.charAt(1) != '{' || str.indexOf('}') != len - 2) + return null; + len -= 2; + for (int i = len; --i >= 2;) + if (!PT.isDigit(ch = str.charAt(i)) && ch != ' ' && ch != '\t' + && ch != ':') + return null; + int lastN = len; + while (PT.isDigit(str.charAt(--lastN))) { + // loop + } + if (++lastN == len) + lastN = 0; + else + try { + lastN = Integer.parseInt(str.substring(lastN, len)); + } catch (NumberFormatException e) { + return null; + } + BS bs = BS.newN(lastN); + lastN = -1; + int iPrev = -1; + int iThis = -2; + for (int i = 2; i <= len; i++) { + switch (ch = str.charAt(i)) { + case '\t': + case ' ': + case '}': + if (iThis < 0) + break; + if (iThis < lastN) + return null; + lastN = iThis; + if (iPrev < 0) + iPrev = iThis; + bs.setBits(iPrev, iThis + 1); + iPrev = -1; + iThis = -2; + break; + case ':': + iPrev = lastN = iThis; + iThis = -2; + break; + default: + if (PT.isDigit(ch)) { + if (iThis < 0) + iThis = 0; + iThis = (iThis * 10) + (ch - 48); + } + } + } + return (iPrev >= 0 ? null : bs); + } + +} diff --git a/src/javajs/util/Base64.java b/src/javajs/util/Base64.java new file mode 100644 index 0000000..a7ac3a9 --- /dev/null +++ b/src/javajs/util/Base64.java @@ -0,0 +1,120 @@ +// Version 1.0a +// Copyright (C) 1998, James R. Weeks and BioElectroMech. +// Visit BioElectroMech at www.obrador.com. Email James@obrador.com. + +// See license.txt for details about the allowed used of this software. +// This software is based in part on the work of the Independent JPEG Group. +// See IJGreadme.txt for details about the Independent JPEG Group's license. + +// This encoder is inspired by the Java Jpeg encoder by Florian Raemy, +// studwww.eurecom.fr/~raemy. +// It borrows a great deal of code and structure from the Independent +// Jpeg Group's Jpeg 6a library, Copyright Thomas G. Lane. +// See license.txt for details. + +package javajs.util; + + +public class Base64 { + + // 0 1 2 3 4 5 6 + // 0123456789012345678901234567890123456789012345678901234567890123 + private static String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + // 41----------------------5A + // 61----------------------7A + // 30------39 + // 2B + // 2F + // alternative "URL-SAFE" 2D and 5F -_ + + private static int[] decode64 = new int[] { + 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, //0x00-0x0F + 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, //0x10-0x1F + 0,0,0,0, 0,0,0,0, 0,0,0,62, 0,62,0,63, //0x20-0x2F + 52,53,54,55, 56,57,58,59, 60,61,0,0, 0,0,0,0, //0x30-0x3F + 0,0,1,2, 3,4,5,6, 7,8,9,10, 11,12,13,14, //0x40-0x4F + 15,16,17,18, 19,20,21,22, 23,24,25,0, 0,0,0,63, //0x50-0x5F + 0,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, //0x60-0x6F + 41,42,43,44, 45,46,47,48, 49,50,51,0, 0,0,0,0, //0x70-0x7F + }; + +// public static void write(byte[] bytes, OutputChannel out) { +// SB sb = getBase64(bytes); +// int len = sb.length(); +// byte[] b = new byte[1]; +// for (int i = 0; i < len; i++) { +// b[0] = (byte) sb.charAt(i); +// out.write(b, 0, 1); +// } +// } + + public static byte[] getBytes64(byte[] bytes) { + return getBase64(bytes).toBytes(0, -1); + } + + /** + * + * @param bytes + * @return BASE64-encoded string, without ";base64," + */ + public static SB getBase64(byte[] bytes) { + long nBytes = bytes.length; + SB sout = new SB(); + if (nBytes == 0) + return sout; + for (int i = 0, nPad = 0; i < nBytes && nPad == 0;) { + if (i % 75 == 0 && i != 0) + sout.append("\r\n"); + nPad = (i + 2 == nBytes ? 1 : i + 1 == nBytes ? 2 : 0); + int outbytes = ((bytes[i++] << 16) & 0xFF0000) + | ((nPad == 2 ? 0 : bytes[i++] << 8) & 0x00FF00) + | ((nPad >= 1 ? 0 : (int) bytes[i++]) & 0x0000FF); + //System.out.println(Integer.toHexString(outbytes)); + sout.appendC(base64.charAt((outbytes >> 18) & 0x3F)); + sout.appendC(base64.charAt((outbytes >> 12) & 0x3F)); + sout.appendC(nPad == 2 ? '=' : base64.charAt((outbytes >> 6) & 0x3F)); + sout.appendC(nPad >= 1 ? '=' : base64.charAt(outbytes & 0x3F)); + } + return sout; + } + + //Note: Just a simple decoder here. Nothing fancy at all + // Because of the 0s in decode64, this is not a VERIFIER + // Rather, it may decode even bad Base64-encoded data + // + // Bob Hanson 4/2007 + + public static byte[] decodeBase64(String strBase64) { + int nBytes = 0; + int ch; + int pt0 = strBase64.indexOf(";base64,") + 1; + if (pt0 > 0) + pt0 += 7; + char[] chars64 = strBase64.toCharArray(); + int len64 = chars64.length; + if (len64 == 0) + return new byte[0]; + for (int i = len64; --i >= pt0;) + nBytes += ((ch = chars64[i] & 0x7F) == 'A' || decode64[ch] > 0 ? 3 : 0); + nBytes = nBytes >> 2; + byte[] bytes = new byte[nBytes]; + int offset = 18; + for (int i = pt0, pt = 0, b = 0; i < len64; i++) { + if (decode64[ch = chars64[i] & 0x7F] > 0 || ch == 'A' || ch == '=') { + b |= decode64[ch] << offset; + //System.out.println(chars64[i] + " " + decode64[ch] + " " + offset + " " + Integer.toHexString(b)); + offset -= 6; + if (offset < 0) { + bytes[pt++] = (byte) ((b & 0xFF0000) >> 16); + if (pt < nBytes) + bytes[pt++] = (byte) ((b & 0xFF00) >> 8); + if (pt < nBytes) + bytes[pt++] = (byte) (b & 0xFF); + offset = 18; + b = 0; + } + } + } + return bytes; + } +} \ No newline at end of file diff --git a/src/javajs/util/BinaryDocument.java b/src/javajs/util/BinaryDocument.java new file mode 100644 index 0000000..7ac5a56 --- /dev/null +++ b/src/javajs/util/BinaryDocument.java @@ -0,0 +1,352 @@ +/* $RCSfile$ + * $Date: 2006-03-18 15:59:33 -0600 (Sat, 18 Mar 2006) $ + * $Revision: 4652 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2003-2005 Miguel, Jmol Development, www.jmol.org + * + * Contact: hansonr@stolaf.edu + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.util; + + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.util.Map; + +import javajs.api.GenericBinaryDocument; +import javajs.api.GenericOutputChannel; + +/* a basic binary file reader (extended by CompoundDocument). + * + * Note that YOU are responsible for determining whether a file + * is bigEndian or littleEndian; the default is bigEndian. + * + * JavaScript note: readShort() malfunctioned because (short) (xx << 8) + * isn't the same as (int) (xx << 8); same problem in java.io.DataStream + * + * + */ + +public class BinaryDocument extends BC implements GenericBinaryDocument { + + public BinaryDocument() { + } + + + // called by reflection + + protected DataInputStream stream; + protected boolean isRandom = false; + protected boolean isBigEndian = true; + protected BufferedInputStream bis; + protected long nBytes; + protected GenericOutputChannel out; + + + @Override + public void close() { + if (stream != null) + try { + stream.close(); + } catch (IOException e) { + // ignore + } + if (out != null) + out.closeChannel(); + } + + @Override + public BinaryDocument setStream(BufferedInputStream bis, boolean isBigEndian) { + this.bis = bis; + if (bis != null) { + stream = new DataInputStream(bis); + } + this.isBigEndian = isBigEndian; + return this; + } + + @Override + public BufferedInputStream getInputStream() { + return bis; + } + + @Override + public void setStreamData(DataInputStream stream, boolean isBigEndian) { + if (stream != null) + this.stream = stream; + this.isBigEndian = isBigEndian; + } + + @Override + public void setOutputChannel(GenericOutputChannel out) { + this.out = out; + } + + public void setRandom(boolean TF) { + isRandom = TF; + //CANNOT be random for web + } + + @Override + public byte readByte() throws IOException { + nBytes++; + return ioReadByte(); + } + + @Override + public int readUInt8() throws IOException { + nBytes++; + int b = stream.readUnsignedByte(); + if (out != null) + out.writeByteAsInt(b); + return b; + } + + private byte ioReadByte() throws IOException { + byte b = stream.readByte(); + if (out != null) + out.writeByteAsInt(b); + return b; + } + + @Override + public byte[] readBytes(int n) throws IOException { + byte[] b = new byte[n]; + readByteArray(b, 0, n); + return b; + } + + @Override + public int readByteArray(byte[] b, int off, int len) throws IOException { + int n = ioRead(b, off, len); + nBytes += n; + return n; + } + + private int ioRead(byte[] b, int off, int len) throws IOException { + int m = 0; + while (len > 0) { + int n = stream.read(b, off, len); + m += n; + if (n > 0 && out != null) + out.write(b, off, n); + if (n >= len) + break; + off += n; + len -= n; + } + return m; + } + + @Override + public String readString(int nChar) throws IOException { + byte[] temp = new byte[nChar]; + int n = readByteArray(temp, 0, nChar); + return new String(temp, 0, n, "UTF-8"); + } + + @Override + public short readShort() throws IOException { + nBytes += 2; + short n = (isBigEndian ? ioReadShort() + : (short) ((ioReadByte() & 0xff) + | (ioReadByte() & 0xff) << 8)); + /** + * @j2sNative + * + * return (n > 0x7FFF ? n - 0x10000 : n); + */ + { + return n; + } + } + + private short ioReadShort() throws IOException { + short b = stream.readShort(); + if (out != null) + out.writeShort(b); + return b; + } + + + @Override + public int readIntLE() throws IOException { + nBytes += 4; + return readLEInt(); + } + + @Override + public int readInt() throws IOException { + nBytes += 4; + return (isBigEndian ? ioReadInt() : readLEInt()); + } + + private int ioReadInt() throws IOException { + int i = stream.readInt(); + if (out != null) + out.writeInt(i); + return i; + } + + @Override + public int swapBytesI(int n) { + return (((n >> 24) & 0xff) + | ((n >> 16) & 0xff) << 8 + | ((n >> 8) & 0xff) << 16 + | (n & 0xff) << 24); + } + + @Override + public short swapBytesS(short n) { + return (short) ((((n >> 8) & 0xff) + | (n & 0xff) << 8)); + } + + + @Override + public int readUnsignedShort() throws IOException { + nBytes += 2; + int a = (ioReadByte() & 0xff); + int b = (ioReadByte() & 0xff); + return (isBigEndian ? (a << 8) + b : (b << 8) + a); + } + + @Override + public long readLong() throws IOException { + nBytes += 8; + return (isBigEndian ? ioReadLong() + : ((((long) ioReadByte()) & 0xff) + | (((long) ioReadByte()) & 0xff) << 8 + | (((long) ioReadByte()) & 0xff) << 16 + | (((long) ioReadByte()) & 0xff) << 24 + | (((long) ioReadByte()) & 0xff) << 32 + | (((long) ioReadByte()) & 0xff) << 40 + | (((long) ioReadByte()) & 0xff) << 48 + | (((long) ioReadByte()) & 0xff) << 54)); + } + + private long ioReadLong() throws IOException { + long b = stream.readLong(); + if (out != null) + out.writeLong(b); + return b; + } + + private int readLEInt() throws IOException { + ioRead(t8, 0, 4); + return bytesToInt(t8, 0, false); + } + + byte[] t8 = new byte[8]; + + @Override + public float readFloat() throws Exception { + return intToFloat(readInt()); + } + + @Override + public double readDouble() throws IOException { + /** + * + * reading the float equivalent here in JavaScript + * + * @j2sNative + * + * this.readByteArray(this.t8, 0, 8); + * return this.bytesToDoubleToFloat(this.t8, 0, this.isBigEndian); + * + */ + { + nBytes += 8; + return (isBigEndian ? ioReadDouble() : Double.longBitsToDouble(readLELong())); + } + } + + private double ioReadDouble() throws IOException { + double d = stream.readDouble(); + if (out != null) + out.writeLong(Double.doubleToRawLongBits(d)); + return d; + } + + private long readLELong() throws IOException { + return ((((long) ioReadByte()) & 0xff) + | (((long) ioReadByte()) & 0xff) << 8 + | (((long) ioReadByte()) & 0xff) << 16 + | (((long) ioReadByte()) & 0xff) << 24 + | (((long) ioReadByte()) & 0xff) << 32 + | (((long) ioReadByte()) & 0xff) << 40 + | (((long) ioReadByte()) & 0xff) << 48 + | (((long) ioReadByte()) & 0xff) << 56); + } + + @Override + public void seek(long offset) { + // slower, but all that is available using the applet + try { + if (offset == nBytes) + return; + if (offset < nBytes) { + stream.reset(); + if (out != null && nBytes != 0) + out.reset(); + nBytes = 0; + } else { + offset -= nBytes; + } + if (out == null) { + stream.skipBytes((int)offset); + } else { + readByteArray(new byte[(int)offset], 0, (int) offset); + } + nBytes += offset; + } catch (IOException e) { + System.out.println(e.toString()); + } + } + + @Override + public long getPosition() { + return nBytes; + } + + @Override + public SB getAllDataFiles(String binaryFileList, String firstFile) { + return null; + } + + @Override + public void getAllDataMapped(String replace, String string, + Map fileData) { + } + + +/* random access -- application only: + * + void seekFile(long offset) { + try { + file.seek(offset); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + } +*/ +} diff --git a/src/javajs/util/CU.java b/src/javajs/util/CU.java new file mode 100644 index 0000000..e46287e --- /dev/null +++ b/src/javajs/util/CU.java @@ -0,0 +1,555 @@ +package javajs.util; + +import java.util.Hashtable; +import java.util.Map; + +import javajs.api.GenericColor; + +/** + * ColorUtility + * + */ + +public class CU { + + public static String toRGBHexString(GenericColor c) { + int rgb = c.getRGB(); + if (rgb == 0) + return "000000"; + String r = "00" + Integer.toHexString((rgb >> 16) & 0xFF); + r = r.substring(r.length() - 2); + String g = "00" + Integer.toHexString((rgb >> 8) & 0xFF); + g = g.substring(g.length() - 2); + String b = "00" + Integer.toHexString(rgb & 0xFF); + b = b.substring(b.length() - 2); + return r + g + b; + } + + public static String toCSSString(GenericColor c) { + int opacity = c.getOpacity255(); + if (opacity == 255) + return "#" + toRGBHexString(c); + int rgb = c.getRGB(); + return "rgba(" + ((rgb>>16)&0xFF) + "," + ((rgb>>8)&0xff) + "," + (rgb&0xff) + "," + opacity/255f + ")"; + } + + private final static String[] colorNames = { + "black", // 000000 + "pewhite", // ffffff + "pecyan", // 00ffff + "pepurple", // d020ff + "pegreen", // 00ff00 + "peblue", // 6060ff + "peviolet", // ff80c0 + "pebrown", // a42028 + "pepink", // ffd8d8 + "peyellow", // ffff00 + "pedarkgreen", // 00c000 + "peorange", // ffb000 + "pelightblue", // b0b0ff + "pedarkcyan", // 00a0a0 + "pedarkgray", // 606060 + + "aliceblue", // F0F8FF + "antiquewhite", // FAEBD7 + "aqua", // 00FFFF + "aquamarine", // 7FFFD4 + "azure", // F0FFFF + "beige", // F5F5DC + "bisque", // FFE4C4 + "blanchedalmond", // FFEBCD + "blue", // 0000FF + "blueviolet", // 8A2BE2 + "brown", // A52A2A + "burlywood", // DEB887 + "cadetblue", // 5F9EA0 + "chartreuse", // 7FFF00 + "chocolate", // D2691E + "coral", // FF7F50 + "cornflowerblue", // 6495ED + "cornsilk", // FFF8DC + "crimson", // DC143C + "cyan", // 00FFFF + "darkblue", // 00008B + "darkcyan", // 008B8B + "darkgoldenrod", // B8860B + "darkgray", // A9A9A9 + "darkgreen", // 006400 + "darkkhaki", // BDB76B + "darkmagenta", // 8B008B + "darkolivegreen", // 556B2F + "darkorange", // FF8C00 + "darkorchid", // 9932CC + "darkred", // 8B0000 + "darksalmon", // E9967A + "darkseagreen", // 8FBC8F + "darkslateblue", // 483D8B + "darkslategray", // 2F4F4F + "darkturquoise", // 00CED1 + "darkviolet", // 9400D3 + "deeppink", // FF1493 + "deepskyblue", // 00BFFF + "dimgray", // 696969 + "dodgerblue", // 1E90FF + "firebrick", // B22222 + "floralwhite", // FFFAF0 16775920 + "forestgreen", // 228B22 + "fuchsia", // FF00FF + "gainsboro", // DCDCDC + "ghostwhite", // F8F8FF + "gold", // FFD700 + "goldenrod", // DAA520 + "gray", // 808080 + "green", // 008000 + "greenyellow", // ADFF2F + "honeydew", // F0FFF0 + "hotpink", // FF69B4 + "indianred", // CD5C5C + "indigo", // 4B0082 + "ivory", // FFFFF0 + "khaki", // F0E68C + "lavender", // E6E6FA + "lavenderblush", // FFF0F5 + "lawngreen", // 7CFC00 + "lemonchiffon", // FFFACD + "lightblue", // ADD8E6 + "lightcoral", // F08080 + "lightcyan", // E0FFFF + "lightgoldenrodyellow", // FAFAD2 + "lightgreen", // 90EE90 + "lightgrey", // D3D3D3 + "lightgray", // D3D3D3 + "lightpink", // FFB6C1 + "lightsalmon", // FFA07A + "lightseagreen", // 20B2AA + "lightskyblue", // 87CEFA + "lightslategray", // 778899 + "lightsteelblue", // B0C4DE + "lightyellow", // FFFFE0 + "lime", // 00FF00 + "limegreen", // 32CD32 + "linen", // FAF0E6 + "magenta", // FF00FF + "maroon", // 800000 + "mediumaquamarine", // 66CDAA + "mediumblue", // 0000CD + "mediumorchid", // BA55D3 + "mediumpurple", // 9370DB + "mediumseagreen", // 3CB371 + "mediumslateblue", // 7B68EE + "mediumspringgreen", // 00FA9A + "mediumturquoise", // 48D1CC + "mediumvioletred", // C71585 + "midnightblue", // 191970 + "mintcream", // F5FFFA + "mistyrose", // FFE4E1 + "moccasin", // FFE4B5 + "navajowhite", // FFDEAD + "navy", // 000080 + "oldlace", // FDF5E6 + "olive", // 808000 + "olivedrab", // 6B8E23 + "orange", // FFA500 + "orangered", // FF4500 + "orchid", // DA70D6 + "palegoldenrod", // EEE8AA + "palegreen", // 98FB98 + "paleturquoise", // AFEEEE + "palevioletred", // DB7093 + "papayawhip", // FFEFD5 + "peachpuff", // FFDAB9 + "peru", // CD853F + "pink", // FFC0CB + "plum", // DDA0DD + "powderblue", // B0E0E6 + "purple", // 800080 + "red", // FF0000 + "rosybrown", // BC8F8F + "royalblue", // 4169E1 + "saddlebrown", // 8B4513 + "salmon", // FA8072 + "sandybrown", // F4A460 + "seagreen", // 2E8B57 + "seashell", // FFF5EE + "sienna", // A0522D + "silver", // C0C0C0 + "skyblue", // 87CEEB + "slateblue", // 6A5ACD + "slategray", // 708090 + "snow", // FFFAFA 16775930 + "springgreen", // 00FF7F + "steelblue", // 4682B4 + "tan", // D2B48C + "teal", // 008080 + "thistle", // D8BFD8 + "tomato", // FF6347 + "turquoise", // 40E0D0 + "violet", // EE82EE + "wheat", // F5DEB3 + "white", // FFFFFF 16777215 + "whitesmoke", // F5F5F5 + "yellow", // FFFF00 + "yellowgreen", // 9ACD32 + // plus a few rasmol names/values + "bluetint", // AFD7FF + "greenblue", // 2E8B57 + "greentint", // 98FFB3 + "grey", // 808080 + "gray", + "pinktint", // FFABBB + "redorange", // FF4500 + "yellowtint", // F6F675 + }; + + private final static int[] colorArgbs = { + //#FFFFC3 hover + 0xFF000000, // black + // plus the PE chain colors + 0xFFffffff, // pewhite + 0xFF00ffff, // pecyan + 0xFFd020ff, // pepurple + 0xFF00ff00, // pegreen + 0xFF6060ff, // peblue + 0xFFff80c0, // peviolet + 0xFFa42028, // pebrown + 0xFFffd8d8, // pepink + 0xFFffff00, // peyellow + 0xFF00c000, // pedarkgreen + 0xFFffb000, // peorange + 0xFFb0b0ff, // pelightblue + 0xFF00a0a0, // pedarkcyan + 0xFF606060, // pedarkgray + // standard JavaScript + 0xFFF0F8FF, // aliceblue + 0xFFFAEBD7, // antiquewhite + 0xFF00FFFF, // aqua + 0xFF7FFFD4, // aquamarine + 0xFFF0FFFF, // azure + 0xFFF5F5DC, // beige + 0xFFFFE4C4, // bisque + 0xFFFFEBCD, // blanchedalmond + 0xFF0000FF, // blue + 0xFF8A2BE2, // blueviolet + 0xFFA52A2A, // brown + 0xFFDEB887, // burlywood + 0xFF5F9EA0, // cadetblue + 0xFF7FFF00, // chartreuse + 0xFFD2691E, // chocolate + 0xFFFF7F50, // coral + 0xFF6495ED, // cornflowerblue + 0xFFFFF8DC, // cornsilk + 0xFFDC143C, // crimson + 0xFF00FFFF, // cyan + 0xFF00008B, // darkblue + 0xFF008B8B, // darkcyan + 0xFFB8860B, // darkgoldenrod + 0xFFA9A9A9, // darkgray + 0xFF006400, // darkgreen + + 0xFFBDB76B, // darkkhaki + 0xFF8B008B, // darkmagenta + 0xFF556B2F, // darkolivegreen + 0xFFFF8C00, // darkorange + 0xFF9932CC, // darkorchid + 0xFF8B0000, // darkred + 0xFFE9967A, // darksalmon + 0xFF8FBC8F, // darkseagreen + 0xFF483D8B, // darkslateblue + 0xFF2F4F4F, // darkslategray + 0xFF00CED1, // darkturquoise + 0xFF9400D3, // darkviolet + 0xFFFF1493, // deeppink + 0xFF00BFFF, // deepskyblue + 0xFF696969, // dimgray + 0xFF1E90FF, // dodgerblue + 0xFFB22222, // firebrick + 0xFFFFFAF0, // floralwhite + 0xFF228B22, // forestgreen + 0xFFFF00FF, // fuchsia + 0xFFDCDCDC, // gainsboro + 0xFFF8F8FF, // ghostwhite + 0xFFFFD700, // gold + 0xFFDAA520, // goldenrod + 0xFF808080, // gray + 0xFF008000, // green + 0xFFADFF2F, // greenyellow + 0xFFF0FFF0, // honeydew + 0xFFFF69B4, // hotpink + 0xFFCD5C5C, // indianred + 0xFF4B0082, // indigo + 0xFFFFFFF0, // ivory + 0xFFF0E68C, // khaki + 0xFFE6E6FA, // lavender + 0xFFFFF0F5, // lavenderblush + 0xFF7CFC00, // lawngreen + 0xFFFFFACD, // lemonchiffon + 0xFFADD8E6, // lightblue + 0xFFF08080, // lightcoral + 0xFFE0FFFF, // lightcyan + 0xFFFAFAD2, // lightgoldenrodyellow + 0xFF90EE90, // lightgreen + 0xFFD3D3D3, // lightgrey + 0xFFD3D3D3, // lightgray + 0xFFFFB6C1, // lightpink + 0xFFFFA07A, // lightsalmon + 0xFF20B2AA, // lightseagreen + 0xFF87CEFA, // lightskyblue + 0xFF778899, // lightslategray + 0xFFB0C4DE, // lightsteelblue + 0xFFFFFFE0, // lightyellow + 0xFF00FF00, // lime + 0xFF32CD32, // limegreen + 0xFFFAF0E6, // linen + 0xFFFF00FF, // magenta + 0xFF800000, // maroon + 0xFF66CDAA, // mediumaquamarine + 0xFF0000CD, // mediumblue + 0xFFBA55D3, // mediumorchid + 0xFF9370DB, // mediumpurple + 0xFF3CB371, // mediumseagreen + 0xFF7B68EE, // mediumslateblue + 0xFF00FA9A, // mediumspringgreen + 0xFF48D1CC, // mediumturquoise + 0xFFC71585, // mediumvioletred + 0xFF191970, // midnightblue + 0xFFF5FFFA, // mintcream + 0xFFFFE4E1, // mistyrose + 0xFFFFE4B5, // moccasin + 0xFFFFDEAD, // navajowhite + 0xFF000080, // navy + 0xFFFDF5E6, // oldlace + 0xFF808000, // olive + 0xFF6B8E23, // olivedrab + 0xFFFFA500, // orange + 0xFFFF4500, // orangered + 0xFFDA70D6, // orchid + 0xFFEEE8AA, // palegoldenrod + 0xFF98FB98, // palegreen + 0xFFAFEEEE, // paleturquoise + 0xFFDB7093, // palevioletred + 0xFFFFEFD5, // papayawhip + 0xFFFFDAB9, // peachpuff + 0xFFCD853F, // peru + 0xFFFFC0CB, // pink + 0xFFDDA0DD, // plum + 0xFFB0E0E6, // powderblue + 0xFF800080, // purple + 0xFFFF0000, // red + 0xFFBC8F8F, // rosybrown + 0xFF4169E1, // royalblue + 0xFF8B4513, // saddlebrown + 0xFFFA8072, // salmon + 0xFFF4A460, // sandybrown + 0xFF2E8B57, // seagreen + 0xFFFFF5EE, // seashell + 0xFFA0522D, // sienna + 0xFFC0C0C0, // silver + 0xFF87CEEB, // skyblue + 0xFF6A5ACD, // slateblue + 0xFF708090, // slategray + 0xFFFFFAFA, // snow + 0xFF00FF7F, // springgreen + 0xFF4682B4, // steelblue + 0xFFD2B48C, // tan + 0xFF008080, // teal + 0xFFD8BFD8, // thistle + 0xFFFF6347, // tomato + 0xFF40E0D0, // turquoise + 0xFFEE82EE, // violet + 0xFFF5DEB3, // wheat + 0xFFFFFFFF, // white + 0xFFF5F5F5, // whitesmoke + 0xFFFFFF00, // yellow + 0xFF9ACD32, // yellowgreen + // plus a few rasmol names/values + 0xFFAFD7FF, // bluetint + 0xFF2E8B57, // greenblue + 0xFF98FFB3, // greentint + 0xFF808080, // grey + 0xFF808080, // gray + 0xFFFFABBB, // pinktint + 0xFFFF4500, // redorange + 0xFFF6F675, // yellowtint + }; + + private static final Map mapJavaScriptColors = new Hashtable(); + + static { + for (int i = colorNames.length; --i >= 0; ) + mapJavaScriptColors.put(colorNames[i], Integer.valueOf(colorArgbs[i])); + } + + /** + * accepts [xRRGGBB] or [0xRRGGBB] or [0xFFRRGGBB] or #RRGGBB or + * [red,green,blue] or a valid JavaScript color + * + * @param strColor + * @return 0 if invalid or integer color + */ + public static int getArgbFromString(String strColor) { + int len = 0; + if (strColor == null || (len = strColor.length()) == 0) + return 0; + strColor = strColor.toLowerCase(); + if (strColor.charAt(0) == '[' && strColor.charAt(len - 1) == ']') { + String check; + if (strColor.indexOf(",") >= 0) { + String[] tokens = PT.split(strColor.substring(1, strColor + .length() - 1), ","); + if (tokens.length != 3) + return 0; + float red = PT.parseFloat(tokens[0]); + float grn = PT.parseFloat(tokens[1]); + float blu = PT.parseFloat(tokens[2]); + return colorTriadToFFRGB(red, grn, blu); + } + switch (len) { + case 9: + check = "x"; + break; + case 10: + check = "0x"; + break; + default: + return 0; + } + if (strColor.indexOf(check) != 1) + return 0; + strColor = "#" + strColor.substring(len - 7, len - 1); + len = 7; + } + if (len == 7 && strColor.charAt(0) == '#') { + try { + return PT.parseIntRadix(strColor.substring(1, 7), 16) | 0xFF000000; + } catch (Exception e) { + return 0; + } + } + Integer boxedArgb = mapJavaScriptColors.get(strColor); + return (boxedArgb == null ? 0 : boxedArgb.intValue()); + } + + public static int colorTriadToFFRGB(float x, float y, float z) { + if (x <= 1 && y <= 1 && z <= 1) { + if (x > 0) + x = x * 256 - 1; + if (y > 0) + y = y * 256 - 1; + if (z > 0) + z = z * 256 - 1; + } + return rgb((int) x, (int) y, (int) z); + } + + public static int rgb(int red, int grn, int blu) { + return 0xFF000000 | (red << 16) | (grn << 8) | blu; + } + + public final static P3 colorPtFromString(String colorName) { + return colorPtFromInt(getArgbFromString(colorName), null); + } + + public final static P3 colorPtFromInt(int color, P3 pt) { + if (pt == null) + pt = new P3(); + pt.set((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF); + return pt; + } + + public static int colorPtToFFRGB(T3 pt) { + return colorTriadToFFRGB(pt.x, pt.y, pt.z); + } + + public static void toRGB3f(int c, float[] f) { + f[0] = ((c >> 16) & 0xFF) / 255f; // red + f[1] = ((c >> 8) & 0xFF) / 255f; + f[2] = (c & 0xFF) / 255f; + } + + /** + * Return a greyscale rgb value 0-FF using NTSC color lightness algorithm + *

+ * the alpha component is set to 0xFF. If you want a value in the + * range 0-255 then & the result with 0xFF; + * + * @param rgb the rgb value + * @return a grayscale value in the range 0 - 255 decimal + */ + public static int toFFGGGfromRGB(int rgb) { + int grey = (((2989 * ((rgb >> 16) & 0xFF)) + + (5870 * ((rgb >> 8) & 0xFF)) + + (1140 * (rgb & 0xFF)) + 5000) / 10000) & 0xFFFFFF; + return rgb(grey, grey, grey); + } + + + /** + * Convert RGB values to HSL (hue/saturation/lightness) + * + * @param rgb + * range 255 255 255 + * @param doRound + * set to false when just using this for + * for RGB -- HSL -- HSL' -- RGB' conversion + * + * @return the HSL as P3 range 360 100 100 + * @author hansonr + */ + + public static P3 rgbToHSL(P3 rgb, boolean doRound) { + // adapted from http://tips4java.wordpress.com/2009/07/05/hsl-color/ + // see http://en.wikipedia.org/wiki/HSL_color_space + float r = rgb.x / 255; + float g = rgb.y / 255; + float b = rgb.z / 255; + float min = Math.min(r, Math.min(g, b)); + float max = Math.max(r, Math.max(g, b)); + + // lightness is just p * 50 + + float p = (max + min); + float q = (max - min); + + float h = (60 * ((q == 0 ? 0 : max == r ? ((g - b) / q + 6) + : max == g ? (b - r) / q + 2 : (r - g) / q + 4))) % 360; + + float s = q / (q == 0 ? 1 : p <= 1 ? p : 2 - p); + + // we round to tenths for HSL so that we can return enough + // precision to get back 1-255 in RGB + return (doRound ? P3.new3(Math.round(h*10)/10f, Math.round(s * 1000)/10f, + Math.round(p * 500)/10f) : P3.new3(h, s * 100, p * 50)); + } + + /** + * Convert HSL (hue/saturation/luninance) values to RGB + * + * @param hsl in the range 360, 100, 100 + * @return the RGB as P3 range 0 to 255 + * @author hansonr + */ + public static P3 hslToRGB(P3 hsl) { + // adapted from http://tips4java.wordpress.com/2009/07/05/hsl-color/ + // see http://en.wikipedia.org/wiki/HSL_color_space + + // highly condensed + + float h = Math.max(0, Math.min(360, hsl.x)) / 60; + float s = Math.max(0, Math.min(100, hsl.y)) / 100; + float l = Math.max(0, Math.min(100, hsl.z)) / 100; + + float p = l - (l < 0.5 ? l : 1 - l) * s; + float q = 2 * (l - p); + + float r = toRGB(p, q, h + 2); + float g = toRGB(p, q, h); + float b = toRGB(p, q, h - 2); + return P3.new3(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)); + } + + private static float toRGB(float p, float q, float h) { + return ((h = (h + (h < 0 ? 6 : h > 6 ? -6 : 0))) < 1 ? p + q * h + : h < 3 ? p + q : h < 4 ? p + q * (4 - h) : p); + } + +} diff --git a/src/javajs/util/CifDataParser.java b/src/javajs/util/CifDataParser.java new file mode 100644 index 0000000..5be12fd --- /dev/null +++ b/src/javajs/util/CifDataParser.java @@ -0,0 +1,905 @@ +package javajs.util; + +import java.io.BufferedReader; + +import java.util.Hashtable; + +import java.util.Map; + +import javajs.api.GenericCifDataParser; +import javajs.api.GenericLineReader; + + +// BH 11/21/16 -- adds support for array grouping [...] - used in 2016-format magCIF files + +/** +* +* A CIF 1.0 tokenizer class for dealing with quoted strings in CIF files. +* +* Subclassed by org.jmol.adapters.readers.cif.Cif2DataParser +* +* Greek letters implemented in Jmol 13.3.9 and only for +* titles and space groups. All other mark ups ignored. +* +*

+* regarding the treatment of single quotes vs. primes in +* cif file, PMR wrote: +*

+*

+* * There is a formal grammar for CIF +* (see http://www.iucr.org/iucr-top/cif/index.html) +* which confirms this. The textual explanation is +*

+*

+* 14. Matching single or double quote characters (' or ") may +* be used to bound a string representing a non-simple data value +* provided the string does not extend over more than one line. +*

+*

+* 15. Because data values are invariably separated from other +* tokens in the file by white space, such a quote-delimited +* character string may contain instances of the character used +* to delimit the string provided they are not followed by white +* space. For example, the data item +* +* _example 'a dog's life' +* +* is legal; the data value is a dog's life. +*

+*

+* [PMR - the terminating character(s) are quote+whitespace. +* That would mean that: +* +* _example 'Jones' life' +* +* would be an error +*

+*

+* The CIF format was developed in that late 1980's under the aegis of the +* International Union of Crystallography (I am a consultant to the COMCIFs +* committee). It was ratified by the Union and there have been several +* workshops. mmCIF is an extension of CIF which includes a relational +* structure. The formal publications are: +*

+*

+* Hall, S. R. (1991). "The STAR File: A New Format for Electronic Data +* Transfer and Archiving", J. Chem. Inform. Comp. Sci., 31, 326-333. +* Hall, S. R., Allen, F. H. and Brown, I. D. (1991). "The Crystallographic +* Information File (CIF): A New Standard Archive File for Crystallography", +* Acta Cryst., A47, 655-685. +* Hall, S.R. & Spadaccini, N. (1994). "The STAR File: Detailed +* Specifications," J. Chem. Info. Comp. Sci., 34, 505-508. +*

+*/ + +public class CifDataParser implements GenericCifDataParser { + + protected int getVersion() { + return 1; + } + + /** + * The maximum number of columns (data keys) passed to the parser or found in the file + * for a given loop_ or category.subkey listing. + * + */ + public static final int KEY_MAX = 100; + + private GenericLineReader reader; + private BufferedReader br; + + /** + * from buffered reader + */ + protected String line; + + /** + * working string (buffer) + * + */ + protected String str; + + /** + * pointer to current character on str + */ + protected int ich; + + /** + * length of str + * + */ + protected int cch; + + /** + * whether we are processing an unquoted value or key + */ + protected boolean wasUnquoted; + + /** + * optional token terminator; in CIF 2.0 could be } or ] + */ + protected char cterm = '\0'; + + /** + * string to return for CIF data value . and ? + */ + protected String nullString = "\0"; + + /** + * A flag to create and return Java objects, not strings. + * Used only by Jmol scripting x = getProperty("cifInfo", filename). + */ + protected boolean asObject; + + + /** + * debugging flag passed from reader; unused + * + */ + protected boolean debugging; + + + /** + * private processing fields + * + */ + private Object strPeeked; + private int ichPeeked; + private int columnCount; + private String[] columnNames; + private Object[] columnData = new Object[KEY_MAX]; + private boolean isLoop; + private boolean haveData; + + /** + * comments at the top of a file, including #\#CIF_2.0, for example + */ + private SB fileHeader = new SB(); + private boolean isHeader = true; + + + /** + * Set the string value of what is returned for "." and "?" + * + * @param nullString null here returns "." and "?"; default is "\0" + * + */ + public void setNullValue(String nullString) { + this.nullString = nullString; + } + + /** + * A global, static map that contains field information. The assumption is that + * if we read a set of fields for, say, atom_site, once in a lifetime, then + * that should be good forever. Those are static lists. Or should be.... + */ + private static Map htFields = new Hashtable(); + + //////////////////////////////////////////////////////////////// + // special tokenizer class + //////////////////////////////////////////////////////////////// + + public CifDataParser() { + // for reflection + } + + @Override + public Object getColumnData(int i) { + return columnData[i]; + } + + @Override + public int getColumnCount() { + return columnCount; + } + + @Override + public String getColumnName(int i) { + return columnNames[i]; + } + + /** + * A Chemical Information File data parser. + * + * set() should be called immediately upon construction. + * + * Two options; one of reader or br should be null, or reader will be + * ignored. Just simpler this way... + * + * @param reader Anything that can deliver a line of text or null + * @param br A standard BufferedReader. + * @param debugging + * + */ + @Override + public CifDataParser set(GenericLineReader reader, BufferedReader br, boolean debugging) { + this.reader = reader; + this.br = br; + this.debugging = debugging; + return this; + } + + + /** + * + * @return commented-out section at the start of a CIF file. + * + */ + @Override + public String getFileHeader() { + return fileHeader.toString(); + } + + + /** + * Parses all CIF data for a reader defined in the constructor + * into a standard Map structure and close the BufferedReader if + * it exists. + * + * @return Hashtable of models Vector of Hashtable data + */ + @Override + public Map getAllCifData() { + line = ""; + String key; + Map data = null, data0 = null; + Map allData = new Hashtable(); + Lst> models = new Lst>(); + allData.put("models", models); + asObject = (getVersion() >= 2); + nullString = null; + Lst> saveFrames = new Lst>(); + try { + while ((key = getNextToken()) != null) { + if (key.startsWith("global_") || key.startsWith("data_")) { + models.addLast(data0 = data = new Hashtable()); + data.put("name", key); + continue; + } + if (key.startsWith("loop_")) { + getAllCifLoopData(data); + continue; + } + if (key.startsWith("save_")) { + if (key.equals("save_")) { + int n = saveFrames.size(); + if (n == 0) { + System.out.println("CIF ERROR ? save_ without corresponding save_xxxx"); + data = data0; + } else { + data = saveFrames.removeItemAt(n - 1); + } + } else { + saveFrames.addLast(data); + Map d = data; + data = new Hashtable(); + d.put(key, data); + } + continue; + } + if (key.charAt(0) != '_') { + System.out.println("CIF ERROR ? should be an underscore: " + key); + } else { + Object value = (asObject ? getNextTokenObject() : getNextToken()); + if (value == null) { + System.out.println("CIF ERROR ? end of file; data missing: " + key); + } else { + data.put(fixKey(key), value); + } + } + } + } catch (Exception e) { + // ? + } + asObject = false; + try { + if (br != null) + br.close(); + } catch (Exception e) { + // ? + } + nullString = "\0"; + return allData; + } + + /** + * create our own list of keywords and for each one create a list + * of data associated with that keyword. For example, a list of all + * x coordinates, then a list of all y coordinates, etc. + * + * @param data + * @throws Exception + */ + @SuppressWarnings("unchecked") + private void getAllCifLoopData(Map data) throws Exception { + String key; + Lst keyWords = new Lst(); + Object o; + while ((o = peekToken()) != null && o instanceof String && ((String) o).charAt(0) == '_') { + key = fixKey((String) getTokenPeeked()); + keyWords.addLast(key); + data.put(key, new Lst()); + } + columnCount = keyWords.size(); + if (columnCount == 0) + return; + isLoop = true; + while (getData()) + for (int i = 0; i < columnCount; i++) + ((Lst)data.get(keyWords.get(i))).addLast(columnData[i]); + isLoop = false; + } + + @Override + public String readLine() { + try { + line = (reader == null ? br.readLine() : reader.readNextLine()); + if (line == null) + return null; + if (isHeader) { + if (line.startsWith("#")) + fileHeader.append(line).appendC('\n'); + else + isHeader = false; + } + return line; + } catch (Exception e) { + return null; + } + } + + /** + * The work horse; a general reader for loop data. Fills colunnData with + * fieldCount fields. + * + * @return false if EOF + * @throws Exception + */ + @Override + public boolean getData() throws Exception { + // line is already present, and we leave with the next line to parse + if (isLoop) { + for (int i = 0; i < columnCount; ++i) + if ((columnData[i] = getNextDataToken()) == null) + return false; + } else if (haveData) { + haveData = false; + } else { + return false; + } + return (columnCount > 0); + } + + /** + * + * Skips all associated loop data. (Skips to next control word.) + * + * @throws Exception + */ + @Override + public String skipLoop(boolean doReport) throws Exception { + String str; + SB ret = (doReport ? new SB() : null); + int n = 0; + while ((str = (String) peekToken()) != null && str.charAt(0) == '_') { + if (ret != null) + ret.append(str).append("\n"); + getTokenPeeked(); + n++; + } + if (n == 0) + n = columnCount; // end-of-label-section skip + int m = 0; + while ((str = (String) getNextDataToken()) != null) { + if (ret == null) + continue; + ret.append(str).append(" "); + if ((++m % n) == 0) + ret.append("\n"); + } + return (ret == null ? null : ret.toString()); + } + + /** + * Get a token as a String value (for the reader) + * + * @return the next token of any kind, or null + * @throws Exception + */ + @Override + public String getNextToken() throws Exception { + wasUnquoted = true; + return (String) getNextTokenProtected(); + } + + /** + * Get the token as a Java Object + * + * @return the next token of any kind, or null + * @throws Exception + */ + public Object getNextTokenObject() throws Exception { + wasUnquoted = true; + return getNextTokenProtected(); + } + + /** + * Just makes sure + * @return String from buffer. + * @throws Exception + */ + protected Object getNextTokenProtected() throws Exception { + return (getNextLine() ? nextStrToken() : null); + } + + /** + * + * first checks to see if the next token is an unquoted + * control code, and if so, returns null + * + * @return next data token or null + * @throws Exception + */ + @Override + public Object getNextDataToken() throws Exception { + Object o = peekToken(); + if (o == null) + return null; + if (wasUnquoted && o instanceof String) { + String str = (String) o; + if (str.charAt(0) == '_' || str.startsWith("loop_") + || str.startsWith("data_") + || str.startsWith("save_") + || str.startsWith("stop_") + || str.startsWith("global_")) + return null; + } + return getTokenPeeked(); + } + + /** + * Just look at the next token. Saves it for retrieval + * using getTokenPeeked() + * + * @return next token or null if EOF + * @throws Exception + */ + @Override + public Object peekToken() throws Exception { + if (!getNextLine()) + return null; + int ich = this.ich; + strPeeked = nextStrToken(); + ichPeeked= this.ich; + this.ich = ich; + return strPeeked; + } + + /** + * grab a new line if necessary and prepare it + * if it starts with ";" + * + * @return updated this.str + * @throws Exception + */ + private boolean getNextLine() throws Exception { + while (!strHasMoreTokens()) + if (prepareNextLine() == null) + return false; + return true; + } + + /** + * + * @return the token last acquired; may be null + */ + @Override + public Object getTokenPeeked() { + ich = ichPeeked; + return strPeeked; + } + + /** + * Used especially for data that might be multi-line data that + * might have unwanted white space at start or end. + * + * @param str + * @return str without any leading/trailing white space, and no '\n' + */ + @Override + public String fullTrim(String str) { + int pt0 = -1; + int pt1 = str.length(); + while (++pt0 < pt1 && PT.isWhitespace(str.charAt(pt0))) { + } + while (--pt1 > pt0 && PT.isWhitespace(str.charAt(pt1))) { + } + return str.substring(pt0, pt1 + 1); + } + + private final static String grABC = + "ABX\u0394E\u03A6\u0393H" // ABCDEFGH + + "I_K\u039BMNO\u03A0" // I_KLMNOP + + "\u0398P\u03A3TY_\u03A9\u039E\u03A5Z"; // QRSTU_WXYZ + private final static String grabc = + "\u03B1\u03B2\u03C7\u03A4\u03A5\u03C6\u03B3\u03B7" // abcdefgh + + "\u03B9_\u03BA\u03BB\u03BC\u03BD\u03BF\u03C0" // i_klmnop + + "\u03B8\u03C1\u03C3\u03C4\u03C5_\u03C9\u03BE\u03C5\u03B6"; // qrstu_wxyz + + /** + * Only translating the basic Greek set here, not all the other stuff. See + * http://www.iucr.org/resources/cif/spec/version1.1/semantics#markup + * + * @param data + * @return cleaned string + */ + @Override + public String toUnicode(String data) { + int pt; + try { + while ((pt = data.indexOf('\\')) >= 0) { + int c = data.charAt(pt + 1); + String ch = (c >= 65 && c <= 90 ? grABC.substring(c - 65, c - 64) + : c >= 97 && c <= 122 ? grabc.substring(c - 97, c - 96) : "_"); + data = data.substring(0, pt) + ch + data.substring(pt + 2); + } + } catch (Exception e) { + // ignore + } + + return data; + } + + /** + * Process a data block, with or without a loop_. + * + * Passed an array of field names, this method fills two int[] arrays. The + * first, key2col, maps desired key values to actual order of appearance + * (column number) in the file; the second, col2key, is a reverse loop-up for + * that, mapping column numbers to desired field indices. + * + * When called within a loop_ context, this.columnData will be created but not filled. + * + * Alternatively, if fields is null, then this.fieldNames is + * filled, in order, with key data, and both key2col and col2key will be + * simply 0,1,2,... This array is used in cases such as matrices for which + * there are simply too many possibilities to list, and the key name itself + * contains information that we need. + * + * When not a loop_ context, keys are expected to be in the mmCIF form + * category.subkey and will be unique within a data block (see + * http://mmcif.wwpdb.org/docs/tutorials/mechanics/pdbx-mmcif-syntax.html). + * Keys and data will be read for all data in the same category, filling this.columnData. + * + * + * In this way, the calling class does not need to enumerate all possible + * category names, but instead can focus on just those of interest. + * + * + * @param fields + * list of normalized field names, such as + * "_pdbx_struct_assembly_gen_assembly_id" (with "_" instead of ".") + * @param key + * null to indicate a loop_ construct, otherwise the initial category.subkey + * found + * @param data + * when not loop_ the initial data read, otherwise ignored + * @param key2col + * map of desired keys to actual columns + * @param col2key + * map of actual columns to desired keys + * @throws Exception + */ + @Override + public void parseDataBlockParameters(String[] fields, String key, + String data, int[] key2col, int[] col2key) throws Exception { + isLoop = (key == null); + Object o; + String s; + if (fields == null) { + // for reading full list of keys, as for matrices + columnNames = new String[KEY_MAX]; + } else { + if (!htFields.containsKey(fields[0])) + for (int i = fields.length; --i >= 0;) + htFields.put(fields[i], Integer.valueOf(i)); + for (int i = fields.length; --i >= 0;) + key2col[i] = NONE; + } + columnCount = 0; + int pt, i; + if (isLoop) { + while (true) { + o = peekToken(); + if (o == null) { + // we are PREMATURELY done; reset + columnCount = 0; + break; + } + // end of the loop is a new token not starting with underscore + if (!(o instanceof String) || ((String) o).charAt(0) != '_') + break; + + pt = columnCount++; + s = fixKey((String) getTokenPeeked()); + if (fields == null) { + // just make a linear model, saving the list + columnNames[col2key[pt] = key2col[pt] = pt] = s; + continue; + } + Integer iField = htFields.get(s); + i = (iField == null ? NONE : iField.intValue()); + if ((col2key[pt] = i) != NONE) + key2col[i] = pt; + } + } else { + pt = key.indexOf("."); + String str0 = (pt < 0 ? key : key.substring(0, pt + 1)); + while (true) { + // end of the loop is a new token not starting with underscore + pt = columnCount++; + if (key == null) { + key = (String) getTokenPeeked(); + data = getNextToken(); + } + Integer iField = htFields.get(fixKey(key)); + i = (iField == null ? NONE : iField.intValue()); + if ((col2key[pt] = i) != NONE) + columnData[key2col[i] = pt] = data; + if ((o = peekToken()) == null || !(o instanceof String) || !((String) o).startsWith(str0)) + break; + key = null; + } + haveData = (columnCount > 0); + } + } + + @Override + public String fixKey(String key) { + // PRELIMINARY -- BilBao _magnetic + // PRELIMINARY -- Jana2006 + return (PT.rep( + key.startsWith("_magnetic") ? key.substring(9) + : key.startsWith("_jana") ? key.substring(5) + : key, ".", "_").toLowerCase()); + } + + //////////////////// private methods //////////////////// + + + /** + * sets global str and line to be parsed from the beginning + * + * \1 .... \1 indicates an embedded fully escaped data object + * + * @param str new data string + * @return str + */ + protected String setString(String str) { + this.str = line = str; + cch = (str == null ? 0 : str.length()); + ich = 0; + return str; + } + + /* + * http://www.iucr.org/resources/cif/spec/version1.1/cifsyntax + * + * 17. The special sequence of end-of-line followed + * immediately by a semicolon in column one (denoted ";") + * may also be used as a delimiter at the beginning and end + * of a character string comprising a data value. The complete + * bounded string is called a text field, and may be used to + * convey multi-line values. The end-of-line associated with + * the closing semicolon does not form part of the data value. + * Within a multi-line text field, leading white space within + * text lines must be retained as part of the data value; trailing + * white space on a line may however be elided. + * + * 18. A text field delimited by the ; digraph may not + * include a semicolon at the start of a line of text as + * part of its value. + * + * 20. For example, the data value foo may be expressed + * equivalently as an unquoted string foo, as a quoted + * string 'foo' or as a text field + * + *;foo + *; + * + * By contrast the value of the text field + * + *; foo + * bar + *; + * + * is foo bar (where represents an end-of-line); + * the embedded space characters are significant. + * + * + * I (BH) note, however, that we sometimes have: + * + * _some_name + * ; + * the name here + * ; + * + * so this should actually be + * + * ;the name here + * ; + * + * for this, we use fullTrim(); + * + */ + + /** + * + * sets the string for parsing to be from the next line + * when the token buffer is empty, and if ';' is at the + * beginning of that line, extends the string to include + * that full multiline string. Uses \1 to indicate that + * this is a special quotation. + * + * + * + * @return the next line or null if EOF + * @throws Exception + */ + protected String prepareNextLine() throws Exception { + setString(readLine()); + if (line == null || line.length() == 0) + return line; + if (line.charAt(0) == ';') + return preprocessString(); + if (str.startsWith("###non-st#")) + ich = 10; + return line; + } + + /** + * Preprocess the string on a line starting with a semicolon + * to produce a string with a \1 ... \1 segment + * that will be picked up in the next round + * + * @return escaped part with attached extra data + * @throws Exception + */ + protected String preprocessString() throws Exception { + return setString(preprocessSemiString()); + } + + /** + * Encapsulate a multi-line ; .... ; string with \1 ... \1 + * + * CIF 1.0 and CIF 2.0 + * + * @return ecapsulated string + * @throws Exception + */ + protected String preprocessSemiString() throws Exception { + ich = 1; + String str = '\1' + line.substring(1) + '\n'; + while (readLine() != null) { + if (line.startsWith(";")) { + // remove trailing only, and attach rest of next line + str = str.substring(0, str.length() - 1) + + '\1' + line.substring(1); + break; + } + str += line + '\n'; + } + return str; + } + + /** + * @return TRUE if there are more tokens in the line buffer + * + */ + private boolean strHasMoreTokens() { + if (str == null) + return false; + char ch = '#'; + while (ich < cch && ((ch = str.charAt(ich)) == ' ' || ch == '\t')) + ++ich; + return (ich < cch && ch != '#'); + } + + /** + * assume that hasMoreTokens() has been called and that ich is pointing at a + * non-white character. Also sets boolean wasUnQuoted, because we need to know + * if we should be checking for a control keyword. 'loop_' is different from + * just loop_ without the quotes. + * + * @return null if no more tokens, "\0" if '.' or '?', or next token + */ + private Object nextStrToken() { + if (ich == cch) + return null; + char ch = str.charAt(ich); + if (isQuote(ch)) { + wasUnquoted = false; + return getQuotedStringOrObject(ch); + } + int ichStart = ich; + wasUnquoted = true; + while (ich < cch && !isTerminator(ch = str.charAt(ich))) + ++ich; + if (ich == ichStart + 1) + if (nullString != null + && (str.charAt(ichStart) == '.' || str.charAt(ichStart) == '?')) + return nullString; + String s = str.substring(ichStart, ich); + return unquoted(s); + } + + /** + * In CIF 2.0, this method turns a String into an Integer or Float + * In CIF 1.0 (here) just return the unchanged value. + * @param s unquoted string + * @return unchanged value + */ + protected Object unquoted(String s) { + return s; + } + + /** + * The token terminator is space or tab in CIF 1.0, + * but it can be quoted strings in CIF 2.0. + * + * @param c + * @return true if this character is a terminator + */ + protected boolean isTerminator(char c) { + return c == ' ' || c == '\t' || c == cterm ; + } + + /** + * CIF 1.0 only; we handle various quote types here + * @param ch + * @return true if this character is a (starting) quote + */ + protected boolean isQuote(char ch) { + switch (ch) { + case '\'': + case '\"': + case '\1': + return true; + } + return false; + } + + /** + * CIF 1.0 only. + * + * + * @param ch current character being pointed to + * @return a String data object + */ + protected Object getQuotedStringOrObject(char ch) { + int ichStart = ich; + char chClosingQuote = ch; + boolean wasQuote = false; + while (++ich < cch) { + ch = str.charAt(ich); + // CIF 1.0 rules require that the closing ' or "" be followed by space or tab or EOL + if (wasQuote && (ch == ' ' || ch == '\t')) + break; + wasQuote = (ch == chClosingQuote); + } + int pt1 = ichStart + 1; + int pt2 = ich - 1; + if (ich == cch && !wasQuote) { + // reached the end of the string without finding closing ' + // so take the whole thing. Probably a bad CIF file. + pt1--; + pt2++; + } else { + // throw away the last white character + ++ich; + } + return str.substring(pt1, pt2); + } + + +} \ No newline at end of file diff --git a/src/javajs/util/CompoundDocDirEntry.java b/src/javajs/util/CompoundDocDirEntry.java new file mode 100644 index 0000000..e60b874 --- /dev/null +++ b/src/javajs/util/CompoundDocDirEntry.java @@ -0,0 +1,96 @@ +/* $RCSfile$ + * $Author$ + * $Date$ + * $Revision$ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2011 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package javajs.util; + + +class CompoundDocDirEntry { + + private final CompoundDocument cd; + + /** + * @param compoundDocument + */ + CompoundDocDirEntry(CompoundDocument compoundDocument) { + cd = compoundDocument; + } + + // 128 bytes + //offset 0: + byte[] unicodeName64 = new byte[64]; + short nBytesUnicodeName; // twice the ascii length, including terminating 0 + byte entryType; // 0 empty; 1 storage; 2 stream; 5 root storage + //byte entryColor; // 0 red or 1 black + //int DIDchildLeft; + //int DIDchildRight; + //int DIDstorageRoot; + byte[] uniqueID16 = new byte[16]; + byte[] userflags4 = new byte[4]; + //long timeStamp1; + //long timeStamp2; + //offset 116: + int SIDfirstSector; // either SAT or SSAT + int lenStream; + byte[] unused = new byte[8]; + + // derived: + + String entryName; + boolean isStandard; + boolean isEmpty; + + final boolean readData() { + try { + cd.readByteArray(unicodeName64, 0, 64); + nBytesUnicodeName = cd.readShort(); + entryType = cd.readByte(); + /*entryColor = */cd.readByte(); + /*DIDchildLeft = */cd.readInt(); + /*DIDchildRight = */cd.readInt(); + /*DIDstorageRoot = */cd.readInt(); + cd.readByteArray(uniqueID16, 0, 16); + cd.readByteArray(userflags4, 0, 4); + /*timeStamp1 = */ cd.readByteArray(unused, 0, 8);//cd.readLong(); + /*timeStamp2 = */ cd.readByteArray(unused, 0, 8);//cd.readLong(); + //offset 116: + SIDfirstSector = cd.readInt(); + lenStream = cd.readInt(); + cd.readByteArray(unused, 0, 4); + } catch (Exception e) { + System.out.println(e.toString()); + return false; + } + entryName = ""; + for (int i = 0; i < nBytesUnicodeName - 2; i += 2) + entryName += (char) unicodeName64[i]; + isStandard = (entryType == 5 || lenStream >= cd.header.minBytesStandardStream); + isEmpty = (entryType == 0 || lenStream <= 0); + //System.out.println(entryName + " type " + entryType); + return true; + } +} \ No newline at end of file diff --git a/src/javajs/util/CompoundDocHeader.java b/src/javajs/util/CompoundDocHeader.java new file mode 100644 index 0000000..72317fe --- /dev/null +++ b/src/javajs/util/CompoundDocHeader.java @@ -0,0 +1,115 @@ +/* $RCSfile$ + * $Author$ + * $Date$ + * $Revision$ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2011 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package javajs.util; + + +class CompoundDocHeader { + + /** + * + */ + private final CompoundDocument cd; + + /** + * @param compoundDocument + */ + CompoundDocHeader(CompoundDocument compoundDocument) { + cd = compoundDocument; + } + + //512 bytes + //offset 0: + byte[] magicNumbers = new byte[8]; // D0CF11E0A1B11AE1 + byte[] uniqueID16 = new byte[16]; + byte revNumber; // 3E = 62 + //byte unusedb1; + byte verNumber; // 3 + //byte unusedb2; + //short byteOrder; // -2 littleEndian + short sectorPower; // 2^sectorPower = sector size; 512 = 2^9 + short shortSectorPower; // 2^shortSectorPower = short sector size; 64 = 2^6 + byte[] unused = new byte[10]; + int nSATsectors; // number of sectors for sector allocation table + int SID_DIR_start; // sector identifier of start of directory sector + //offset 56: + int minBytesStandardStream; // less than this (and not DIR) will be "short" + int SID_SSAT_start; // start of short sector allocation table (SSAT) + int nSSATsectors; // number of sectors allocated to SSAT + int SID_MSAT_next; // pointer to next master sector allocation table sector + int nAdditionalMATsectors; // number of sectors allocated to more MSAT sectors + //offset 76; 436 bytes: + int[] MSAT0 = new int[109]; // beginning of master allocation table + + /* + * Sector 0 is first sector AFTER this header + * + * If sectorPower = 9, then this allows for 109 PAGES + * of sector allocation tables, with 127 pointers per + * page (plus 1 pointer to the next SAT page), each + * pointing to a sector of 512 bytes. Thus, with no additional + * MSAT pages, the header allows for 109*128*512 = 7.1 Mb file + * + */ + + final boolean readData() { + try { + cd.readByteArray(magicNumbers, 0, 8); + if ((magicNumbers[0] & 0xFF) != 0xD0 || (magicNumbers[1] & 0xFF) != 0xCF + || (magicNumbers[2] & 0xFF) != 0x11 || (magicNumbers[3] & 0xFF) != 0xE0 + || (magicNumbers[4] & 0xFF) != 0xA1 || (magicNumbers[5] & 0xFF) != 0xB1 + || (magicNumbers[6] & 0xFF) != 0x1A || (magicNumbers[7] & 0xFF) != 0xE1) + return false; + cd.readByteArray(uniqueID16, 0, 16); + revNumber = cd.readByte(); + cd.readByte(); + verNumber = cd.readByte(); + cd.readByte(); + byte b1 = cd.readByte(); + byte b2 = cd.readByte(); + cd.isBigEndian = (b1 == -1 && b2 == -2); + sectorPower = cd.readShort(); + shortSectorPower = cd.readShort(); + cd.readByteArray(unused, 0, 10); + nSATsectors = cd.readInt(); + SID_DIR_start = cd.readInt(); + cd.readByteArray(unused, 0, 4); + minBytesStandardStream = cd.readInt(); + SID_SSAT_start = cd.readInt(); + nSSATsectors = cd.readInt(); + SID_MSAT_next = cd.readInt(); + nAdditionalMATsectors = cd.readInt(); + for (int i = 0; i < 109; i++) + MSAT0[i] = cd.readInt(); + } catch (Exception e) { + System.out.println(e.toString()); + return false; + } + return true; + } +} \ No newline at end of file diff --git a/src/javajs/util/CompoundDocument.java b/src/javajs/util/CompoundDocument.java new file mode 100644 index 0000000..2e2c242 --- /dev/null +++ b/src/javajs/util/CompoundDocument.java @@ -0,0 +1,395 @@ +/* $RCSfile$ + * $Author: egonw $ + * $Date: 2006-03-18 15:59:33 -0600 (Sat, 18 Mar 2006) $ + * $Revision: 4652 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2003-2005 Miguel, Jmol Development, www.jmol.org + * + * Contact: hansonr@stolaf.edu + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.util; + + +import java.io.DataInputStream; +import java.io.BufferedInputStream; + + +import java.util.Map; + +import javajs.api.GenericZipTools; + + + +/* a simple compound document reader. + * + * DIRECTORY STRUCTURE IS NOT REGENERATED + * + * See http://sc.openoffice.org/compdocfileformat.pdf + * + * random access file info: + * http://java.sun.com/docs/books/tutorial/essential/io/rafs.html + * + * SHOOT! random access is only for applications, not applets! + * + * With a bit more work, this could be set up to deliver binary files, but + * right now I've only implemented it for string-based data. + * + */ + +public class CompoundDocument extends BinaryDocument{ + +// RandomAccessFile file; + CompoundDocHeader header = new CompoundDocHeader(this); + Lst directory = new Lst(); + CompoundDocDirEntry rootEntry; + + protected GenericZipTools jzt; + + int[] SAT; + int[] SSAT; + int sectorSize; + int shortSectorSize; + int nShortSectorsPerStandardSector; + int nIntPerSector; + int nDirEntriesperSector; + + // called by reflection + + public CompoundDocument(){ + super(); + this.isBigEndian = true; + } + + public void setDocStream(GenericZipTools jzt, BufferedInputStream bis) { + this.jzt = jzt; + if (!isRandom) { + stream = new DataInputStream(bis); + } + stream.mark(Integer.MAX_VALUE); + if (!readHeader()) + return; + getSectorAllocationTable(); + getShortSectorAllocationTable(); + getDirectoryTable(); + } + + public Lst getDirectory() { + return directory; + } + + public String getDirectoryListing(String separator) { + SB sb = new SB(); + for (int i = 0; i < directory.size(); i++) { + CompoundDocDirEntry thisEntry = directory.get(i); + if (!thisEntry.isEmpty) + sb.append(separator).append(thisEntry.entryName) + .append("\tlen=").appendI(thisEntry.lenStream) + .append("\tSID=").appendI(thisEntry.SIDfirstSector) + .append(thisEntry.isStandard ? "\tfileOffset=" + + getOffset(thisEntry.SIDfirstSector) : ""); + } + return sb.toString(); + } + + public SB getAllData() { + return getAllDataFiles(null, null); + } + + /** + * reads a compound document directory and saves all data in a Hashtable + * so that the files may be organized later in a different order. Also adds + * a #Directory_Listing entry. + * + * Files are bracketed by BEGIN Directory Entry and END Directory Entry lines, + * similar to ZipUtil.getAllData. + * + * @param prefix + * @param binaryFileList |-separated list of files that should be saved + * as xx xx xx hex byte strings. The directory listing + * is appended with ":asBinaryString" + * @param fileData + */ + @Override + public void getAllDataMapped(String prefix, + String binaryFileList, Map fileData) { + fileData.put("#Directory_Listing", getDirectoryListing("|")); + binaryFileList = "|" + binaryFileList + "|"; + for (int i = 0; i < directory.size(); i++) { + CompoundDocDirEntry thisEntry = directory.get(i); + if (!thisEntry.isEmpty && thisEntry.entryType != 5) { + String name = thisEntry.entryName; + System.out.println("CompoundDocument file " + name); + boolean isBinary = (binaryFileList.indexOf("|" + name + "|") >= 0); + if (isBinary) + name += ":asBinaryString"; + fileData.put(prefix + "/" + name, appendData(new SB(), name, thisEntry, isBinary).toString()); + } + } + close(); + } + + @Override + public SB getAllDataFiles(String binaryFileList, String firstFile) { +// firstFile is now ignored +// if (firstFile != null) { +// for (int i = 0; i < directory.size(); i++) { +// CompoundDocDirEntry thisEntry = directory.get(i); +// if (thisEntry.entryName.equals(firstFile)) { +// directory.remove(i); +// directory.add(1, thisEntry); // after ROOT_ENTRY +// break; +// } +// } +// } + SB data = new SB(); + data.append("Compound Document File Directory: "); + data.append(getDirectoryListing("|")); + data.append("\n"); + CompoundDocDirEntry thisEntry; + binaryFileList = "|" + binaryFileList + "|"; + for (int i = 0, n = directory.size(); i < n; i++) { + thisEntry = directory.get(i); + //System.out.println("CompoundDocument reading " + thisEntry.entryName); + String name = thisEntry.entryName; + switch (thisEntry.entryType) { + case 5: // root + break; + case 1: // user storage (dir) + data.append("NEW Directory ").append(name).append("\n"); + break; + case 2: // user stream (file) + if (name.endsWith(".gz")) + name = name.substring(0, name.length() - 3); + appendData(data, name, thisEntry, binaryFileList.indexOf("|" + thisEntry.entryName + "|") >= 0); + break; + } + } + close(); + return data; + } + + private SB appendData(SB data, String name, CompoundDocDirEntry thisEntry, + boolean isBinary) { + data.append("BEGIN Directory Entry ").append(name).append("\n"); + data.appendSB(getEntryAsString(thisEntry, isBinary)); + data.append("\nEND Directory Entry ").append(name).append("\n"); + return data; + } + + public SB getFileAsString(String entryName) { + for (int i = 0; i < directory.size(); i++) { + CompoundDocDirEntry thisEntry = directory.get(i); + if (thisEntry.entryName.equals(entryName)) + return getEntryAsString(thisEntry, false); + } + return new SB(); + } + + private long getOffset(int SID) { + return (SID + 1) * sectorSize; + } + + private void gotoSector(int SID) { + seek(getOffset(SID)); + } + + private boolean readHeader() { + if (!header.readData()) + return false; + sectorSize = 1 << header.sectorPower; + shortSectorSize = 1 << header.shortSectorPower; + nShortSectorsPerStandardSector = sectorSize / shortSectorSize; // e.g. 512 / 64 = 8 + nIntPerSector = sectorSize / 4; // e.g. 512 / 4 = 128 + nDirEntriesperSector = sectorSize / 128; // e.g. 512 / 128 = 4 +// System.out.println( +// "compound document: revNum=" + header.revNumber + +// " verNum=" + header.verNumber + " isBigEndian=" + isBigEndian + +// " bytes per standard/short sector=" + sectorSize + "/" + shortSectorSize); + return true; + } + + private void getSectorAllocationTable() { + int nSID = 0; + int thisSID; + SAT = new int[header.nSATsectors * nIntPerSector + 109]; + + try { + for (int i = 0; i < 109; i++) { + thisSID = header.MSAT0[i]; + if (thisSID < 0) + break; + gotoSector(thisSID); + for (int j = 0; j < nIntPerSector; j++) { + SAT[nSID++] = readInt(); + //Logger.debug(thisSID+"."+j + "/" + (nSID - 1) + " : " + SAT[nSID - 1]); + } + } + int nMaster = header.nAdditionalMATsectors; + thisSID = header.SID_MSAT_next; + int[] MSAT = new int[nIntPerSector]; + out: while (nMaster-- > 0 && thisSID >= 0) { + // read a page of sector identifiers pointing to SAT sectors + gotoSector(thisSID); + for (int i = 0; i < nIntPerSector; i++) + MSAT[i] = readInt(); + // read each page of SAT sector identifiers + // last entry is pointer to next master sector allocation table page + for (int i = 0; i < nIntPerSector - 1; i++) { + thisSID = MSAT[i]; + if (thisSID < 0) + break out; + gotoSector(thisSID); + for (int j = nIntPerSector; --j >= 0;) + SAT[nSID++] = readInt(); + } + thisSID = MSAT[nIntPerSector - 1]; + } + } catch (Exception e) { + System.out.println(e.toString()); + } + } + + private void getShortSectorAllocationTable() { + int nSSID = 0; + int thisSID = header.SID_SSAT_start; + int nMax = header.nSSATsectors * nIntPerSector; + SSAT = new int[nMax]; + try { + while (thisSID > 0 && nSSID < nMax) { + gotoSector(thisSID); + for (int j = 0; j < nIntPerSector; j++) { + SSAT[nSSID++] = readInt(); + //System.out.println("short: " + thisSID+"."+j+" SSID=" +(nSSID-1)+" "+SSAT[nSSID-1]); + } + thisSID = SAT[thisSID]; + } + } catch (Exception e) { + System.out.println(e.toString()); + } + } + + private void getDirectoryTable() { + int thisSID = header.SID_DIR_start; + CompoundDocDirEntry thisEntry; + rootEntry = null; + try { + while (thisSID > 0) { + gotoSector(thisSID); + for (int j = nDirEntriesperSector; --j >= 0;) { + thisEntry = new CompoundDocDirEntry(this); + thisEntry.readData(); + directory.addLast(thisEntry); + if (thisEntry.entryType == 5) + rootEntry = thisEntry; + } + thisSID = SAT[thisSID]; + } + } catch (Exception e) { + System.out.println(e.toString()); + } +// System.out.println("CompoundDocument directory entry: \n" +// + getDirectoryListing("\n")); + } + + private SB getEntryAsString(CompoundDocDirEntry thisEntry, boolean asBinaryString) { + if(thisEntry.isEmpty) + return new SB(); + //System.out.println(thisEntry.entryName + " " + thisEntry.entryType + " " + thisEntry.lenStream + " " + thisEntry.isStandard + " " + thisEntry.SIDfirstSector); + return (thisEntry.isStandard ? getStandardStringData( + thisEntry.SIDfirstSector, thisEntry.lenStream, asBinaryString) + : getShortStringData(thisEntry.SIDfirstSector, thisEntry.lenStream, asBinaryString)); + } + private SB getStandardStringData(int thisSID, int nBytes, + boolean asBinaryString) { + SB data = new SB(); + byte[] byteBuf = new byte[sectorSize]; + ZipData gzipData = new ZipData(nBytes); + try { + while (thisSID > 0 && nBytes > 0) { + gotoSector(thisSID); + nBytes = getSectorData(data, byteBuf, sectorSize, nBytes, asBinaryString, gzipData); + thisSID = SAT[thisSID]; + } + if (nBytes == -9999) + return new SB(); + } catch (Exception e) { + System.out.println(e.toString()); + } + if (gzipData.isEnabled) + gzipData.addTo(jzt, data); + return data; + } + + private int getSectorData(SB data, byte[] byteBuf, + int nSectorBytes, int nBytes, + boolean asBinaryString, ZipData gzipData) + throws Exception { + readByteArray(byteBuf, 0, byteBuf.length); + int n = gzipData.addBytes(byteBuf, nSectorBytes, nBytes); + if (n >= 0) + return n; + if (asBinaryString) { + for (int i = 0; i < nSectorBytes; i++) { + data.append(Integer.toHexString(byteBuf[i] & 0xFF)).appendC(' '); + if (--nBytes < 1) + break; + } + } else { + for (int i = 0; i < nSectorBytes; i++) { + if (byteBuf[i] == 0) + return -9999; // don't allow binary data + data.appendC((char) byteBuf[i]); + if (--nBytes < 1) + break; + } + } + return nBytes; + } + + private SB getShortStringData(int shortSID, int nBytes, boolean asBinaryString) { + SB data = new SB(); + if (rootEntry == null) + return data; + int thisSID = rootEntry.SIDfirstSector; + int ptShort = 0; + byte[] byteBuf = new byte[shortSectorSize]; + ZipData gzipData = new ZipData(nBytes); + try { + //System.out.println("CD shortSID=" + shortSID); + // point to correct short data sector, 512/64 = 4 per page + while (thisSID >= 0 && shortSID >= 0 && nBytes > 0) { + while (shortSID - ptShort >= nShortSectorsPerStandardSector) { + ptShort += nShortSectorsPerStandardSector; + thisSID = SAT[thisSID]; + } + seek(getOffset(thisSID) + (shortSID - ptShort) * shortSectorSize); + nBytes = getSectorData(data, byteBuf, shortSectorSize, nBytes, asBinaryString, gzipData); + shortSID = SSAT[shortSID]; + //System.out.println("CD shortSID=" + shortSID); + } + } catch (Exception e) { + System.out.println(data.toString()); + System.out.println("reader error in CompoundDocument " + e.toString()); + } + if (gzipData.isEnabled) + gzipData.addTo(jzt, data); + return data; + } +} diff --git a/src/javajs/util/DF.java b/src/javajs/util/DF.java new file mode 100644 index 0000000..1d8a2f1 --- /dev/null +++ b/src/javajs/util/DF.java @@ -0,0 +1,164 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2007-04-26 16:57:51 -0500 (Thu, 26 Apr 2007) $ + * $Revision: 7502 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package javajs.util; + +/** + * created to remove ambiguities and make a simpler DecimalFormat + */ +public class DF { + + private final static String[] formattingStrings = { "0", "0.0", "0.00", + "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000", + "0.000000000" }; + private final static String zeros = "0000000000000000000000000000000000000000"; + + private final static float[] formatAdds = { 0.5f, 0.05f, 0.005f, 0.0005f, + 0.00005f, 0.000005f, 0.0000005f, 0.00000005f, 0.000000005f, 0.0000000005f }; + + private final static Boolean[] useNumberLocalization = new Boolean[] { Boolean.TRUE }; + + public static void setUseNumberLocalization(boolean TF) { + useNumberLocalization[0] = (TF ? Boolean.TRUE : Boolean.FALSE); + } + + public static String formatDecimalDbl(double value, int decimalDigits) { + if (decimalDigits == Integer.MAX_VALUE + || value == Double.NEGATIVE_INFINITY + || value == Double.POSITIVE_INFINITY + || Double.isNaN(value)) + return "" + value; + return DF.formatDecimal((float) value, decimalDigits); + } + + /** + * a simple alternative to DecimalFormat (which Java2Script does not have + * and which is quite too complex for our use here.) + * + * @param value + * @param decimalDigits + * @return formatted decimal + */ + public static String formatDecimal(float value, int decimalDigits) { + if (decimalDigits == Integer.MAX_VALUE + || value == Float.NEGATIVE_INFINITY || value == Float.POSITIVE_INFINITY || Float.isNaN(value)) + return "" + value; + int n; + if (decimalDigits < 0) { + decimalDigits = -decimalDigits; + if (decimalDigits > formattingStrings.length) + decimalDigits = formattingStrings.length; + if (value == 0) + return formattingStrings[decimalDigits] + "E+0"; + //scientific notation + n = 0; + double d; + if (Math.abs(value) < 1) { + n = 10; + d = value * 1e-10; + } else { + n = -10; + d = value * 1e10; + } + String s = ("" + d).toUpperCase(); + int i = s.indexOf("E"); + n = PT.parseInt(s.substring(i + 1)) + n; + return (i < 0 ? "" + value : formatDecimal(PT.parseFloat(s.substring( + 0, i)), decimalDigits - 1) + + "E" + (n >= 0 ? "+" : "") + n); + } + + if (decimalDigits >= formattingStrings.length) + decimalDigits = formattingStrings.length - 1; + String s1 = ("" + value).toUpperCase(); + int pt = s1.indexOf("."); + if (pt < 0) // specifically JavaScript "-2" not "-2.0" + return s1 + formattingStrings[decimalDigits].substring(1); + boolean isNeg = s1.startsWith("-"); + if (isNeg) { + s1 = s1.substring(1); + pt--; + } + int pt1 = s1.indexOf("E-"); + if (pt1 > 0) { + n = PT.parseInt(s1.substring(pt1 + 1)); + // 3.567E-2 + // 0.03567 + s1 = "0." + zeros.substring(0, -n - 1) + s1.substring(0, 1) + s1.substring(2, pt1); + pt = 1; + } + + pt1 = s1.indexOf("E"); + // 3.5678E+3 + // 3567.800000000 + // 1.234E10 %3.8f -> 12340000000.00000000 + if (pt1 > 0) { + n = PT.parseInt(s1.substring(pt1 + 1)); + s1 = s1.substring(0, 1) + s1.substring(2, pt1) + zeros; + s1 = s1.substring(0, n + 1) + "." + s1.substring(n + 1); + pt = s1.indexOf("."); + } + // "234.345667 len == 10; pt = 3 + // " 0.0 " decimalDigits = 1 + + int len = s1.length(); + int pt2 = decimalDigits + pt + 1; + if (pt2 < len && s1.charAt(pt2) >= '5') { + return formatDecimal( + value + (isNeg ? -1 : 1) * formatAdds[decimalDigits], decimalDigits); + } + + SB sb = SB.newS(s1.substring(0, (decimalDigits == 0 ? pt + : ++pt))); + for (int i = 0; i < decimalDigits; i++, pt++) { + if (pt < len) + sb.appendC(s1.charAt(pt)); + else + sb.appendC('0'); + } + s1 = (isNeg ? "-" : "") + sb; + return (Boolean.TRUE.equals(useNumberLocalization[0]) ? s1 : s1.replace(',', + '.')); + } + + /** + * an alternative to DecimalFormat "0.#" + * + * @param x + * @param precision + * @return formatted number + */ + public static String formatDecimalTrimmed(double x, int precision) { + String str = formatDecimalDbl(x, precision); + int m = str.length() - 1; + char zero = '0'; + while (m >= 0 && str.charAt(m) == zero) + m--; + return str.substring(0, m + 1); // 0.##... + } + +} diff --git a/src/javajs/util/DataReader.java b/src/javajs/util/DataReader.java new file mode 100644 index 0000000..1eb8ade --- /dev/null +++ b/src/javajs/util/DataReader.java @@ -0,0 +1,56 @@ +package javajs.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +/** + * Just a simple abstract class to join a String reader and a String[] + * reader under the same BufferedReader umbrella. + * + * Subclassed as StringDataReader, ArrayDataReader, and ListDataReader + * + */ + +public abstract class DataReader extends BufferedReader { + + public abstract DataReader setData(Object data); + + protected int ptMark; + + public DataReader() { + super(new StringReader("")); + } + + protected DataReader(Reader in) { + super(in); + } + + public BufferedReader getBufferedReader() { + return this; + } + + protected int readBuf(char[] buf, int off, int len) throws IOException { + // not used by StringDataReader + int nRead = 0; + String line = readLine(); + if (line == null) + return 0; + int linept = 0; + int linelen = line.length(); + for (int i = off; i < len && linelen >= 0; i++) { + if (linept >= linelen) { + linept = 0; + buf[i] = '\n'; + line = readLine(); + linelen = (line == null ? -1 : line.length()); + } else { + buf[i] = line.charAt(linept++); + } + nRead++; + } + return nRead; + } + +} \ No newline at end of file diff --git a/src/javajs/util/DebugJS.java b/src/javajs/util/DebugJS.java new file mode 100644 index 0000000..ab22558 --- /dev/null +++ b/src/javajs/util/DebugJS.java @@ -0,0 +1,28 @@ +package javajs.util; + +/** + * A class to insert a JavaScript debug statement for Firefox or Chrome debugger + * + */ + +public class DebugJS { + + /** + * Insert a JavaScript debug statement + * + */ + public static void _(String msg) { + /** + * @j2sNative + * + * if (Clazz._debugging) { + * + * debugger; + * + * } + * + */ + {} + } + +} diff --git a/src/javajs/util/Eigen.java b/src/javajs/util/Eigen.java new file mode 100644 index 0000000..52736c0 --- /dev/null +++ b/src/javajs/util/Eigen.java @@ -0,0 +1,1063 @@ +/* $RCSfile$ + * $Author: egonw $ + * $Date: 2005-11-10 09:52:44f -0600 (Thu, 10 Nov 2005) $ + * $Revision: 4255 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2003-2005 Miguel, Jmol Development, www.jmol.org + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package javajs.util; + +import javajs.api.EigenInterface; + + +/** + * Eigenvalues and eigenvectors of a real matrix. + * See javajs.api.EigenInterface() as well. + * + * adapted by Bob Hanson from http://math.nist.gov/javanumerics/jama/ (public + * domain); adding quaternion superimposition capability; removing + * nonsymmetric reduction to Hessenberg form, which we do not need in Jmol. + * + * Output is as a set of double[n] columns, but for the EigenInterface + * we return them as V3[3] and float[3] (or double[3]) values. + * + * Eigenvalues and eigenvectors are sorted from smallest to largest eigenvalue. + * + *

+ * If A is symmetric, then A = V*D*V' where the eigenvalue matrix D is diagonal + * and the eigenvector matrix V is orthogonal. I.e. A = + * V.times(D.times(V.transpose())) and V.times(V.transpose()) equals the + * identity matrix. + *

+ * If A is not symmetric, then the eigenvalue matrix D is block diagonal with + * the real eigenvalues in 1-by-1 blocks and any complex eigenvalues, lambda + + * i*mu, in 2-by-2 blocks, [lambda, mu; -mu, lambda]. The columns of V represent + * the eigenvectors in the sense that A*V = V*D, i.e. A.times(V) equals + * V.times(D). The matrix V may be badly conditioned, or even singular, so the + * validity of the equation A = V*D*inverse(V) depends upon V.cond(). + **/ + +public class Eigen implements EigenInterface { + + /* ------------------------ + Public Methods + * ------------------------ */ + + public Eigen() {} + + public Eigen set(int n) { + this.n = n; + V = new double[n][n]; + d = new double[n]; + e = new double[n]; + return this; + } + + @Override + public Eigen setM(double[][] m) { + set(m.length); + calc(m); + return this; + } + + /** + * return values sorted from smallest to largest value. + */ + @Override + public double[] getEigenvalues() { + return d; + } + + /** + * Specifically for 3x3 systems, returns eigenVectors as V3[3] + * and values as float[3]; sorted from smallest to largest value. + * + * @param eigenVectors returned vectors + * @param eigenValues returned values + * + */ + @Override + public void fillFloatArrays(V3[] eigenVectors, float[] eigenValues) { + for (int i = 0; i < 3; i++) { + if (eigenVectors != null) { + if (eigenVectors[i] == null) + eigenVectors[i] = new V3(); + eigenVectors[i].set((float) V[0][i], (float) V[1][i], (float) V[2][i]); + } + if (eigenValues != null) + eigenValues[i] = (float) d[i]; + } + } + + /** + * Transpose V and turn into floats; sorted from smallest to largest value. + * + * @return ROWS of eigenvectors f[0], f[1], f[2], etc. + */ + @Override + public float[][] getEigenvectorsFloatTransposed() { + float[][] f = new float[n][n]; + for (int i = n; --i >= 0;) + for (int j = n; --j >= 0;) + f[j][i] = (float) V[i][j]; + return f; + } + + + /** + * Check for symmetry, then construct the eigenvalue decomposition + * + * @param A + * Square matrix + */ + + public void calc(double[][] A) { + + /* Jmol only has need of symmetric solutions + * + issymmetric = true; + + for (int j = 0; (j < n) & issymmetric; j++) { + for (int i = 0; (i < n) & issymmetric; i++) { + issymmetric = (A[i][j] == A[j][i]); + } + } + + if (issymmetric) { + */ + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + V[i][j] = A[i][j]; + } + } + + // Tridiagonalize. + tred2(); + + // Diagonalize. + tql2(); + /* + } else { + H = new double[n][n]; + ort = new double[n]; + + for (int j = 0; j < n; j++) { + for (int i = 0; i < n; i++) { + H[i][j] = A[i][j]; + } + } + + // Reduce to Hessenberg form. + orthes(); + + // Reduce Hessenberg to real Schur form. + hqr2(); + } + */ + + } + + /** + * Return the real parts of the eigenvalues + * + * @return real(diag(D)) + */ + + public double[] getRealEigenvalues() { + return d; + } + + /** + * Return the imaginary parts of the eigenvalues + * + * @return imag(diag(D)) + */ + + public double[] getImagEigenvalues() { + return e; + } + + /* ------------------------ + Class variables + * ------------------------ */ + + /** + * Row and column dimension (square matrix). + * + * @serial matrix dimension. + */ + private int n = 3; + + /** + * Symmetry flag. + * + * @serial internal symmetry flag. + */ + //private boolean issymmetric = true; + + /** + * Arrays for internal storage of eigenvalues. + * + * @serial internal storage of eigenvalues. + */ + private double[] d, e; + + /** + * Array for internal storage of eigenvectors. + * + * @serial internal storage of eigenvectors. + */ + private double[][] V; + + /** + * Array for internal storage of nonsymmetric Hessenberg form. + * + * @serial internal storage of nonsymmetric Hessenberg form. + */ + //private double[][] H; + + /** + * Working storage for nonsymmetric algorithm. + * + * @serial working storage for nonsymmetric algorithm. + */ + //private double[] ort; + + /* ------------------------ + Private Methods + * ------------------------ */ + + // Symmetric Householder reduction to tridiagonal form. + + private void tred2() { + + // This is derived from the Algol procedures tred2 by + // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for + // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding + // Fortran subroutine in EISPACK. + + for (int j = 0; j < n; j++) { + d[j] = V[n - 1][j]; + } + + // Householder reduction to tridiagonal form. + + for (int i = n - 1; i > 0; i--) { + + // Scale to avoid under/overflow. + + double scale = 0.0; + double h = 0.0; + for (int k = 0; k < i; k++) { + scale = scale + Math.abs(d[k]); + } + if (scale == 0.0) { + e[i] = d[i - 1]; + for (int j = 0; j < i; j++) { + d[j] = V[i - 1][j]; + V[i][j] = 0.0; + V[j][i] = 0.0; + } + } else { + + // Generate Householder vector. + + for (int k = 0; k < i; k++) { + d[k] /= scale; + h += d[k] * d[k]; + } + double f = d[i - 1]; + double g = Math.sqrt(h); + if (f > 0) { + g = -g; + } + e[i] = scale * g; + h = h - f * g; + d[i - 1] = f - g; + for (int j = 0; j < i; j++) { + e[j] = 0.0; + } + + // Apply similarity transformation to remaining columns. + + for (int j = 0; j < i; j++) { + f = d[j]; + V[j][i] = f; + g = e[j] + V[j][j] * f; + for (int k = j + 1; k <= i - 1; k++) { + g += V[k][j] * d[k]; + e[k] += V[k][j] * f; + } + e[j] = g; + } + f = 0.0; + for (int j = 0; j < i; j++) { + e[j] /= h; + f += e[j] * d[j]; + } + double hh = f / (h + h); + for (int j = 0; j < i; j++) { + e[j] -= hh * d[j]; + } + for (int j = 0; j < i; j++) { + f = d[j]; + g = e[j]; + for (int k = j; k <= i - 1; k++) { + V[k][j] -= (f * e[k] + g * d[k]); + } + d[j] = V[i - 1][j]; + V[i][j] = 0.0; + } + } + d[i] = h; + } + + // Accumulate transformations. + + for (int i = 0; i < n - 1; i++) { + V[n - 1][i] = V[i][i]; + V[i][i] = 1.0; + double h = d[i + 1]; + if (h != 0.0) { + for (int k = 0; k <= i; k++) { + d[k] = V[k][i + 1] / h; + } + for (int j = 0; j <= i; j++) { + double g = 0.0; + for (int k = 0; k <= i; k++) { + g += V[k][i + 1] * V[k][j]; + } + for (int k = 0; k <= i; k++) { + V[k][j] -= g * d[k]; + } + } + } + for (int k = 0; k <= i; k++) { + V[k][i + 1] = 0.0; + } + } + for (int j = 0; j < n; j++) { + d[j] = V[n - 1][j]; + V[n - 1][j] = 0.0; + } + V[n - 1][n - 1] = 1.0; + e[0] = 0.0; + } + + // Symmetric tridiagonal QL algorithm. + + private void tql2() { + + // This is derived from the Algol procedures tql2, by + // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for + // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding + // Fortran subroutine in EISPACK. + + for (int i = 1; i < n; i++) { + e[i - 1] = e[i]; + } + e[n - 1] = 0.0; + + double f = 0.0; + double tst1 = 0.0; + double eps = Math.pow(2.0, -52.0); + for (int l = 0; l < n; l++) { + + // Find small subdiagonal element + + tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l])); + int m = l; + while (m < n) { + if (Math.abs(e[m]) <= eps * tst1) { + break; + } + m++; + } + + // If m == l, d[l] is an eigenvalue, + // otherwise, iterate. + + if (m > l) { + int iter = 0; + do { + iter = iter + 1; // (Could check iteration count here.) + + // Compute implicit shift + + double g = d[l]; + double p = (d[l + 1] - g) / (2.0 * e[l]); + double r = hypot(p, 1.0); + if (p < 0) { + r = -r; + } + d[l] = e[l] / (p + r); + d[l + 1] = e[l] * (p + r); + double dl1 = d[l + 1]; + double h = g - d[l]; + for (int i = l + 2; i < n; i++) { + d[i] -= h; + } + f = f + h; + + // Implicit QL transformation. + + p = d[m]; + double c = 1.0; + double c2 = c; + double c3 = c; + double el1 = e[l + 1]; + double s = 0.0; + double s2 = 0.0; + for (int i = m - 1; i >= l; i--) { + c3 = c2; + c2 = c; + s2 = s; + g = c * e[i]; + h = c * p; + r = hypot(p, e[i]); + e[i + 1] = s * r; + s = e[i] / r; + c = p / r; + p = c * d[i] - s * g; + d[i + 1] = h + s * (c * g + s * d[i]); + + // Accumulate transformation. + + for (int k = 0; k < n; k++) { + h = V[k][i + 1]; + V[k][i + 1] = s * V[k][i] + c * h; + V[k][i] = c * V[k][i] - s * h; + } + } + p = -s * s2 * c3 * el1 * e[l] / dl1; + e[l] = s * p; + d[l] = c * p; + + // Check for convergence. + + } while (Math.abs(e[l]) > eps * tst1); + } + d[l] = d[l] + f; + e[l] = 0.0; + } + + // Sort eigenvalues and corresponding vectors. + + for (int i = 0; i < n - 1; i++) { + int k = i; + double p = d[i]; + for (int j = i + 1; j < n; j++) { + if (d[j] < p) { + k = j; + p = d[j]; + } + } + if (k != i) { + d[k] = d[i]; + d[i] = p; + for (int j = 0; j < n; j++) { + p = V[j][i]; + V[j][i] = V[j][k]; + V[j][k] = p; + } + } + } + } + + private static double hypot(double a, double b) { + + // sqrt(a^2 + b^2) without under/overflow. + + double r; + if (Math.abs(a) > Math.abs(b)) { + r = b / a; + r = Math.abs(a) * Math.sqrt(1 + r * r); + } else if (b != 0) { + r = a / b; + r = Math.abs(b) * Math.sqrt(1 + r * r); + } else { + r = 0.0; + } + return r; + } + + // Nonsymmetric reduction to Hessenberg form. + + /* + private void orthes() { + + // This is derived from the Algol procedures orthes and ortran, + // by Martin and Wilkinson, Handbook for Auto. Comp., + // Vol.ii-Linear Algebra, and the corresponding + // Fortran subroutines in EISPACK. + + int low = 0; + int high = n - 1; + + for (int m = low + 1; m <= high - 1; m++) { + + // Scale column. + + double scale = 0.0; + for (int i = m; i <= high; i++) { + scale = scale + Math.abs(H[i][m - 1]); + } + if (scale != 0.0) { + + // Compute Householder transformation. + + double h = 0.0; + for (int i = high; i >= m; i--) { + ort[i] = H[i][m - 1] / scale; + h += ort[i] * ort[i]; + } + double g = Math.sqrt(h); + if (ort[m] > 0) { + g = -g; + } + h = h - ort[m] * g; + ort[m] = ort[m] - g; + + // Apply Householder similarity transformation + // H = (I-u*u'/h)*H*(I-u*u')/h) + + for (int j = m; j < n; j++) { + double f = 0.0; + for (int i = high; i >= m; i--) { + f += ort[i] * H[i][j]; + } + f = f / h; + for (int i = m; i <= high; i++) { + H[i][j] -= f * ort[i]; + } + } + + for (int i = 0; i <= high; i++) { + double f = 0.0; + for (int j = high; j >= m; j--) { + f += ort[j] * H[i][j]; + } + f = f / h; + for (int j = m; j <= high; j++) { + H[i][j] -= f * ort[j]; + } + } + ort[m] = scale * ort[m]; + H[m][m - 1] = scale * g; + } + } + + // Accumulate transformations (Algol's ortran). + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + V[i][j] = (i == j ? 1.0 : 0.0); + } + } + + for (int m = high - 1; m >= low + 1; m--) { + if (H[m][m - 1] != 0.0) { + for (int i = m + 1; i <= high; i++) { + ort[i] = H[i][m - 1]; + } + for (int j = m; j <= high; j++) { + double g = 0.0; + for (int i = m; i <= high; i++) { + g += ort[i] * V[i][j]; + } + // Double division avoids possible underflow + g = (g / ort[m]) / H[m][m - 1]; + for (int i = m; i <= high; i++) { + V[i][j] += g * ort[i]; + } + } + } + } + } + + // Complex scalar division. + + private transient double cdivr, cdivi; + + private void cdiv(double xr, double xi, double yr, double yi) { + double r, d; + if (Math.abs(yr) > Math.abs(yi)) { + r = yi / yr; + d = yr + r * yi; + cdivr = (xr + r * xi) / d; + cdivi = (xi - r * xr) / d; + } else { + r = yr / yi; + d = yi + r * yr; + cdivr = (r * xr + xi) / d; + cdivi = (r * xi - xr) / d; + } + } + + // Nonsymmetric reduction from Hessenberg to real Schur form. + + private void hqr2() { + + // This is derived from the Algol procedure hqr2, + // by Martin and Wilkinson, Handbook for Auto. Comp., + // Vol.ii-Linear Algebra, and the corresponding + // Fortran subroutine in EISPACK. + + // Initialize + + int nn = this.n; + int n = nn - 1; + int low = 0; + int high = nn - 1; + double eps = Math.pow(2.0, -52.0); + double exshift = 0.0; + double p = 0, q = 0, r = 0, s = 0, z = 0, t, w, x, y; + + // Store roots isolated by balanc and compute matrix norm + + double norm = 0.0; + for (int i = 0; i < nn; i++) { + if (i < low || i > high) { + d[i] = H[i][i]; + e[i] = 0.0; + } + for (int j = Math.max(i - 1, 0); j < nn; j++) { + norm = norm + Math.abs(H[i][j]); + } + } + + // Outer loop over eigenvalue index + + int iter = 0; + while (n >= low) { + + // Look for single small sub-diagonal element + + int l = n; + while (l > low) { + s = Math.abs(H[l - 1][l - 1]) + Math.abs(H[l][l]); + if (s == 0.0) { + s = norm; + } + if (Math.abs(H[l][l - 1]) < eps * s) { + break; + } + l--; + } + + // Check for convergence + // One root found + + if (l == n) { + H[n][n] = H[n][n] + exshift; + d[n] = H[n][n]; + e[n] = 0.0; + n--; + iter = 0; + + // Two roots found + + } else if (l == n - 1) { + w = H[n][n - 1] * H[n - 1][n]; + p = (H[n - 1][n - 1] - H[n][n]) / 2.0; + q = p * p + w; + z = Math.sqrt(Math.abs(q)); + H[n][n] = H[n][n] + exshift; + H[n - 1][n - 1] = H[n - 1][n - 1] + exshift; + x = H[n][n]; + + // Real pair + + if (q >= 0) { + if (p >= 0) { + z = p + z; + } else { + z = p - z; + } + d[n - 1] = x + z; + d[n] = d[n - 1]; + if (z != 0.0) { + d[n] = x - w / z; + } + e[n - 1] = 0.0; + e[n] = 0.0; + x = H[n][n - 1]; + s = Math.abs(x) + Math.abs(z); + p = x / s; + q = z / s; + r = Math.sqrt(p * p + q * q); + p = p / r; + q = q / r; + + // Row modification + + for (int j = n - 1; j < nn; j++) { + z = H[n - 1][j]; + H[n - 1][j] = q * z + p * H[n][j]; + H[n][j] = q * H[n][j] - p * z; + } + + // Column modification + + for (int i = 0; i <= n; i++) { + z = H[i][n - 1]; + H[i][n - 1] = q * z + p * H[i][n]; + H[i][n] = q * H[i][n] - p * z; + } + + // Accumulate transformations + + for (int i = low; i <= high; i++) { + z = V[i][n - 1]; + V[i][n - 1] = q * z + p * V[i][n]; + V[i][n] = q * V[i][n] - p * z; + } + + // Complex pair + + } else { + d[n - 1] = x + p; + d[n] = x + p; + e[n - 1] = z; + e[n] = -z; + } + n = n - 2; + iter = 0; + + // No convergence yet + + } else { + + // Form shift + + x = H[n][n]; + y = 0.0; + w = 0.0; + if (l < n) { + y = H[n - 1][n - 1]; + w = H[n][n - 1] * H[n - 1][n]; + } + + // Wilkinson's original ad hoc shift + + if (iter == 10) { + exshift += x; + for (int i = low; i <= n; i++) { + H[i][i] -= x; + } + s = Math.abs(H[n][n - 1]) + Math.abs(H[n - 1][n - 2]); + x = y = 0.75 * s; + w = -0.4375 * s * s; + } + + // MATLAB's new ad hoc shift + + if (iter == 30) { + s = (y - x) / 2.0; + s = s * s + w; + if (s > 0) { + s = Math.sqrt(s); + if (y < x) { + s = -s; + } + s = x - w / ((y - x) / 2.0 + s); + for (int i = low; i <= n; i++) { + H[i][i] -= s; + } + exshift += s; + x = y = w = 0.964; + } + } + + iter = iter + 1; // (Could check iteration count here.) + + // Look for two consecutive small sub-diagonal elements + + int m = n - 2; + while (m >= l) { + z = H[m][m]; + r = x - z; + s = y - z; + p = (r * s - w) / H[m + 1][m] + H[m][m + 1]; + q = H[m + 1][m + 1] - z - r - s; + r = H[m + 2][m + 1]; + s = Math.abs(p) + Math.abs(q) + Math.abs(r); + p = p / s; + q = q / s; + r = r / s; + if (m == l) { + break; + } + if (Math.abs(H[m][m - 1]) * (Math.abs(q) + Math.abs(r)) < eps + * (Math.abs(p) * (Math.abs(H[m - 1][m - 1]) + Math.abs(z) + Math + .abs(H[m + 1][m + 1])))) { + break; + } + m--; + } + + for (int i = m + 2; i <= n; i++) { + H[i][i - 2] = 0.0; + if (i > m + 2) { + H[i][i - 3] = 0.0; + } + } + + // Double QR step involving rows l:n and columns m:n + + for (int k = m; k <= n - 1; k++) { + boolean notlast = (k != n - 1); + if (k != m) { + p = H[k][k - 1]; + q = H[k + 1][k - 1]; + r = (notlast ? H[k + 2][k - 1] : 0.0); + x = Math.abs(p) + Math.abs(q) + Math.abs(r); + if (x != 0.0) { + p = p / x; + q = q / x; + r = r / x; + } + } + if (x == 0.0) { + break; + } + s = Math.sqrt(p * p + q * q + r * r); + if (p < 0) { + s = -s; + } + if (s != 0) { + if (k != m) { + H[k][k - 1] = -s * x; + } else if (l != m) { + H[k][k - 1] = -H[k][k - 1]; + } + p = p + s; + x = p / s; + y = q / s; + z = r / s; + q = q / p; + r = r / p; + + // Row modification + + for (int j = k; j < nn; j++) { + p = H[k][j] + q * H[k + 1][j]; + if (notlast) { + p = p + r * H[k + 2][j]; + H[k + 2][j] = H[k + 2][j] - p * z; + } + H[k][j] = H[k][j] - p * x; + H[k + 1][j] = H[k + 1][j] - p * y; + } + + // Column modification + + for (int i = 0; i <= Math.min(n, k + 3); i++) { + p = x * H[i][k] + y * H[i][k + 1]; + if (notlast) { + p = p + z * H[i][k + 2]; + H[i][k + 2] = H[i][k + 2] - p * r; + } + H[i][k] = H[i][k] - p; + H[i][k + 1] = H[i][k + 1] - p * q; + } + + // Accumulate transformations + + for (int i = low; i <= high; i++) { + p = x * V[i][k] + y * V[i][k + 1]; + if (notlast) { + p = p + z * V[i][k + 2]; + V[i][k + 2] = V[i][k + 2] - p * r; + } + V[i][k] = V[i][k] - p; + V[i][k + 1] = V[i][k + 1] - p * q; + } + } // (s != 0) + } // k loop + } // check convergence + } // while (n >= low) + + // Backsubstitute to find vectors of upper triangular form + + if (norm == 0.0) { + return; + } + + for (n = nn - 1; n >= 0; n--) { + p = d[n]; + q = e[n]; + + // Real vector + + if (q == 0) { + int l = n; + H[n][n] = 1.0; + for (int i = n - 1; i >= 0; i--) { + w = H[i][i] - p; + r = 0.0; + for (int j = l; j <= n; j++) { + r = r + H[i][j] * H[j][n]; + } + if (e[i] < 0.0) { + z = w; + s = r; + } else { + l = i; + if (e[i] == 0.0) { + if (w != 0.0) { + H[i][n] = -r / w; + } else { + H[i][n] = -r / (eps * norm); + } + + // Solve real equations + + } else { + x = H[i][i + 1]; + y = H[i + 1][i]; + q = (d[i] - p) * (d[i] - p) + e[i] * e[i]; + t = (x * s - z * r) / q; + H[i][n] = t; + if (Math.abs(x) > Math.abs(z)) { + H[i + 1][n] = (-r - w * t) / x; + } else { + H[i + 1][n] = (-s - y * t) / z; + } + } + + // Overflow control + + t = Math.abs(H[i][n]); + if ((eps * t) * t > 1) { + for (int j = i; j <= n; j++) { + H[j][n] = H[j][n] / t; + } + } + } + } + + // Complex vector + + } else if (q < 0) { + int l = n - 1; + + // Last vector component imaginary so matrix is triangular + + if (Math.abs(H[n][n - 1]) > Math.abs(H[n - 1][n])) { + H[n - 1][n - 1] = q / H[n][n - 1]; + H[n - 1][n] = -(H[n][n] - p) / H[n][n - 1]; + } else { + cdiv(0.0, -H[n - 1][n], H[n - 1][n - 1] - p, q); + H[n - 1][n - 1] = cdivr; + H[n - 1][n] = cdivi; + } + H[n][n - 1] = 0.0; + H[n][n] = 1.0; + for (int i = n - 2; i >= 0; i--) { + double ra, sa, vr, vi; + ra = 0.0; + sa = 0.0; + for (int j = l; j <= n; j++) { + ra = ra + H[i][j] * H[j][n - 1]; + sa = sa + H[i][j] * H[j][n]; + } + w = H[i][i] - p; + + if (e[i] < 0.0) { + z = w; + r = ra; + s = sa; + } else { + l = i; + if (e[i] == 0) { + cdiv(-ra, -sa, w, q); + H[i][n - 1] = cdivr; + H[i][n] = cdivi; + } else { + + // Solve complex equations + + x = H[i][i + 1]; + y = H[i + 1][i]; + vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q; + vi = (d[i] - p) * 2.0 * q; + if (vr == 0.0 & vi == 0.0) { + vr = eps + * norm + * (Math.abs(w) + Math.abs(q) + Math.abs(x) + Math.abs(y) + Math + .abs(z)); + } + cdiv(x * r - z * ra + q * sa, x * s - z * sa - q * ra, vr, vi); + H[i][n - 1] = cdivr; + H[i][n] = cdivi; + if (Math.abs(x) > (Math.abs(z) + Math.abs(q))) { + H[i + 1][n - 1] = (-ra - w * H[i][n - 1] + q * H[i][n]) / x; + H[i + 1][n] = (-sa - w * H[i][n] - q * H[i][n - 1]) / x; + } else { + cdiv(-r - y * H[i][n - 1], -s - y * H[i][n], z, q); + H[i + 1][n - 1] = cdivr; + H[i + 1][n] = cdivi; + } + } + + // Overflow control + + t = Math.max(Math.abs(H[i][n - 1]), Math.abs(H[i][n])); + if ((eps * t) * t > 1) { + for (int j = i; j <= n; j++) { + H[j][n - 1] = H[j][n - 1] / t; + H[j][n] = H[j][n] / t; + } + } + } + } + } + } + + // Vectors of isolated roots + + for (int i = 0; i < nn; i++) { + if (i < low || i > high) { + for (int j = i; j < nn; j++) { + V[i][j] = H[i][j]; + } + } + } + + // Back transformation to get eigenvectors of original matrix + + for (int j = nn - 1; j >= low; j--) { + for (int i = low; i <= high; i++) { + z = 0.0; + for (int k = low; k <= Math.min(j, high); k++) { + z = z + V[i][k] * H[k][j]; + } + V[i][j] = z; + } + } + } + */ + + +} diff --git a/src/javajs/util/Encoding.java b/src/javajs/util/Encoding.java new file mode 100644 index 0000000..8d1a9a7 --- /dev/null +++ b/src/javajs/util/Encoding.java @@ -0,0 +1,33 @@ +/* $RCSfile$ + * $Author$ + * $Date$ + * $Revision$ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2011 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package javajs.util; + +public enum Encoding { + NONE, UTF8, UTF_16BE, UTF_16LE, UTF_32BE, UTF_32LE +} \ No newline at end of file diff --git a/src/javajs/util/JSAudioThread.java b/src/javajs/util/JSAudioThread.java new file mode 100644 index 0000000..3bcb006 --- /dev/null +++ b/src/javajs/util/JSAudioThread.java @@ -0,0 +1,258 @@ +package javajs.util; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.SourceDataLine; + +import sun.audio.AudioData; +import sun.audio.AudioDataStream; +import sun.audio.AudioPlayer; + + +/** + * The JSAudioThread adds a layer to JSThread that is specific for audio. + * This class utilizes JSAudioLine, which is not fully fleshed out. + * + * Two very simple interfaces, + * + * (new JSAudioThread()).playULawData(byte[] b); + * + * and + * + * (new JSAudioThread(audioFormat)).playOnce(byte[] b, offset, length); + * + * allow straightforward audio production without having to work with + * SourceDataLine directly. + * + * As a JSThread, this class implements myInit(), isLooping(), myLoop(), + * whenDone(), onException(), and doFinally(). + * + * If the constructor JSAudioThread(JSAudioThreadUser, AudioFormat, byte[]) is + * used, then the JSAudioThreadUser must simply implement fillAudioBuffer(), + * checkSoundStatus(), and audioThreadExiting() for very simple streaming audio. + * JSAudioThread will then take care of all the timing issues. + * + * But the implementer is welcome to override any of the JSThread overrides + * themselves in order to customize this further. + * + * The standard streaming case, then is: + * + * audioThread = new JSAudioThread(audioUser, audioFormat, audioByteBuffer); + * audioThread.start(); + * + * where audioUser provides + * + * checkSoundStatus() (called in isLooping()), + * + * fillAudioBuffer() (called in myLoop()), + * + * and + * + * audioThreadExiting() (called in doFinally()). + * + * @author Bob Hanson + * + */ +public class JSAudioThread extends JSThread { + + protected Owner owner; + protected boolean done; + protected int myBufferLength; + protected SourceDataLine line; + protected int rate, nChannels, bitsPerSample; + + protected byte[] audioByteBuffer; + protected int audioBufferByteLength; + + private AudioFormat audioFormat; + private int myBufferOffset; + private int playCount; + + public JSAudioThread(Owner owner, AudioFormat audioFormat, byte[] audioByteBuffer) { + this.owner = owner; + setFormat(audioFormat); + setBuffer(audioByteBuffer); + } + + /** + * A convenience constructor requiring standard settings of + * signed (for 8-bit) and littleEndian (for 16-bit) + * + * @param owner + * @param rate + * @param bitsPerSample + * @param nChannels + * @param audioByteBuffer + */ + public JSAudioThread(Owner owner, int rate, int bitsPerSample, int nChannels, byte[] audioByteBuffer) { + this.owner = owner; + setFormat(new AudioFormat(rate, bitsPerSample, nChannels, true, false)); + setBuffer(audioByteBuffer); + } + + /** + * primarily available for (new JSAudioThread()).playULawData + * + */ + public JSAudioThread() { + } + + /** + * primarily available for (new JSAudioThread()).playOnce + * + */ + public JSAudioThread(AudioFormat audioFormat) { + setFormat(audioFormat); + } + + /** + * + * A simple 8-bit uLaw data player + * + * @param data + */ + public void playULawData(byte[] data) { + // this constructor uses default new AudioFormat(ULAW,8000,8,1,1,8000,true) + // threading is taken care of by the browser in JavaScript + AudioPlayer.player.start(new AudioDataStream(new AudioData(data))); + } + + + /** + * Just play once through; no additions + * + * @param data + */ + public void playOnce(byte[] data, int offset, int length) { + setBuffer(data); + myBufferOffset = offset; + myBufferLength = length; + playCount = 1; + start(); + } + + public void setBuffer(byte[] audioByteBuffer) { + this.audioByteBuffer = audioByteBuffer; + audioBufferByteLength = audioByteBuffer.length; + } + + public SourceDataLine getLine() { + return line; + } + + public AudioFormat getFormat() { + return audioFormat; + } + + public void setFormat(AudioFormat audioFormat) { + this.audioFormat = audioFormat; + rate = (int) audioFormat.getSampleRate(); + bitsPerSample = audioFormat.getSampleSizeInBits(); + nChannels = audioFormat.getChannels(); + } + + public void resetAudio() { + if (line == null) + return; + line.flush(); + line.close(); + line = null; + } + + /** + * Standard initialization of a SourceDataLine + * + */ + @Override + protected boolean myInit() { + try { + DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); + if (line != null) + line.close(); + line = (SourceDataLine) AudioSystem.getLine(info); + line.open(audioFormat, audioBufferByteLength); + line.start(); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + protected boolean isLooping() { + return !done && (--playCount >= 0 || owner != null && owner.checkSoundStatus()); + } + + @Override + protected boolean myLoop() { + if (!done) { + if ((myBufferLength = (owner == null ? myBufferLength : owner.fillAudioBuffer())) <= 0) + return !(done = true); + try { + if (line == null) + myInit(); + line.write(audioByteBuffer, myBufferOffset, myBufferLength); + } catch (Exception e) { + e.printStackTrace(); + done = true; + } + } + return !done; + } + + @Override + protected void whenDone() { + done = true; + resetAudio(); + } + + @Override + protected int getDelayMillis() { + // about 25% of the actual play time + return 1000 // ms/sec + * (myBufferLength * 8 / bitsPerSample) // * samples + / rate // * seconds/sample) + / nChannels // / number of channels + / 4; // * 25% + } + + @Override + protected void onException(Exception e) { + e.printStackTrace(); + } + + @Override + protected void doFinally() { + if (owner != null) + owner.audioThreadExiting(); + } + + public interface Owner { + + /** + * + * @return true if thread should continue; false if not + * + */ + boolean checkSoundStatus(); + + /** fill audio buffer + * + * @return number of bytes to write to audio line + * + */ + int fillAudioBuffer(); + + /** + * called from the finally clause when complete + * + */ + void audioThreadExiting(); + + + } + +} + diff --git a/src/javajs/util/JSJSONParser.java b/src/javajs/util/JSJSONParser.java new file mode 100644 index 0000000..ce580e0 --- /dev/null +++ b/src/javajs/util/JSJSONParser.java @@ -0,0 +1,328 @@ +package javajs.util; + +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Map; + +import javajs.J2SIgnoreImport; + + + +/** + * a very simple JSON parser for JSON objects that are compatible with JavaScript + * A gross simplification of https://github.com/douglascrockford/JSON-java + * + * A SUBSET of JSON with similarly to window.JSON.parse(): + * + * In JavaScript returns "null" for a null value, not null + * + * -- requires quoted strings for keys and values + * + * -- does not allow /xxx/ objects + * + * @author Bob Hanson + * + */ +@J2SIgnoreImport({ HashMap.class }) +public class JSJSONParser { + + private String str; + private int index; + private int len; + private boolean asHashTable; + + public JSJSONParser () { + // for reflection + } + + /** + * requires { "key":"value", "key":"value",....} + * + * @param str + * @param asHashTable TODO + * + * @return Map or null + */ + @SuppressWarnings("unchecked") + public Map parseMap(String str, boolean asHashTable) { + index = 0; + this.asHashTable = asHashTable; + this.str = str; + len = str.length(); + if (getChar() != '{') + return null; + returnChar(); + return (Map) getValue(false); + } + + /** + * Could return Integer, Float, Boolean, String, Map, Lst, or null + * + * @param str + * @return a object equivalent to the JSON string str + * + */ + public Object parse(String str) { + index = 0; + this.str = str; + len = str.length(); + return getValue(false); + } + + private char next() { + return (index < len ? str.charAt(index++) : '\0'); + } + + private void returnChar() { + index--; + } + + /** + * Get the next char in the string, skipping whitespace. + * + * @throws JSONException + * @return one character, or 0 if there are no more characters. + */ + private char getChar() throws JSONException { + for (;;) { + char c = next(); + if (c == 0 || c > ' ') { + return c; + } + } + } + + /** + * only allowing the following values: + * + * {...} object + * + * [...] array + * + * Integer + * + * Float + * + * "quoted string" + * + * + * @param isKey if we should allow {...} and [...] + * @return a subclass of Object + * @throws JSONException + */ + private Object getValue(boolean isKey) throws JSONException { + int i = index; + char c = getChar(); + switch (c) { + case '\0': + return null; + case '"': + case '\'': + return getString(c); + case '{': + if (!isKey) + return getObject(); + c = 0; + break; + case '[': + if (!isKey) + return getArray(); + c = 0; + break; + default: + // standard syntax is assumed; not checking all possible invalid keys + // for example, "-" is not allowed in JavaScript, which is what this is for + returnChar(); + while (c >= ' ' && "[,]{:}'\"".indexOf(c) < 0) + c = next(); + returnChar(); + if (isKey && c != ':') + c = 0; + break; + } + if (isKey && c == 0) + throw new JSONException("invalid key"); + + String string = str.substring(i, index); + + // check for the only valid simple words: true, false, null (lower case) + // and in this case, only for + + if (!isKey) { + if (string.equals("true")) { + return Boolean.TRUE; + } + if (string.equals("false")) { + return Boolean.FALSE; + } + if (string.equals("null")) { + return (asHashTable ? string : null); + } + } + // only numbers from here on: + + c = string.charAt(0); + if (c >= '0' && c <= '9' || c == '-') + try { + if (string.indexOf('.') < 0 && string.indexOf('e') < 0 + && string.indexOf('E') < 0) + return new Integer(string); + // not allowing infinity or NaN + // using float here because Jmol does not use Double + Float d = Float.valueOf(string); + if (!d.isInfinite() && !d.isNaN()) + return d; + } catch (Exception e) { + } + // not a valid number + System.out.println("JSON parser cannot parse " + string); + throw new JSONException("invalid value"); + } + + private String getString(char quote) throws JSONException { + char c; + SB sb = null; + int i0 = index; + for (;;) { + int i1 = index; + switch (c = next()) { + case '\0': + case '\n': + case '\r': + throw syntaxError("Unterminated string"); + case '\\': + switch (c = next()) { + case '"': + case '\'': + case '\\': + case '/': + break; + case 'b': + c = '\b'; + break; + case 't': + c = '\t'; + break; + case 'n': + c = '\n'; + break; + case 'f': + c = '\f'; + break; + case 'r': + c = '\r'; + break; + case 'u': + int i = index; + index += 4; + try { + c = (char) Integer.parseInt(str.substring(i, index), 16); + } catch (Exception e) { + throw syntaxError("Substring bounds error"); + } + break; + default: + throw syntaxError("Illegal escape."); + } + break; + default: + if (c == quote) + return (sb == null ? str.substring(i0, i1) : sb.toString()); + break; + } + if (index > i1 + 1) { + if (sb == null) { + sb = new SB(); + sb.append(str.substring(i0, i1)); + } + } + if (sb != null) + sb.appendC(c); + } + } + + private Object getObject() { + Map map = (asHashTable ? new Hashtable() : new HashMap()); + String key = null; + switch (getChar()) { + case '}': + return map; + case 0: + throw new JSONException("invalid object"); + } + returnChar(); + boolean isKey = false; + for (;;) { + if ((isKey = !isKey) == true) + key = getValue(true).toString(); + else + map.put(key, getValue(false)); + switch (getChar()) { + case '}': + return map; + case ':': + if (isKey) + continue; + isKey = true; + //$FALL-THROUGH$ + case ',': + if (!isKey) + continue; + //$FALL-THROUGH$ + default: + throw syntaxError("Expected ',' or ':' or '}'"); + } + } + } + + private Object getArray() { + Lst l = new Lst(); + switch (getChar()) { + case ']': + return l; + case 0: + throw new JSONException("invalid array"); + } + returnChar(); + boolean isNull = false; + for (;;) { + if (isNull) { + l.addLast(null); + isNull = false; + } else { + l.addLast(getValue(false)); + } + switch (getChar()) { + case ',': + switch (getChar()) { + case ']': + // terminal , + return l; + case ',': + // empty value + isNull = true; + //$FALL-THROUGH$ + default: + returnChar(); + } + continue; + case ']': + return l; + default: + throw syntaxError("Expected ',' or ']'"); + } + } + } + + /** + * Make a JSONException to signal a syntax error. + * + * @param message + * The error message. + * @return A JSONException object, suitable for throwing + */ + public JSONException syntaxError(String message) { + return new JSONException(message + " for " + str.substring(0, Math.min(index, len))); + } + +} diff --git a/src/javajs/util/JSONException.java b/src/javajs/util/JSONException.java new file mode 100644 index 0000000..157f100 --- /dev/null +++ b/src/javajs/util/JSONException.java @@ -0,0 +1,13 @@ +package javajs.util; + +public class JSONException extends RuntimeException { + /** + * + */ + private static final long serialVersionUID = 1L; + + public JSONException(String message) { + super(message); + } + +} diff --git a/src/javajs/util/JSThread.java b/src/javajs/util/JSThread.java new file mode 100644 index 0000000..c2f3c4d --- /dev/null +++ b/src/javajs/util/JSThread.java @@ -0,0 +1,214 @@ +package javajs.util; + +import java.awt.Toolkit; +import java.awt.event.InvocationEvent; + +//import javajs.J2SRequireImport; +import javajs.api.JSFunction; + + +/** + * An abstract class that takes care of simple threading in Java or JavaScript. + * + * To use it, subclass it and complete the necessary methods. + * + * + * There are three states: INIT, LOOP, and DONE. + * + * These states are passed into run1 + * + * + * @author Bob Hanson + * + */ +//@J2SRequireImport(swingjs.JSToolkit.class) +public abstract class JSThread extends Thread implements JSFunction { + + public static final int INIT = 0; + public static final int LOOP = 1; + public static final int DONE = 2; + + public static int threadCount = 0; + + protected boolean isJS; + + public JSThread() { + this(null, "JSThread-" + (++threadCount)); + } + + public JSThread(String name) { + this(null, name); + } + + public JSThread(ThreadGroup group, String name) { + super(group, name); + /** + * @j2sNative + * + * this.isJS = true; + */ + {} + } + + @Override + public void run() { + run1(INIT); + } + + @Override + public synchronized void start() { + + + /** + * @j2sNative + * + * Clazz.load("swingjs.JSToolkit").dispatch$O$I$I(this, 1, 0); + * + */ + { + super.start(); + } + + } + + /** + * thread initialization + * + * @return false to exit thread before any looping + */ + protected abstract boolean myInit(); + + /** + * check for continuing to loop + * + * @return true if we are to continue looping + */ + protected abstract boolean isLooping(); + + /** + * + * @return false to handle sleepAndReturn yourself + */ + protected abstract boolean myLoop(); + /** + * what to do when the DONE state is reached + * + */ + protected abstract void whenDone(); + + /** + * + * @return the sleep time in milliseconds + */ + protected abstract int getDelayMillis(); + + /** + * handle an exception -- state will be set to DONE no matter what you do here + * + * @param e + */ + protected abstract void onException(Exception e); + + /** + * anything you want done in try{}catch(}finally(). + * Note that this method is not fired if we are in JavaScript + * mode and the normal return from sleepAndReturn() is taken. + * + */ + protected abstract void doFinally(); + + /** + * a generic method that loops until done, either in Java or JavaScript. + * + * In JavaScript it will reenter and continue at the appropriate spot. + * + * This method may be overridden if desired. + * + * @see org.uwi.SimThread + * + * @param state + */ + protected void run1(int state) { + boolean executeFinally = true; + // called by thisThread.run(); + try { + while (!interrupted()) { + switch (state) { + case INIT: + if (!myInit()) + return; + // initial code here + state = LOOP; + continue; + case LOOP: + // to stop looping, return false from isLooping() + if (!isLooping()) { + state = DONE; + continue; + } + // To handle sleepAndReturn yourself, or to skip the + // sleep when desired, return false from myLoop(); + // Note that doFinally must not be executed in this case. + // This is because JavaScript will do a return here + // for every loop, and Java will not. + if (myLoop() && sleepAndReturn(getDelayMillis(), state)) { + executeFinally = false; + return; + } + continue; + case DONE: + whenDone(); + // whatever + return; + } + } + } catch (Exception e) { + onException(e); + state = DONE; + } finally { + if (executeFinally) + doFinally(); + } + // normal exit + } + + /** + * + * @param r2 + * @param state + * @return true if we should interrupt (i.e. JavaScript) + * @throws InterruptedException + */ + protected boolean sleepAndReturn(final int delay, final int state) + throws InterruptedException { + if (!isJS) { + sleep(delay); + return false; + } + + // in JavaScript, we need to do this through the system event queue, + // which in JSToolkit takes care of all the "thread" handling. + + final JSThread me = this; + Runnable r = new Runnable() { + @Override + public void run() { + me.run1(state); + } + }; + /** + * @j2sNative + * + * setTimeout( + * function() {java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent$java_awt_AWTEvent(Clazz.new(java.awt.event.InvocationEvent.c$$O$Runnable,[me, r]))}, + * delay); + * + */ + { + Toolkit.getDefaultToolkit().getSystemEventQueue() + .postEvent(new InvocationEvent(me, r)); + } + return true; + } + +} diff --git a/src/javajs/util/LimitedLineReader.java b/src/javajs/util/LimitedLineReader.java new file mode 100644 index 0000000..e1d973f --- /dev/null +++ b/src/javajs/util/LimitedLineReader.java @@ -0,0 +1,48 @@ +package javajs.util; + +import java.io.BufferedReader; + +/** + * A simple class to read a designated number of bytes from a + * file and then return them line by line, skipping lines that + * start with #, and including the \n or \r characters at line ends. + * + * Generally useful for determining what sort of data a file contains. + * + */ +public class LimitedLineReader { + private char[] buf; + private int cchBuf; + private int ichCurrent; + + public LimitedLineReader(BufferedReader bufferedReader, int readLimit) + throws Exception { + bufferedReader.mark(readLimit + 1); + buf = new char[readLimit]; + cchBuf = Math.max(bufferedReader.read(buf, 0, readLimit), 0); + ichCurrent = 0; + bufferedReader.reset(); + } + + public String getHeader(int n) { + return (n == 0 ? new String(buf) : new String(buf, 0, Math.min(cchBuf, n))); + } + + public String readLineWithNewline() { + while (ichCurrent < cchBuf) { + int ichBeginningOfLine = ichCurrent; + char ch = 0; + while (ichCurrent < cchBuf && + (ch = buf[ichCurrent++]) != '\r' && ch != '\n') { + } + if (ch == '\r' && ichCurrent < cchBuf && buf[ichCurrent] == '\n') + ++ichCurrent; + int cchLine = ichCurrent - ichBeginningOfLine; + if (buf[ichBeginningOfLine] == '#') + continue; // flush comment lines; + return new String(buf, ichBeginningOfLine, cchLine); + } + return ""; + } +} + diff --git a/src/javajs/util/ListDataReader.java b/src/javajs/util/ListDataReader.java new file mode 100644 index 0000000..7814977 --- /dev/null +++ b/src/javajs/util/ListDataReader.java @@ -0,0 +1,57 @@ +package javajs.util; + +import java.io.IOException; + + + + + +/** + * + * VectorDataReader subclasses BufferedReader and overrides its + * read, readLine, mark, and reset methods so that JmolAdapter + * works with Vector arrays without any further adaptation. + * + */ + +public class ListDataReader extends DataReader { + private Lst data; + private int pt; + private int len; + + public ListDataReader() { + super(); + } + + @SuppressWarnings("unchecked") + @Override + public DataReader setData(Object data) { + this.data = (Lst) data; + len = this.data.size(); + return this; + } + + @Override + public int read(char[] buf, int off, int len) throws IOException { + return readBuf(buf, off, len); + } + + @Override + public String readLine() { + return (pt < len ? data.get(pt++) : null); + } + + /** + * + * @param ptr + */ + public void mark(long ptr) { + //ignore ptr. + ptMark = pt; + } + + @Override + public void reset() { + pt = ptMark; + } +} \ No newline at end of file diff --git a/src/javajs/util/Lst.java b/src/javajs/util/Lst.java new file mode 100644 index 0000000..e7fdda5 --- /dev/null +++ b/src/javajs/util/Lst.java @@ -0,0 +1,56 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2007-04-26 16:57:51 -0500 (Thu, 26 Apr 2007) $ + * $Revision: 7502 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package javajs.util; + +import java.util.ArrayList; + +/** + * created to remove ambiguities in add and remove + * + * @param + */ +@SuppressWarnings("serial") +public class Lst extends ArrayList { + + public Lst() { + super(); + } + + public boolean addLast(V v) { + return super.add(v); + } + + public V removeItemAt(int location) { + return super.remove(location); + } + + public boolean removeObj(Object v) { + return super.remove(v); + } + +} diff --git a/src/javajs/util/M3.java b/src/javajs/util/M3.java new file mode 100644 index 0000000..7749277 --- /dev/null +++ b/src/javajs/util/M3.java @@ -0,0 +1,633 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + +import java.io.Serializable; + + + +/** + * A single precision floating point 3 by 3 matrix. + * + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 for unique + * constructor and method names for the optimization of compiled + * JavaScript using Java2Script. + * + * + * + */ +@SuppressWarnings("serial") +public class M3 extends M34 implements Serializable { + + /** + * Constructs and initializes a Matrix3f from the specified 9 element array. + * this.m00 =v[0], this.m01=v[1], etc. + * + * @param v + * the array of length 9 containing in order + * @return m + */ + public static M3 newA9(float[] v) { + M3 m = new M3(); + m.setA(v); + return m; + } + + /** + * Constructs a new matrix with the same values as the Matrix3f parameter. + * + * @param m1 + * The source matrix. + * @return m + */ + public static M3 newM3(M3 m1) { + M3 m = new M3(); + if (m1 == null) { + m.setScale(1); + return m; + } + m.m00 = m1.m00; + m.m01 = m1.m01; + m.m02 = m1.m02; + + m.m10 = m1.m10; + m.m11 = m1.m11; + m.m12 = m1.m12; + + m.m20 = m1.m20; + m.m21 = m1.m21; + m.m22 = m1.m22; + return m; + } + + /** + * Sets this Matrix3f to a scalar * Identity. + * @param scale + */ + public void setScale(float scale) { + clear33(); + m00 = m11 = m22 = scale; + } + + /** + * Sets the value of this matrix to the double value of the Matrix3f argument. + * + * @param m1 + * the matrix3f + */ + public void setM3(M34 m1) { + setM33(m1); + } + /** + * Sets the values in this Matrix3f equal to the row-major array parameter + * (ie, the first four elements of the array will be copied into the first row + * of this matrix, etc.). + * + * @param m + */ + public void setA(float m[]) { + m00 = m[0]; + m01 = m[1]; + m02 = m[2]; + m10 = m[3]; + m11 = m[4]; + m12 = m[5]; + m20 = m[6]; + m21 = m[7]; + m22 = m[8]; + } + + /** + * Sets the specified element of this matrix3d to the value provided. + * + * @param row + * the row number to be modified (zero indexed) + * @param col + * the column number to be modified (zero indexed) + * @param v + * the new value + */ + public void setElement(int row, int col, float v) { + set33(row, col, v); + } + + /** + * Retrieves the value at the specified row and column of this matrix. + * + * @param row + * the row number to be retrieved (zero indexed) + * @param col + * the column number to be retrieved (zero indexed) + * @return the value at the indexed element + */ + public float getElement(int row, int col) { + return get33(row, col); + } + + /** + * Sets the specified row of this matrix3d to the three values provided. + * + * @param row + * the row number to be modified (zero indexed) + * @param x + * the first column element + * @param y + * the second column element + * @param z + * the third column element + */ + public void setRow(int row, float x, float y, float z) { + switch (row) { + case 0: + m00 = x; + m01 = y; + m02 = z; + return; + case 1: + m10 = x; + m11 = y; + m12 = z; + return; + case 2: + m20 = x; + m21 = y; + m22 = z; + return; + default: + err(); + } + } + + /** + * Sets the specified row of this matrix3d to the Vector provided. + * + * @param row + * the row number to be modified (zero indexed) + * @param v + * the replacement row + */ + public void setRowV(int row, T3 v) { + switch (row) { + case 0: + m00 = v.x; + m01 = v.y; + m02 = v.z; + return; + case 1: + m10 = v.x; + m11 = v.y; + m12 = v.z; + return; + case 2: + m20 = v.x; + m21 = v.y; + m22 = v.z; + return; + default: + err(); + } + } + + /** + * Sets the specified row of this matrix3d to the four values provided. + * + * @param row + * the row number to be modified (zero indexed) + * @param v + * the replacement row + */ + public void setRowA(int row, float v[]) { + setRow33(row, v); + } + + /** + * Copies the matrix values in the specified row into the array parameter. + * + * @param row + * the matrix row + * @param v + * The array into which the matrix row values will be copied + */ + @Override + public void getRow(int row, float v[]) { + getRow33(row, v); + } + + /** + * Sets the specified column of this matrix3d to the three values provided. + * + * @param column + * the column number to be modified (zero indexed) + * @param x + * the first row element + * @param y + * the second row element + * @param z + * the third row element + */ + public void setColumn3(int column, float x, float y, float z) { + switch (column) { + case 0: + m00 = x; + m10 = y; + m20 = z; + break; + case 1: + m01 = x; + m11 = y; + m21 = z; + break; + case 2: + m02 = x; + m12 = y; + m22 = z; + break; + default: + err(); + } + } + + /** + * Sets the specified column of this matrix3d to the vector provided. + * + * @param column + * the column number to be modified (zero indexed) + * @param v + * the replacement column + */ + public void setColumnV(int column, T3 v) { + switch (column) { + case 0: + m00 = v.x; + m10 = v.y; + m20 = v.z; + break; + case 1: + m01 = v.x; + m11 = v.y; + m21 = v.z; + break; + case 2: + m02 = v.x; + m12 = v.y; + m22 = v.z; + break; + default: + err(); + } + } + + /** + * Copies the matrix values in the specified column into the vector parameter. + * + * @param column + * the matrix column + * @param v + * The vector into which the matrix row values will be copied + */ + public void getColumnV(int column, T3 v) { + switch (column) { + case 0: + v.x = m00; + v.y = m10; + v.z = m20; + break; + case 1: + v.x = m01; + v.y = m11; + v.z = m21; + break; + case 2: + v.x = m02; + v.y = m12; + v.z = m22; + break; + default: + err(); + } + } + + /** + * Sets the specified column of this matrix3d to the four values provided. + * + * @param column + * the column number to be modified (zero indexed) + * @param v + * the replacement column + */ + public void setColumnA(int column, float v[]) { + setColumn33(column, v); + } + + /** + * Copies the matrix values in the specified column into the array parameter. + * + * @param column + * the matrix column + * @param v + * The array into which the matrix row values will be copied + */ + public void getColumn(int column, float v[]) { + getColumn33(column, v); + } + + /** + * Sets the value of this matrix to sum of itself and matrix m1. + * + * @param m1 + * the other matrix + */ + public void add(M3 m1) { + add33(m1); + } + + /** + * Sets the value of this matrix to the matrix difference of itself and matrix + * m1 (this = this - m1). + * + * @param m1 + * the other matrix + */ + public void sub(M3 m1) { + sub33(m1); + } + + /** + * Sets the value of this matrix to its transpose. + */ + public void transpose() { + transpose33(); + } + + /** + * Sets the value of this matrix to the transpose of the argument matrix + * + * @param m1 + * the matrix to be transposed + */ + public void transposeM(M3 m1) { + // alias-safe + setM33(m1); + transpose33(); + } + + /** + * Sets the value of this matrix to the matrix inverse of the passed matrix + * m1. + * + * @param m1 + * the matrix to be inverted + */ + public void invertM(M3 m1) { + setM33(m1); + invert(); + } + + /** + * Sets the value of this matrix to its inverse. + */ + public void invert() { + double s = determinant3(); + if (s == 0.0) + return; + s = 1 / s; + // alias-safe way. + set9(m11 * m22 - m12 * m21, m02 * m21 - m01 * m22, m01 * m12 - m02 * m11, + m12 * m20 - m10 * m22, m00 * m22 - m02 * m20, m02 * m10 - m00 * m12, + m10 * m21 - m11 * m20, m01 * m20 - m00 * m21, m00 * m11 - m01 * m10); + scale((float) s); + } + + /** + * Sets the value of this matrix to a rotation matrix about the x axis by the + * passed angle. + * + * @param angle + * the angle to rotate about the X axis in radians + * @return this + */ + public M3 setAsXRotation(float angle) { + setXRot(angle); + return this; + } + + /** + * Sets the value of this matrix to a rotation matrix about the y axis by the + * passed angle. + * + * @param angle + * the angle to rotate about the Y axis in radians + * @return this + */ + public M3 setAsYRotation(float angle) { + setYRot(angle); + return this; + } + + /** + * Sets the value of this matrix to a rotation matrix about the z axis by the + * passed angle. + * + * @param angle + * the angle to rotate about the Z axis in radians + * @return this + */ + public M3 setAsZRotation(float angle) { + setZRot(angle); + return this; + } + + /** + * Multiplies each element of this matrix by a scalar. + * + * @param scalar + * The scalar multiplier. + */ + public void scale(float scalar) { + mul33(scalar); + } + + /** + * Sets the value of this matrix to the result of multiplying itself with + * matrix m1. + * + * @param m1 + * the other matrix + */ + public void mul(M3 m1) { + mul2(this, m1); + } + + /** + * Sets the value of this matrix to the result of multiplying the two argument + * matrices together. + * + * @param m1 + * the first matrix + * @param m2 + * the second matrix + */ + public void mul2(M3 m1, M3 m2) { + // alias-safe way. + set9(m1.m00 * m2.m00 + m1.m01 * m2.m10 + m1.m02 * m2.m20, m1.m00 * m2.m01 + + m1.m01 * m2.m11 + m1.m02 * m2.m21, m1.m00 * m2.m02 + m1.m01 * m2.m12 + + m1.m02 * m2.m22, + + m1.m10 * m2.m00 + m1.m11 * m2.m10 + m1.m12 * m2.m20, m1.m10 * m2.m01 + + m1.m11 * m2.m11 + m1.m12 * m2.m21, m1.m10 * m2.m02 + m1.m11 * m2.m12 + + m1.m12 * m2.m22, + + m1.m20 * m2.m00 + m1.m21 * m2.m10 + m1.m22 * m2.m20, m1.m20 * m2.m01 + + m1.m21 * m2.m11 + m1.m22 * m2.m21, m1.m20 * m2.m02 + m1.m21 * m2.m12 + + m1.m22 * m2.m22); + } + + /** + * Returns true if the Object o is of type Matrix3f and all of the data + * members of t1 are equal to the corresponding data members in this Matrix3f. + * + * @param o + * the object with which the comparison is made. + */ + @Override + public boolean equals(Object o) { + if (!(o instanceof M3)) + return false; + M3 m = (M3) o; + return m00 == m.m00 && m01 == m.m01 && m02 == m.m02 && m10 == m.m10 + && m11 == m.m11 && m12 == m.m12 && m20 == m.m20 && m21 == m.m21 + && m22 == m.m22; + } + + /** + * Returns a hash number based on the data values in this object. Two + * different Matrix3f objects with identical data values (ie, returns true for + * equals(Matrix3f) ) will return the same hash number. Two objects with + * different data members may return the same hash value, although this is not + * likely. + * + * @return the integer hash value + */ + @Override + public int hashCode() { + return T3.floatToIntBits(m00) ^ T3.floatToIntBits(m01) + ^ T3.floatToIntBits(m02) ^ T3.floatToIntBits(m10) + ^ T3.floatToIntBits(m11) ^ T3.floatToIntBits(m12) + ^ T3.floatToIntBits(m20) ^ T3.floatToIntBits(m21) + ^ T3.floatToIntBits(m22); + } + + /** + * Sets this matrix to all zeros. + */ + public void setZero() { + clear33(); + } + + /** + * Sets 9 values + * + * @param m00 + * @param m01 + * @param m02 + * @param m10 + * @param m11 + * @param m12 + * @param m20 + * @param m21 + * @param m22 + */ + private void set9(float m00, float m01, float m02, float m10, float m11, + float m12, float m20, float m21, float m22) { + this.m00 = m00; + this.m01 = m01; + this.m02 = m02; + this.m10 = m10; + this.m11 = m11; + this.m12 = m12; + this.m20 = m20; + this.m21 = m21; + this.m22 = m22; + } + + /** + * Returns a string that contains the values of this Matrix3f. + * + * @return the String representation + */ + @Override + public String toString() { + return "[\n [" + m00 + "\t" + m01 + "\t" + m02 + "]" + "\n [" + m10 + + "\t" + m11 + "\t" + m12 + "]" + "\n [" + m20 + "\t" + m21 + "\t" + + m22 + "] ]"; + } + + /** + * Sets the value of this matrix to the matrix conversion of the single + * precision axis and angle argument. + * + * @param a + * the axis and angle to be converted + * @return this + */ + public M3 setAA(A4 a) { + setAA33(a); + return this; + } + + /** + * 3D ball rotation from dx dy in-plane mouse motion + * adapted from Andrew Hanson + * Computer Graphics beyond the Third Dimension: + * Geometry, Orientation Control, and Rendering for + * Graphics in Dimensions Greater than Three + * Course Notes for SIGGRAPH ’98 + * http://www.cse.ohio-state.edu/~hwshen/888_su02/hanson_note.pdf + * + * @param responseFactor Jmol uses 0.02 here + * @param dx + * @param dy + * @return true if successful; false if not; + */ + public boolean setAsBallRotation(float responseFactor, float dx, float dy) { + float r = (float) Math.sqrt(dx * dx + dy * dy); + float th = r * responseFactor; + if (th == 0) { + setScale(1); + return false; + } + float c = (float) Math.cos(th); + float s = (float) Math.sin(th); + float nx = -dy / r; + float ny = dx / r; + float c1 = c - 1; + m00 = 1 + c1 * nx * nx; + m01 = m10 = c1 * nx * ny; + m20 = -(m02 = s * nx); + m11 = 1 + c1 * ny * ny; + m21 = -(m12 = s * ny); + m22 = c; + return true; + } + + public boolean isRotation() { + return (Math.abs(determinant3() - 1) < 0.001f); + } + +} diff --git a/src/javajs/util/M34.java b/src/javajs/util/M34.java new file mode 100644 index 0000000..b0882dd --- /dev/null +++ b/src/javajs/util/M34.java @@ -0,0 +1,417 @@ +package javajs.util; + +/** + * A base class for both M3 and M4 to conserve code size. + * + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 for unique + * constructor and method names for the optimization of compiled + * JavaScript using Java2Script. + * and for subclassing to M3 and M4 + * + */ +public abstract class M34 { + + /** + * The first element of the first row + */ + public float m00; + + /** + * The second element of the first row. + */ + public float m01; + + /** + * third element of the first row. + */ + public float m02; + + /** + * The first element of the second row. + */ + public float m10; + + /** + * The second element of the second row. + */ + public float m11; + + /** + * The third element of the second row. + */ + public float m12; + + /** + * The first element of the third row. + */ + public float m20; + + /** + * The second element of the third row. + */ + public float m21; + + /** + * The third element of the third row. + */ + public float m22; + + protected void setAA33(A4 a) { + double x = a.x; + double y = a.y; + double z = a.z; + double angle = a.angle; + // Taken from Rick's which is taken from Wertz. pg. 412 + // Bug Fixed and changed into right-handed by hiranabe + double n = Math.sqrt(x * x + y * y + z * z); + // zero-div may occur + n = 1 / n; + x *= n; + y *= n; + z *= n; + double c = Math.cos(angle); + double s = Math.sin(angle); + double omc = 1.0 - c; + m00 = (float) (c + x * x * omc); + m11 = (float) (c + y * y * omc); + m22 = (float) (c + z * z * omc); + + double tmp1 = x * y * omc; + double tmp2 = z * s; + m01 = (float) (tmp1 - tmp2); + m10 = (float) (tmp1 + tmp2); + + tmp1 = x * z * omc; + tmp2 = y * s; + m02 = (float) (tmp1 + tmp2); + m20 = (float) (tmp1 - tmp2); + + tmp1 = y * z * omc; + tmp2 = x * s; + m12 = (float) (tmp1 - tmp2); + m21 = (float) (tmp1 + tmp2); + } + + public void rotate(T3 t) { + // alias-safe + rotate2(t, t); + } + + /** + * Transform the vector vec using this Matrix3f and place the result into + * vecOut. + * + * @param t + * the single precision vector to be transformed + * @param result + * the vector into which the transformed values are placed + */ + public void rotate2(T3 t, T3 result) { + // alias-safe + result.set(m00 * t.x + m01 * t.y + m02 * t.z, m10 * t.x + m11 * t.y + m12 + * t.z, m20 * t.x + m21 * t.y + m22 * t.z); + } + + + /** + * Sets the value of this matrix to the double value of the Matrix3f argument. + * + * @param m1 + * the matrix3f + */ + protected void setM33(M34 m1) { + m00 = m1.m00; + m01 = m1.m01; + m02 = m1.m02; + m10 = m1.m10; + m11 = m1.m11; + m12 = m1.m12; + m20 = m1.m20; + m21 = m1.m21; + m22 = m1.m22; + } + + protected void clear33() { + m00 = m01 = m02 = m10 = m11 = m12 = m20 = m21 = m22 = 0.0f; + } + + protected void set33(int row, int col, float v) { + switch (row) { + case 0: + switch (col) { + case 0: + m00 = v; + return; + case 1: + m01 = v; + return; + case 2: + m02 = v; + return; + } + break; + case 1: + switch (col) { + case 0: + m10 = v; + return; + case 1: + m11 = v; + return; + case 2: + m12 = v; + return; + } + break; + case 2: + switch (col) { + case 0: + m20 = v; + return; + case 1: + m21 = v; + return; + case 2: + m22 = v; + return; + } + break; + } + err(); + } + + protected float get33(int row, int col) { + switch (row) { + case 0: + switch (col) { + case 0: + return m00; + case 1: + return m01; + case 2: + return m02; + } + break; + case 1: + switch (col) { + case 0: + return m10; + case 1: + return m11; + case 2: + return m12; + } + break; + case 2: + switch (col) { + case 0: + return m20; + case 1: + return m21; + case 2: + return m22; + } + break; + } + err(); + return 0; + } + + protected void setRow33(int row, float v[]) { + switch (row) { + case 0: + m00 = v[0]; + m01 = v[1]; + m02 = v[2]; + return; + case 1: + m10 = v[0]; + m11 = v[1]; + m12 = v[2]; + return; + case 2: + m20 = v[0]; + m21 = v[1]; + m22 = v[2]; + return; + default: + err(); + } + } + + public abstract void getRow(int row, float v[]); + + protected void getRow33(int row, float v[]) { + switch (row) { + case 0: + v[0] = m00; + v[1] = m01; + v[2] = m02; + return; + case 1: + v[0] = m10; + v[1] = m11; + v[2] = m12; + return; + case 2: + v[0] = m20; + v[1] = m21; + v[2] = m22; + return; + } + err(); + } + + protected void setColumn33(int column, float v[]) { + switch(column) { + case 0: + m00 = v[0]; + m10 = v[1]; + m20 = v[2]; + break; + case 1: + m01 = v[0]; + m11 = v[1]; + m21 = v[2]; + break; + case 2: + m02 = v[0]; + m12 = v[1]; + m22 = v[2]; + break; + default: + err(); + } + } + + protected void getColumn33(int column, float v[]) { + switch(column) { + case 0: + v[0] = m00; + v[1] = m10; + v[2] = m20; + break; + case 1: + v[0] = m01; + v[1] = m11; + v[2] = m21; + break; + case 2: + v[0] = m02; + v[1] = m12; + v[2] = m22; + break; + default: + err(); + } + } + + protected void add33(M34 m1) { + m00 += m1.m00; + m01 += m1.m01; + m02 += m1.m02; + m10 += m1.m10; + m11 += m1.m11; + m12 += m1.m12; + m20 += m1.m20; + m21 += m1.m21; + m22 += m1.m22; + } + + protected void sub33(M34 m1) { + m00 -= m1.m00; + m01 -= m1.m01; + m02 -= m1.m02; + m10 -= m1.m10; + m11 -= m1.m11; + m12 -= m1.m12; + m20 -= m1.m20; + m21 -= m1.m21; + m22 -= m1.m22; + } + + protected void mul33(float x) { + m00 *= x; + m01 *= x; + m02 *= x; + m10 *= x; + m11 *= x; + m12 *= x; + m20 *= x; + m21 *= x; + m22 *= x; + } + + protected void transpose33() { + float tmp = m01; + m01 = m10; + m10 = tmp; + + tmp = m02; + m02 = m20; + m20 = tmp; + + tmp = m12; + m12 = m21; + m21 = tmp; + } + + protected void setXRot(float angle) { + double c = Math.cos(angle); + double s = Math.sin(angle); + m00 = 1.0f; + m01 = 0.0f; + m02 = 0.0f; + m10 = 0.0f; + m11 = (float) c; + m12 = (float) -s; + m20 = 0.0f; + m21 = (float) s; + m22 = (float) c; + } + + protected void setYRot(float angle) { + double c = Math.cos(angle); + double s = Math.sin(angle); + m00 = (float) c; + m01 = 0.0f; + m02 = (float) s; + m10 = 0.0f; + m11 = 1.0f; + m12 = 0.0f; + m20 = (float) -s; + m21 = 0.0f; + m22 = (float) c; + } + + protected void setZRot(float angle) { + double c = Math.cos(angle); + double s = Math.sin(angle); + m00 = (float) c; + m01 = (float) -s; + m02 = 0.0f; + m10 = (float) s; + m11 = (float) c; + m12 = 0.0f; + m20 = 0.0f; + m21 = 0.0f; + m22 = 1.0f; + } + + /** + * @return 3x3 determinant + */ + public float determinant3() { + return m00 * (m11 * m22 - m21 * m12) - m01 * (m10 * m22 - m20 * m12) + m02 + * (m10 * m21 - m20 * m11); + } + + protected void err() { + throw new ArrayIndexOutOfBoundsException( + "matrix column/row out of bounds"); + } + + +} diff --git a/src/javajs/util/M4.java b/src/javajs/util/M4.java new file mode 100644 index 0000000..1f16335 --- /dev/null +++ b/src/javajs/util/M4.java @@ -0,0 +1,940 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + +/** + * A single precision floating point 4 by 4 matrix. + * + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 for unique + * constructor and method names for the optimization of compiled + * JavaScript using Java2Script. + * + */ +public class M4 extends M34 { + + /** + * The fourth element of the first row. + */ + public float m03; + + /** + * The fourth element of the second row. + */ + public float m13; + + /** + * The fourth element of the third row. + */ + public float m23; + + /** + * The first element of the fourth row. + */ + public float m30; + + /** + * The second element of the fourth row. + */ + public float m31; + + /** + * The third element of the fourth row. + */ + public float m32; + + /** + * The fourth element of the fourth row. + */ + public float m33 = 0; + + public M4() { + } + /** + * Constructs and initializes a Matrix4f from the specified 16 element array. + * this.m00 =v[0], this.m01=v[1], etc. + * + * @param v + * the array of length 16 containing in order + * @return m + */ + public static M4 newA16(float[] v) { + M4 m = new M4(); + m.m00 = v[0]; + m.m01 = v[1]; + m.m02 = v[2]; + m.m03 = v[3]; + + m.m10 = v[4]; + m.m11 = v[5]; + m.m12 = v[6]; + m.m13 = v[7]; + + m.m20 = v[8]; + m.m21 = v[9]; + m.m22 = v[10]; + m.m23 = v[11]; + + m.m30 = v[12]; + m.m31 = v[13]; + m.m32 = v[14]; + m.m33 = v[15]; + + return m; + } + + /** + * Constructs a new matrix with the same values as the Matrix4f parameter. + * + * @param m1 + * the source matrix + * @return m + */ + public static M4 newM4(M4 m1) { + M4 m = new M4(); + if (m1 == null) { + m.setIdentity(); + return m; + } + m.setToM3(m1); + m.m03 = m1.m03; + m.m13 = m1.m13; + m.m23 = m1.m23; + m.m30 = m1.m30; + m.m31 = m1.m31; + m.m32 = m1.m32; + m.m33 = m1.m33; + return m; + } + + /** + * Constructs and initializes a Matrix4f from the rotation matrix and + * translation. + * + * @param m1 + * The rotation matrix representing the rotational components + * @param t + * The translational components of the matrix + * @return m + */ + public static M4 newMV(M3 m1, T3 t) { + M4 m = new M4(); + m.setMV(m1, t); + return m; + } + + /** + * Sets this matrix to all zeros. + */ + public void setZero() { + clear33(); + m03 = m13 = m23 = m30 = m31 = m32 = m33 = 0.0f; + } + + /** + * Sets this Matrix4f to identity. + */ + public void setIdentity() { + setZero(); + m00 = m11 = m22 = m33 = 1.0f; + } + + /** + * Sets the value of this matrix to a copy of the passed matrix m1. + * + * @param m1 + * the matrix to be copied + * @return this + */ + public M4 setM4(M4 m1) { + setM33(m1); + m03 = m1.m03; + m13 = m1.m13; + m23 = m1.m23; + m30 = m1.m30; + m31 = m1.m31; + m32 = m1.m32; + m33 = m1.m33; + return this; + } + + /** + * Initializes a Matrix4f from the rotation matrix and translation. + * + * @param m1 + * The rotation matrix representing the rotational components + * @param t + * The translational components of the matrix + */ + public void setMV(M3 m1, T3 t) { + setM33(m1); + setTranslation(t); + m33 = 1; + } + + /** + * Sets the rotational component (upper 3x3) of this matrix to the matrix + * values in the single precision Matrix3f argument; the other elements of + * this matrix are initialized as if this were an identity matrix (ie, affine + * matrix with no translational component). + * + * @param m1 + * the 3x3 matrix + */ + public void setToM3(M34 m1) { + setM33(m1); + m03 = m13 = m23 = m30 = m31 = m32 = 0.0f; + m33 = 1.0f; + } + + /** + * Sets the rotational component (upper 3x3) of this matrix + * to a rotation given by an axis angle + * + * @param a + * the axis and angle to be converted + */ + public void setToAA(A4 a) { + setIdentity(); + setAA33(a); + } + + /** + * Sets the values in this Matrix4f equal to the row-major array parameter + * (ie, the first four elements of the array will be copied into the first row + * of this matrix, etc.). + * + * @param m + */ + public void setA(float m[]) { + m00 = m[0]; + m01 = m[1]; + m02 = m[2]; + m03 = m[3]; + m10 = m[4]; + m11 = m[5]; + m12 = m[6]; + m13 = m[7]; + m20 = m[8]; + m21 = m[9]; + m22 = m[10]; + m23 = m[11]; + m30 = m[12]; + m31 = m[13]; + m32 = m[14]; + m33 = m[15]; + } + + /** + * Modifies the translational components of this matrix to the values of the + * Vector3f argument; the other values of this matrix are not modified. + * + * @param trans + * the translational component + */ + public void setTranslation(T3 trans) { + m03 = trans.x; + m13 = trans.y; + m23 = trans.z; + } + + /** + * Sets the specified element of this matrix4f to the value provided. + * + * @param row + * the row number to be modified (zero indexed) + * @param col + * the column number to be modified (zero indexed) + * @param v + * the new value + */ + public void setElement(int row, int col, float v) { + if (row < 3 && col < 3) { + set33(row, col, v); + return; + } + if (row > 3 || col > 3) + err(); + switch (row) { + case 0: + m03 = v; + return; + case 1: + m13 = v; + return; + case 2: + m23 = v; + return; + } + switch (col) { + case 0: + m30 = v; + return; + case 1: + m31 = v; + return; + case 2: + m32 = v; + return; + case 3: + m33 = v; + return; + } + } + + /** + * Retrieves the value at the specified row and column of this matrix. + * + * @param row + * the row number to be retrieved (zero indexed) + * @param col + * the column number to be retrieved (zero indexed) + * @return the value at the indexed element + */ + public float getElement(int row, int col) { + if (row < 3 && col < 3) + return get33(row, col); + if (row > 3 || col > 3) { + err(); + return 0; + } + switch (row) { + case 0: + return m03; + case 1: + return m13; + case 2: + return m23; + default: + switch (col) { + case 0: + return m30; + case 1: + return m31; + case 2: + return m32; + default: + return m33; + } + } + } + + /** + * Retrieves the translational components of this matrix. + * + * @param trans + * the vector that will receive the translational component + */ + public void getTranslation(T3 trans) { + trans.x = m03; + trans.y = m13; + trans.z = m23; + } + + /** + * Gets the upper 3x3 values of this matrix and places them into the matrix + * m1. + * + * @param m1 + * The matrix that will hold the values + */ + public void getRotationScale(M3 m1) { + m1.m00 = m00; + m1.m01 = m01; + m1.m02 = m02; + m1.m10 = m10; + m1.m11 = m11; + m1.m12 = m12; + m1.m20 = m20; + m1.m21 = m21; + m1.m22 = m22; + } + + /** + * Replaces the upper 3x3 matrix values of this matrix with the values in the + * matrix m1. + * + * @param m1 + * The matrix that will be the new upper 3x3 + */ + public void setRotationScale(M3 m1) { + m00 = m1.m00; + m01 = m1.m01; + m02 = m1.m02; + m10 = m1.m10; + m11 = m1.m11; + m12 = m1.m12; + m20 = m1.m20; + m21 = m1.m21; + m22 = m1.m22; + } + + /** + * Sets the specified row of this matrix4f to the four values provided. + * + * @param row + * the row number to be modified (zero indexed) + * @param v + * the replacement row + */ + public void setRowA(int row, float v[]) { + if (row < 3) + setRow33(row, v); + switch (row) { + case 0: + m03 = v[3]; + return; + case 1: + m13 = v[3]; + return; + case 2: + m23 = v[3]; + return; + case 3: + m30 = v[0]; + m31 = v[1]; + m32 = v[2]; + m33 = v[3]; + return; + } + err(); + } + + /** + * Copies the matrix values in the specified row into the array parameter. + * + * @param row + * the matrix row + * @param v + * The array into which the matrix row values will be copied + */ + @Override + public void getRow(int row, float v[]) { + if (row < 3) + getRow33(row, v); + switch (row) { + case 0: + v[3] = m03; + return; + case 1: + v[3] = m13; + return; + case 2: + v[3] = m23; + return; + case 3: + v[0] = m30; + v[1] = m31; + v[2] = m32; + v[3] = m33; + return; + } + err(); + } + + /** + * Sets the specified column of this matrix4f to the four values provided. + * + * @param column + * the column number to be modified (zero indexed) + * @param x + * the first row element + * @param y + * the second row element + * @param z + * the third row element + * @param w + * the fourth row element + */ + public void setColumn4(int column, float x, float y, float z, float w) { + if (column == 0) { + m00 = x; + m10 = y; + m20 = z; + m30 = w; + } else if (column == 1) { + m01 = x; + m11 = y; + m21 = z; + m31 = w; + } else if (column == 2) { + m02 = x; + m12 = y; + m22 = z; + m32 = w; + } else if (column == 3) { + m03 = x; + m13 = y; + m23 = z; + m33 = w; + } else { + err(); + } + } + + /** + * Sets the specified column of this matrix4f to the four values provided. + * + * @param column + * the column number to be modified (zero indexed) + * @param v + * the replacement column + */ + public void setColumnA(int column, float v[]) { + if (column < 3) + setColumn33(column, v); + switch (column) { + case 0: + m30 = v[3]; + return; + case 1: + m31 = v[3]; + return; + case 2: + m32 = v[3]; + return; + case 3: + m03 = v[0]; + m13 = v[1]; + m23 = v[2]; + m33 = v[3]; + return; + default: + err(); + } + } + + /** + * Copies the matrix values in the specified column into the array parameter. + * + * @param column + * the matrix column + * @param v + * The array into which the matrix column values will be copied + */ + public void getColumn(int column, float v[]) { + if (column < 3) + getColumn33(column, v); + switch (column) { + case 0: + v[3] = m30; + return; + case 1: + v[3] = m31; + return; + case 2: + v[3] = m32; + return; + case 3: + v[0] = m03; + v[1] = m13; + v[2] = m23; + v[3] = m33; + return; + default: + err(); + } + } + + /** + * Sets the value of this matrix to the matrix difference of itself and matrix + * m1 (this = this - m1). + * + * @param m1 + * the other matrix + */ + public void sub(M4 m1) { + sub33(m1); + m03 -= m1.m03; + m13 -= m1.m13; + m23 -= m1.m23; + m30 -= m1.m30; + m31 -= m1.m31; + m32 -= m1.m32; + m33 -= m1.m33; + } + + /** + * Sets the value of this matrix to its transpose. + */ + public void transpose() { + transpose33(); + float tmp = m03; + m03 = m30; + m30 = tmp; + + tmp = m13; + m13 = m31; + m31 = tmp; + + tmp = m23; + m23 = m32; + m32 = tmp; + } + + /** + * Sets the value of this matrix to its inverse. + * @return this + */ + public M4 invert() { + float s = determinant4(); + if (s == 0.0) + return this; + s = 1 / s; + // alias-safe way. + // less *,+,- calculation than expanded expression. + set(m11 * (m22 * m33 - m23 * m32) + m12 * (m23 * m31 - m21 * m33) + m13 + * (m21 * m32 - m22 * m31), m21 * (m02 * m33 - m03 * m32) + m22 + * (m03 * m31 - m01 * m33) + m23 * (m01 * m32 - m02 * m31), m31 + * (m02 * m13 - m03 * m12) + m32 * (m03 * m11 - m01 * m13) + m33 + * (m01 * m12 - m02 * m11), m01 * (m13 * m22 - m12 * m23) + m02 + * (m11 * m23 - m13 * m21) + m03 * (m12 * m21 - m11 * m22), + + m12 * (m20 * m33 - m23 * m30) + m13 * (m22 * m30 - m20 * m32) + m10 + * (m23 * m32 - m22 * m33), m22 * (m00 * m33 - m03 * m30) + m23 + * (m02 * m30 - m00 * m32) + m20 * (m03 * m32 - m02 * m33), m32 + * (m00 * m13 - m03 * m10) + m33 * (m02 * m10 - m00 * m12) + m30 + * (m03 * m12 - m02 * m13), m02 * (m13 * m20 - m10 * m23) + m03 + * (m10 * m22 - m12 * m20) + m00 * (m12 * m23 - m13 * m22), + + m13 * (m20 * m31 - m21 * m30) + m10 * (m21 * m33 - m23 * m31) + m11 + * (m23 * m30 - m20 * m33), m23 * (m00 * m31 - m01 * m30) + m20 + * (m01 * m33 - m03 * m31) + m21 * (m03 * m30 - m00 * m33), m33 + * (m00 * m11 - m01 * m10) + m30 * (m01 * m13 - m03 * m11) + m31 + * (m03 * m10 - m00 * m13), m03 * (m11 * m20 - m10 * m21) + m00 + * (m13 * m21 - m11 * m23) + m01 * (m10 * m23 - m13 * m20), + + m10 * (m22 * m31 - m21 * m32) + m11 * (m20 * m32 - m22 * m30) + m12 + * (m21 * m30 - m20 * m31), m20 * (m02 * m31 - m01 * m32) + m21 + * (m00 * m32 - m02 * m30) + m22 * (m01 * m30 - m00 * m31), m30 + * (m02 * m11 - m01 * m12) + m31 * (m00 * m12 - m02 * m10) + m32 + * (m01 * m10 - m00 * m11), m00 * (m11 * m22 - m12 * m21) + m01 + * (m12 * m20 - m10 * m22) + m02 * (m10 * m21 - m11 * m20)); + scale(s); + return this; + } + + /** + * Sets 16 values + * + * @param m00 + * @param m01 + * @param m02 + * @param m03 + * @param m10 + * @param m11 + * @param m12 + * @param m13 + * @param m20 + * @param m21 + * @param m22 + * @param m23 + * @param m30 + * @param m31 + * @param m32 + * @param m33 + */ + private void set(float m00, float m01, float m02, float m03, float m10, + float m11, float m12, float m13, float m20, float m21, + float m22, float m23, float m30, float m31, float m32, + float m33) { + this.m00 = m00; + this.m01 = m01; + this.m02 = m02; + this.m03 = m03; + this.m10 = m10; + this.m11 = m11; + this.m12 = m12; + this.m13 = m13; + this.m20 = m20; + this.m21 = m21; + this.m22 = m22; + this.m23 = m23; + this.m30 = m30; + this.m31 = m31; + this.m32 = m32; + this.m33 = m33; + } + /** + * Computes the determinant of this matrix. + * + * @return the determinant of the matrix + */ + public float determinant4() { + // less *,+,- calculation than expanded expression. + return (m00 * m11 - m01 * m10) * (m22 * m33 - m23 * m32) + - (m00 * m12 - m02 * m10) * (m21 * m33 - m23 * m31) + + (m00 * m13 - m03 * m10) * (m21 * m32 - m22 * m31) + + (m01 * m12 - m02 * m11) * (m20 * m33 - m23 * m30) + - (m01 * m13 - m03 * m11) * (m20 * m32 - m22 * m30) + + (m02 * m13 - m03 * m12) * (m20 * m31 - m21 * m30); + + } + + /** + * Multiplies each element of this matrix by a scalar. + * + * @param scalar + * The scalar multiplier. + */ + public void scale(float scalar) { + mul33(scalar); + m03 *= scalar; + m13 *= scalar; + m23 *= scalar; + m30 *= scalar; + m31 *= scalar; + m32 *= scalar; + m33 *= scalar; + } + + /** + * Sets the value of this matrix to the result of multiplying itself with + * matrix m1. + * + * @param m1 + * the other matrix + */ + public void mul(M4 m1) { + mul2(this, m1); + } + + /** + * Sets the value of this matrix to the result of multiplying the two argument + * matrices together. + * + * @param m1 + * the first matrix + * @param m2 + * the second matrix + */ + public void mul2(M4 m1, M4 m2) { + // alias-safe way. + set(m1.m00 * m2.m00 + m1.m01 * m2.m10 + m1.m02 * m2.m20 + m1.m03 * m2.m30, + m1.m00 * m2.m01 + m1.m01 * m2.m11 + m1.m02 * m2.m21 + m1.m03 * m2.m31, + m1.m00 * m2.m02 + m1.m01 * m2.m12 + m1.m02 * m2.m22 + m1.m03 * m2.m32, + m1.m00 * m2.m03 + m1.m01 * m2.m13 + m1.m02 * m2.m23 + m1.m03 * m2.m33, + + m1.m10 * m2.m00 + m1.m11 * m2.m10 + m1.m12 * m2.m20 + m1.m13 * m2.m30, + m1.m10 * m2.m01 + m1.m11 * m2.m11 + m1.m12 * m2.m21 + m1.m13 * m2.m31, + m1.m10 * m2.m02 + m1.m11 * m2.m12 + m1.m12 * m2.m22 + m1.m13 * m2.m32, + m1.m10 * m2.m03 + m1.m11 * m2.m13 + m1.m12 * m2.m23 + m1.m13 * m2.m33, + + m1.m20 * m2.m00 + m1.m21 * m2.m10 + m1.m22 * m2.m20 + m1.m23 * m2.m30, + m1.m20 * m2.m01 + m1.m21 * m2.m11 + m1.m22 * m2.m21 + m1.m23 * m2.m31, + m1.m20 * m2.m02 + m1.m21 * m2.m12 + m1.m22 * m2.m22 + m1.m23 * m2.m32, + m1.m20 * m2.m03 + m1.m21 * m2.m13 + m1.m22 * m2.m23 + m1.m23 * m2.m33, + + m1.m30 * m2.m00 + m1.m31 * m2.m10 + m1.m32 * m2.m20 + m1.m33 * m2.m30, + m1.m30 * m2.m01 + m1.m31 * m2.m11 + m1.m32 * m2.m21 + m1.m33 * m2.m31, + m1.m30 * m2.m02 + m1.m31 * m2.m12 + m1.m32 * m2.m22 + m1.m33 * m2.m32, + m1.m30 * m2.m03 + m1.m31 * m2.m13 + m1.m32 * m2.m23 + m1.m33 * m2.m33); + } + + /** + * Transform the vector vec using this Matrix4f and place the result back into + * vec. + * + * @param vec + * the single precision vector to be transformed + */ + public void transform(T4 vec) { + transform2(vec, vec); + } + + /** + * Transform the vector vec using this Matrix4f and place the result into + * vecOut. + * + * @param vec + * the single precision vector to be transformed + * @param vecOut + * the vector into which the transformed values are placed + */ + public void transform2(T4 vec, T4 vecOut) { + // alias-safe + vecOut.set4(m00 * vec.x + m01 * vec.y + m02 * vec.z + m03 * vec.w, m10 + * vec.x + m11 * vec.y + m12 * vec.z + m13 * vec.w, m20 * vec.x + m21 + * vec.y + m22 * vec.z + m23 * vec.w, m30 * vec.x + m31 * vec.y + m32 + * vec.z + m33 * vec.w); + } + + /** + * Transforms the point parameter with this Matrix4f and places the result + * back into point. The fourth element of the point input parameter is assumed + * to be one. + * + * @param point + * the input point to be transformed. + */ + public void rotTrans(T3 point) { + rotTrans2(point, point); + } + + /** + * Transforms the point parameter with this Matrix4f and places the result + * into pointOut. The fourth element of the point input parameter is assumed to + * be one. point may be pointOut + * + * @param point + * the input point to be transformed. + * @param pointOut + * the transformed point + * @return pointOut + */ + public T3 rotTrans2(T3 point, T3 pointOut) { + pointOut.set( + m00 * point.x + m01 * point.y + m02 * point.z + m03, + m10 * point.x + m11 * point.y + m12 * point.z + m13, + m20 * point.x + m21 * point.y + m22 * point.z + m23); + return pointOut; + } + + /** + * Sets the value of this matrix to a rotation matrix about the w axis by the + * passed angle. + * + * @param angle + * the angle to rotate about the W axis in radians + * @return this + */ + public M4 setAsXYRotation(float angle) { + setIdentity(); + double c = Math.cos(angle); + double s = Math.sin(angle); + m22 = (float) c; + m23 = (float) -s; + m32 = (float) s; + m33 = (float) c; + return this; + } + + /** + * Sets the value of this matrix to a rotation matrix about the w axis by the + * passed angle. + * + * @param angle + * the angle to rotate about the W axis in radians + * @return this + */ + public M4 setAsYZRotation(float angle) { + setIdentity(); + double c = Math.cos(angle); + double s = Math.sin(angle); + m00 = (float) c; + m03 = (float) -s; + m30 = (float) s; + m33 = (float) c; + return this; + } + + /** + * Sets the value of this matrix to a rotation matrix about the w axis by the + * passed angle. + * + * @param angle + * the angle to rotate about the W axis in radians + * @return this + */ + public M4 setAsXZRotation(float angle) { + setIdentity(); + double c = Math.cos(angle); + double s = Math.sin(angle); + m11 = (float) c; + m13 = (float) -s; + m31 = (float) s; + m33 = (float) c; + return this; + } + + /** + * Returns true if the Object o is of type Matrix4f and all of the data + * members of t1 are equal to the corresponding data members in this Matrix4f. + * + * @param o + * the object with which the comparison is made. + */ + @Override + public boolean equals(Object o) { + if (!(o instanceof M4)) + return false; + M4 m = (M4) o; + return (this.m00 == m.m00 && this.m01 == m.m01 && this.m02 == m.m02 + && this.m03 == m.m03 && this.m10 == m.m10 && this.m11 == m.m11 + && this.m12 == m.m12 && this.m13 == m.m13 && this.m20 == m.m20 + && this.m21 == m.m21 && this.m22 == m.m22 && this.m23 == m.m23 + && this.m30 == m.m30 && this.m31 == m.m31 && this.m32 == m.m32 && this.m33 == m.m33); + } + + /** + * Returns a hash number based on the data values in this object. Two + * different Matrix4f objects with identical data values (ie, returns true for + * equals(Matrix4f) ) will return the same hash number. Two objects with + * different data members may return the same hash value, although this is not + * likely. + * + * @return the integer hash value + */ + @Override + public int hashCode() { + return T3.floatToIntBits(m00) ^ T3.floatToIntBits(m01) + ^ T3.floatToIntBits(m02) ^ T3.floatToIntBits(m03) + ^ T3.floatToIntBits(m10) ^ T3.floatToIntBits(m11) + ^ T3.floatToIntBits(m12) ^ T3.floatToIntBits(m13) + ^ T3.floatToIntBits(m20) ^ T3.floatToIntBits(m21) + ^ T3.floatToIntBits(m22) ^ T3.floatToIntBits(m23) + ^ T3.floatToIntBits(m30) ^ T3.floatToIntBits(m31) + ^ T3.floatToIntBits(m32) ^ T3.floatToIntBits(m33); + } + + /** + * Returns a string that contains the values of this Matrix4f. + * + * @return the String representation + */ + @Override + public String toString() { + return "[\n [" + m00 + "\t" + m01 + "\t" + m02 + "\t" + m03 + "]" + + "\n [" + m10 + "\t" + m11 + "\t" + m12 + "\t" + m13 + "]" + "\n [" + + m20 + "\t" + m21 + "\t" + m22 + "\t" + m23 + "]" + "\n [" + m30 + + "\t" + m31 + "\t" + m32 + "\t" + m33 + "] ]"; + } + public M4 round(float f) { + m00 = rnd(m00, f); + m01 = rnd(m01, f); + m02 = rnd(m02, f); + m03 = rnd(m03, f); + m10 = rnd(m10, f); + m11 = rnd(m11, f); + m12 = rnd(m12, f); + m13 = rnd(m13, f); + m20 = rnd(m20, f); + m21 = rnd(m21, f); + m22 = rnd(m22, f); + m23 = rnd(m23, f); + m30 = rnd(m30, f); + m31 = rnd(m31, f); + m32 = rnd(m32, f); + m33 = rnd(m33, f); + return this; + } + + private float rnd(float n, float f) { + return (Math.abs(n) < f ? 0 : n); + } +} diff --git a/src/javajs/util/Matrix.java b/src/javajs/util/Matrix.java new file mode 100644 index 0000000..cea5a04 --- /dev/null +++ b/src/javajs/util/Matrix.java @@ -0,0 +1,457 @@ +package javajs.util; + +/** + * + * streamlined and refined for Jmol by Bob Hanson + * + * from http://math.nist.gov/javanumerics/jama/ + * + * Jama = Java Matrix class. + * + * @author The MathWorks, Inc. and the National Institute of Standards and + * Technology. + * @version 5 August 1998 + */ + +public class Matrix implements Cloneable { + + public double[][] a; + protected int m, n; + + /** + * Construct a matrix quickly without checking arguments. + * + * @param a + * Two-dimensional array of doubles or null + * @param m + * Number of rows. + * @param n + * Number of colums. + */ + + public Matrix(double[][] a, int m, int n) { + this.a = (a == null ? new double[m][n] : a); + this.m = m; + this.n = n; + } + + /** + * Get row dimension. + * + * @return m, the number of rows. + */ + + public int getRowDimension() { + return m; + } + + /** + * Get column dimension. + * + * @return n, the number of columns. + */ + + public int getColumnDimension() { + return n; + } + + /** + * Access the internal two-dimensional array. + * + * @return Pointer to the two-dimensional array of matrix elements. + */ + + public double[][] getArray() { + return a; + } + + /** + * Copy the internal two-dimensional array. + * + * @return Two-dimensional array copy of matrix elements. + */ + + public double[][] getArrayCopy() { + double[][] x = new double[m][n]; + for (int i = m; --i >= 0;) + for (int j = n; --j >= 0;) + x[i][j] = a[i][j]; + return x; + } + + /** + * Make a deep copy of a matrix + * + * @return copy + */ + + public Matrix copy() { + Matrix x = new Matrix(null, m, n); + double[][] c = x.a; + for (int i = m; --i >= 0;) + for (int j = n; --j >= 0;) + c[i][j] = a[i][j]; + return x; + } + + /** + * Clone the Matrix object. + */ + + @Override + public Object clone() { + return copy(); + } + + /** + * Get a submatrix. + * + * @param i0 + * Initial row index + * @param j0 + * Initial column index + * @param nrows + * Number of rows + * @param ncols + * Number of columns + * @return submatrix + * + */ + + public Matrix getSubmatrix(int i0, int j0, int nrows, int ncols) { + Matrix x = new Matrix(null, nrows, ncols); + double[][] xa = x.a; + for (int i = nrows; --i >= 0;) + for (int j = ncols; --j >= 0;) + xa[i][j] = a[i0 + i][j0 + j]; + return x; + } + + /** + * Get a submatrix for a give number of columns and selected row set. + * + * @param r + * Array of row indices. + * @param n + * number of rows + * @return submatrix + */ + + public Matrix getMatrixSelected(int[] r, int n) { + Matrix x = new Matrix(null, r.length, n); + double[][] xa = x.a; + for (int i = r.length; --i >= 0;) { + double[] b = a[r[i]]; + for (int j = n; --j >= 0;) + xa[i][j] = b[j]; + } + return x; + } + + /** + * Matrix transpose. + * + * @return A' + */ + + public Matrix transpose() { + Matrix x = new Matrix(null, n, m); + double[][] c = x.a; + for (int i = m; --i >= 0;) + for (int j = n; --j >= 0;) + c[j][i] = a[i][j]; + return x; + } + + /** + * add two matrices + * @param b + * @return new Matrix this + b + */ + public Matrix add(Matrix b) { + return scaleAdd(b, 1); + } + + /** + * subtract two matrices + * @param b + * @return new Matrix this - b + */ + public Matrix sub(Matrix b) { + return scaleAdd(b, -1); + } + + /** + * X = A + B*scale + * @param b + * @param scale + * @return X + * + */ + public Matrix scaleAdd(Matrix b, double scale) { + Matrix x = new Matrix(null, m, n); + double[][] xa = x.a; + double[][] ba = b.a; + for (int i = m; --i >= 0;) + for (int j = n; --j >= 0;) + xa[i][j] = ba[i][j] * scale + a[i][j]; + return x; + } + + /** + * Linear algebraic matrix multiplication, A * B + * + * @param b + * another matrix + * @return Matrix product, A * B or null for wrong dimension + */ + + public Matrix mul(Matrix b) { + if (b.m != n) + return null; + Matrix x = new Matrix(null, m, b.n); + double[][] xa = x.a; + double[][] ba = b.a; + for (int j = b.n; --j >= 0;) + for (int i = m; --i >= 0;) { + double[] arowi = a[i]; + double s = 0; + for (int k = n; --k >= 0;) + s += arowi[k] * ba[k][j]; + xa[i][j] = s; + } + return x; + } + + /** + * Matrix inverse or pseudoinverse + * + * @return inverse (m == n) or pseudoinverse (m != n) + */ + + public Matrix inverse() { + return new LUDecomp(m, n).solve(identity(m, m), n); + } + + /** + * Matrix trace. + * + * @return sum of the diagonal elements. + */ + + public double trace() { + double t = 0; + for (int i = Math.min(m, n); --i >= 0;) + t += a[i][i]; + return t; + } + + /** + * Generate identity matrix + * + * @param m + * Number of rows. + * @param n + * Number of columns. + * @return An m-by-n matrix with ones on the diagonal and zeros elsewhere. + */ + + public static Matrix identity(int m, int n) { + Matrix x = new Matrix(null, m, n); + double[][] xa = x.a; + for (int i = Math.min(m, n); --i >= 0;) + xa[i][i] = 1; + return x; + } + + /** + * similarly to M3/M4 standard rotation/translation matrix + * we set a rotationTranslation matrix to be: + * + * [ nxn rot nx1 trans + * + * 1xn 0 1x1 1 ] + * + * + * @return rotation matrix + */ + public Matrix getRotation() { + return getSubmatrix(0, 0, m - 1, n - 1); + } + + public Matrix getTranslation() { + return getSubmatrix(0, n - 1, m - 1, 1); + } + + public static Matrix newT(T3 r, boolean asColumn) { + return (asColumn ? new Matrix(new double[][] { new double[] { r.x }, + new double[] { r.y }, new double[] { r.z } }, 3, 1) : new Matrix( + new double[][] { new double[] { r.x, r.y, r.z } }, 1, 3)); + } + + @Override + public String toString() { + String s = "[\n"; + for (int i = 0; i < m; i++) { + s += " ["; + for (int j = 0; j < n; j++) + s += " " + a[i][j]; + s += "]\n"; + } + s += "]"; + return s; + } + + /** + * + * Edited down by Bob Hanson for minimum needed by Jmol -- just constructor + * and solve + * + * LU Decomposition. + *

+ * For an m-by-n matrix A with m >= n, the LU decomposition is an m-by-n unit + * lower triangular matrix L, an n-by-n upper triangular matrix U, and a + * permutation vector piv of length m so that A(piv,:) = L*U. If m < n, then L + * is m-by-m and U is m-by-n. + *

+ * The LU decompostion with pivoting always exists, even if the matrix is + * singular, so the constructor will never fail. The primary use of the LU + * decomposition is in the solution of square systems of simultaneous linear + * equations. This will fail if isNonsingular() returns false. + */ + + private class LUDecomp { + + /* ------------------------ + Class variables + * ------------------------ */ + + /** + * Array for internal storage of decomposition. + * + */ + private double[][] LU; + + /** + * Internal storage of pivot vector. + * + */ + private int[] piv; + + private int pivsign; + + /* ------------------------ + Constructor + * ------------------------ */ + + /** + * LU Decomposition Structure to access L, U and piv. + * @param m + * @param n + * + */ + + protected LUDecomp(int m, int n) { + + // Use a "left-looking", dot-product, Crout/Doolittle algorithm. + + LU = getArrayCopy(); + piv = new int[m]; + for (int i = m; --i >= 0;) + piv[i] = i; + pivsign = 1; + double[] LUrowi; + double[] LUcolj = new double[m]; + + // Outer loop. + + for (int j = 0; j < n; j++) { + + // Make a copy of the j-th column to localize references. + + for (int i = m; --i >= 0;) + LUcolj[i] = LU[i][j]; + + // Apply previous transformations. + + for (int i = m; --i >= 0;) { + LUrowi = LU[i]; + + // Most of the time is spent in the following dot product. + + int kmax = Math.min(i, j); + double s = 0.0; + for (int k = kmax; --k >= 0;) + s += LUrowi[k] * LUcolj[k]; + + LUrowi[j] = LUcolj[i] -= s; + } + + // Find pivot and exchange if necessary. + + int p = j; + for (int i = m; --i > j;) + if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) + p = i; + if (p != j) { + for (int k = n; --k >= 0;) { + double t = LU[p][k]; + LU[p][k] = LU[j][k]; + LU[j][k] = t; + } + int k = piv[p]; + piv[p] = piv[j]; + piv[j] = k; + pivsign = -pivsign; + } + + // Compute multipliers. + + if (j < m & LU[j][j] != 0.0) + for (int i = m; --i > j;) + LU[i][j] /= LU[j][j]; + } + } + + /* ------------------------ + default Methods + * ------------------------ */ + + /** + * Solve A*X = B + * + * @param b + * A Matrix with as many rows as A and any number of columns. + * @param n + * @return X so that L*U*X = B(piv,:) or null for wrong size or singular matrix + */ + + protected Matrix solve(Matrix b, int n) { + for (int j = 0; j < n; j++) + if (LU[j][j] == 0) + return null; // matrix is singular + + // Copy right hand side with pivoting + int nx = b.n; + Matrix x = b.getMatrixSelected(piv, nx); + double[][] a = x.a; + + // Solve L*Y = B(piv,:) + for (int k = 0; k < n; k++) + for (int i = k + 1; i < n; i++) + for (int j = 0; j < nx; j++) + a[i][j] -= a[k][j] * LU[i][k]; + + // Solve U*X = Y; + for (int k = n; --k >= 0;) { + for (int j = nx; --j >= 0;) + a[k][j] /= LU[k][k]; + for (int i = k; --i >= 0;) + for (int j = nx; --j >= 0;) + a[i][j] -= a[k][j] * LU[i][k]; + } + return x; + } + } + +} diff --git a/src/javajs/util/Measure.java b/src/javajs/util/Measure.java new file mode 100644 index 0000000..8fd799b --- /dev/null +++ b/src/javajs/util/Measure.java @@ -0,0 +1,729 @@ +/* $RCSfile$ + * $Author: egonw $ + * $Date: 2005-11-10 09:52:44 -0600 (Thu, 10 Nov 2005) $ + * $Revision: 4255 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2003-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.util; + +import javajs.api.EigenInterface; + +import javajs.api.Interface; + + + + +//import org.jmol.script.T; + +final public class Measure { + + public final static float radiansPerDegree = (float) (2 * Math.PI / 360); + + public static float computeAngle(T3 pointA, T3 pointB, T3 pointC, V3 vectorBA, V3 vectorBC, boolean asDegrees) { + vectorBA.sub2(pointA, pointB); + vectorBC.sub2(pointC, pointB); + float angle = vectorBA.angle(vectorBC); + return (asDegrees ? angle / radiansPerDegree : angle); + } + + public static float computeAngleABC(T3 pointA, T3 pointB, T3 pointC, boolean asDegrees) { + V3 vectorBA = new V3(); + V3 vectorBC = new V3(); + return computeAngle(pointA, pointB, pointC, vectorBA, vectorBC, asDegrees); + } + + public static float computeTorsion(T3 p1, T3 p2, T3 p3, T3 p4, boolean asDegrees) { + + float ijx = p1.x - p2.x; + float ijy = p1.y - p2.y; + float ijz = p1.z - p2.z; + + float kjx = p3.x - p2.x; + float kjy = p3.y - p2.y; + float kjz = p3.z - p2.z; + + float klx = p3.x - p4.x; + float kly = p3.y - p4.y; + float klz = p3.z - p4.z; + + float ax = ijy * kjz - ijz * kjy; + float ay = ijz * kjx - ijx * kjz; + float az = ijx * kjy - ijy * kjx; + float cx = kjy * klz - kjz * kly; + float cy = kjz * klx - kjx * klz; + float cz = kjx * kly - kjy * klx; + + float ai2 = 1f / (ax * ax + ay * ay + az * az); + float ci2 = 1f / (cx * cx + cy * cy + cz * cz); + + float ai = (float) Math.sqrt(ai2); + float ci = (float) Math.sqrt(ci2); + float denom = ai * ci; + float cross = ax * cx + ay * cy + az * cz; + float cosang = cross * denom; + if (cosang > 1) { + cosang = 1; + } + if (cosang < -1) { + cosang = -1; + } + + float torsion = (float) Math.acos(cosang); + float dot = ijx * cx + ijy * cy + ijz * cz; + float absDot = Math.abs(dot); + torsion = (dot / absDot > 0) ? torsion : -torsion; + return (asDegrees ? torsion / radiansPerDegree : torsion); + } + + /** + * This method calculates measures relating to two points in space + * with related quaternion frame difference. It is used in Jmol for + * calculating straightness and many other helical quantities. + * + * @param a + * @param b + * @param dq + * @return new T3[] { pt_a_prime, n, r, P3.new3(theta, pitch, residuesPerTurn), pt_b_prime }; + */ + public static T3[] computeHelicalAxis(P3 a, P3 b, Quat dq) { + + // b + // | /| + // | / | + // | / | + // |/ c + // b'+ / \ + // | / \ Vcb = Vab . n + // n | / \d Vda = (Vcb - Vab) / 2 + // |/theta \ + // a'+---------a + // r + + V3 vab = new V3(); + vab.sub2(b, a); + /* + * testing here to see if directing the normal makes any difference -- oddly + * enough, it does not. When n = -n and theta = -theta vab.n is reversed, + * and that magnitude is multiplied by n in generating the A'-B' vector. + * + * a negative angle implies a left-handed axis (sheets) + */ + float theta = dq.getTheta(); + V3 n = dq.getNormal(); + float v_dot_n = vab.dot(n); + if (Math.abs(v_dot_n) < 0.0001f) + v_dot_n = 0; + V3 va_prime_d = new V3(); + va_prime_d.cross(vab, n); + if (va_prime_d.dot(va_prime_d) != 0) + va_prime_d.normalize(); + V3 vda = new V3(); + V3 vcb = V3.newV(n); + if (v_dot_n == 0) + v_dot_n = PT.FLOAT_MIN_SAFE; // allow for perpendicular axis to vab + vcb.scale(v_dot_n); + vda.sub2(vcb, vab); + vda.scale(0.5f); + va_prime_d.scale(theta == 0 ? 0 : (float) (vda.length() / Math.tan(theta + / 2 / 180 * Math.PI))); + V3 r = V3.newV(va_prime_d); + if (theta != 0) + r.add(vda); + P3 pt_a_prime = P3.newP(a); + pt_a_prime.sub(r); + // already done this. ?? + if (v_dot_n != PT.FLOAT_MIN_SAFE) + n.scale(v_dot_n); + // must calculate directed angle: + P3 pt_b_prime = P3.newP(pt_a_prime); + pt_b_prime.add(n); + theta = computeTorsion(a, pt_a_prime, pt_b_prime, b, true); + if (Float.isNaN(theta) || r.length() < 0.0001f) + theta = dq.getThetaDirectedV(n); // allow for r = 0 + // anything else is an array + float residuesPerTurn = Math.abs(theta == 0 ? 0 : 360f / theta); + float pitch = Math.abs(v_dot_n == PT.FLOAT_MIN_SAFE ? 0 : n.length() + * (theta == 0 ? 1 : 360f / theta)); + return new T3[] { pt_a_prime, n, r, P3.new3(theta, pitch, residuesPerTurn), pt_b_prime }; + } + + public static P4 getPlaneThroughPoints(T3 pointA, + T3 pointB, + T3 pointC, V3 vNorm, + V3 vAB, P4 plane) { + float w = getNormalThroughPoints(pointA, pointB, pointC, vNorm, vAB); + plane.set4(vNorm.x, vNorm.y, vNorm.z, w); + return plane; + } + + public static void getPlaneThroughPoint(T3 pt, V3 normal, P4 plane) { + plane.set4(normal.x, normal.y, normal.z, -normal.dot(pt)); + } + + public static float distanceToPlane(P4 plane, T3 pt) { + return (plane == null ? Float.NaN + : (plane.dot(pt) + plane.w) / (float) Math.sqrt(plane.dot(plane))); + } + + public static float directedDistanceToPlane(P3 pt, P4 plane, P3 ptref) { + float f = plane.dot(pt) + plane.w; + float f1 = plane.dot(ptref) + plane.w; + return Math.signum(f1) * f / (float) Math.sqrt(plane.dot(plane)); + } + + public static float distanceToPlaneD(P4 plane, float d, P3 pt) { + return (plane == null ? Float.NaN : (plane.dot(pt) + plane.w) / d); + } + + public static float distanceToPlaneV(V3 norm, float w, P3 pt) { + return (norm == null ? Float.NaN + : (norm.dot(pt) + w) / (float) Math.sqrt(norm.dot(norm))); + } + + /** + * note that if vAB or vAC is dispensible, vNormNorm can be one of them + * @param pointA + * @param pointB + * @param pointC + * @param vNormNorm + * @param vAB + */ + public static void calcNormalizedNormal(T3 pointA, T3 pointB, + T3 pointC, T3 vNormNorm, T3 vAB) { + vAB.sub2(pointB, pointA); + vNormNorm.sub2(pointC, pointA); + vNormNorm.cross(vAB, vNormNorm); + vNormNorm.normalize(); + } + + public static float getDirectedNormalThroughPoints(T3 pointA, + T3 pointB, T3 pointC, T3 ptRef, V3 vNorm, + V3 vAB) { + // for x = plane({atomno=1}, {atomno=2}, {atomno=3}, {atomno=4}) + float nd = getNormalThroughPoints(pointA, pointB, pointC, vNorm, vAB); + if (ptRef != null) { + P3 pt0 = P3.newP(pointA); + pt0.add(vNorm); + float d = pt0.distance(ptRef); + pt0.sub2(pointA, vNorm); + if (d > pt0.distance(ptRef)) { + vNorm.scale(-1); + nd = -nd; + } + } + return nd; + } + + /** + * if vAC is dispensible vNorm can be vAC + * @param pointA + * @param pointB + * @param pointC + * @param vNorm + * @param vTemp + * @return w + */ + public static float getNormalThroughPoints(T3 pointA, T3 pointB, + T3 pointC, T3 vNorm, T3 vTemp) { + // for Polyhedra + calcNormalizedNormal(pointA, pointB, pointC, vNorm, vTemp); + // ax + by + cz + d = 0 + // so if a point is in the plane, then N dot X = -d + vTemp.setT(pointA); + return -vTemp.dot(vNorm); + } + + public static void getPlaneProjection(P3 pt, P4 plane, P3 ptProj, V3 vNorm) { + float dist = distanceToPlane(plane, pt); + vNorm.set(plane.x, plane.y, plane.z); + vNorm.normalize(); + vNorm.scale(-dist); + ptProj.add2(pt, vNorm); + } + + /** + * + * @param ptCenter + * @param ptA + * @param ptB + * @param ptC + * @param isOutward + * @param normal set to be opposite to direction of ptCenter from ABC + * @param vTemp + * @return true if winding is CCW; false if CW + */ + public static boolean getNormalFromCenter(P3 ptCenter, P3 ptA, P3 ptB, P3 ptC, + boolean isOutward, V3 normal, V3 vTemp) { + float d = getNormalThroughPoints(ptA, ptB, ptC, normal, vTemp); + boolean isReversed = (distanceToPlaneV(normal, d, ptCenter) > 0); + if (isReversed == isOutward) + normal.scale(-1f); + return !isReversed; + } + + public final static V3 axisY = V3.new3(0, 1, 0); + + public static void getNormalToLine(P3 pointA, P3 pointB, + V3 vNormNorm) { + // vector in xy plane perpendicular to a line between two points RMH + vNormNorm.sub2(pointA, pointB); + vNormNorm.cross(vNormNorm, axisY); + vNormNorm.normalize(); + if (Float.isNaN(vNormNorm.x)) + vNormNorm.set(1, 0, 0); + } + + public static void getBisectingPlane(P3 pointA, V3 vAB, + T3 ptTemp, V3 vTemp, P4 plane) { + ptTemp.scaleAdd2(0.5f, vAB, pointA); + vTemp.setT(vAB); + vTemp.normalize(); + getPlaneThroughPoint(ptTemp, vTemp, plane); + } + + public static void projectOntoAxis(P3 point, P3 axisA, + V3 axisUnitVector, + V3 vectorProjection) { + vectorProjection.sub2(point, axisA); + float projectedLength = vectorProjection.dot(axisUnitVector); + point.scaleAdd2(projectedLength, axisUnitVector, axisA); + vectorProjection.sub2(point, axisA); + } + + public static void calcBestAxisThroughPoints(P3[] points, P3 axisA, + V3 axisUnitVector, + V3 vectorProjection, + int nTriesMax) { + // just a crude starting point. + + int nPoints = points.length; + axisA.setT(points[0]); + axisUnitVector.sub2(points[nPoints - 1], axisA); + axisUnitVector.normalize(); + + /* + * We now calculate the least-squares 3D axis + * through the helix alpha carbons starting with Vo + * as a first approximation. + * + * This uses the simple 0-centered least squares fit: + * + * Y = M cross Xi + * + * minimizing R^2 = SUM(|Y - Yi|^2) + * + * where Yi is the vector PERPENDICULAR of the point onto axis Vo + * and Xi is the vector PROJECTION of the point onto axis Vo + * and M is a vector adjustment + * + * M = SUM_(Xi cross Yi) / sum(|Xi|^2) + * + * from which we arrive at: + * + * V = Vo + (M cross Vo) + * + * Basically, this is just a 3D version of a + * standard 2D least squares fit to a line, where we would say: + * + * y = m xi + b + * + * D = n (sum xi^2) - (sum xi)^2 + * + * m = [(n sum xiyi) - (sum xi)(sum yi)] / D + * b = [(sum yi) (sum xi^2) - (sum xi)(sum xiyi)] / D + * + * but here we demand that the line go through the center, so we + * require (sum xi) = (sum yi) = 0, so b = 0 and + * + * m = (sum xiyi) / (sum xi^2) + * + * In 3D we do the same but + * instead of x we have Vo, + * instead of multiplication we use cross products + * + * A bit of iteration is necessary. + * + * Bob Hanson 11/2006 + * + */ + + calcAveragePointN(points, nPoints, axisA); + + int nTries = 0; + while (nTries++ < nTriesMax + && findAxis(points, nPoints, axisA, axisUnitVector, vectorProjection) > 0.001) { + } + + /* + * Iteration here gets the job done. + * We now find the projections of the endpoints onto the axis + * + */ + + P3 tempA = P3.newP(points[0]); + projectOntoAxis(tempA, axisA, axisUnitVector, vectorProjection); + axisA.setT(tempA); + } + + public static float findAxis(P3[] points, int nPoints, P3 axisA, + V3 axisUnitVector, V3 vectorProjection) { + V3 sumXiYi = new V3(); + V3 vTemp = new V3(); + P3 pt = new P3(); + P3 ptProj = new P3(); + V3 a = V3.newV(axisUnitVector); + + float sum_Xi2 = 0; + for (int i = nPoints; --i >= 0;) { + pt.setT(points[i]); + ptProj.setT(pt); + projectOntoAxis(ptProj, axisA, axisUnitVector, + vectorProjection); + vTemp.sub2(pt, ptProj); + //sum_Yi2 += vTemp.lengthSquared(); + vTemp.cross(vectorProjection, vTemp); + sumXiYi.add(vTemp); + sum_Xi2 += vectorProjection.lengthSquared(); + } + V3 m = V3.newV(sumXiYi); + m.scale(1 / sum_Xi2); + vTemp.cross(m, axisUnitVector); + axisUnitVector.add(vTemp); + axisUnitVector.normalize(); + //check for change in direction by measuring vector difference length + vTemp.sub2(axisUnitVector, a); + return vTemp.length(); + } + + + public static void calcAveragePoint(P3 pointA, P3 pointB, + P3 pointC) { + pointC.set((pointA.x + pointB.x) / 2, (pointA.y + pointB.y) / 2, + (pointA.z + pointB.z) / 2); + } + + public static void calcAveragePointN(P3[] points, int nPoints, + P3 averagePoint) { + averagePoint.setT(points[0]); + for (int i = 1; i < nPoints; i++) + averagePoint.add(points[i]); + averagePoint.scale(1f / nPoints); + } + + public static Lst transformPoints(Lst vPts, M4 m4, P3 center) { + Lst v = new Lst(); + for (int i = 0; i < vPts.size(); i++) { + P3 pt = P3.newP(vPts.get(i)); + pt.sub(center); + m4.rotTrans(pt); + pt.add(center); + v.addLast(pt); + } + return v; + } + + public static boolean isInTetrahedron(P3 pt, P3 ptA, P3 ptB, + P3 ptC, P3 ptD, + P4 plane, V3 vTemp, + V3 vTemp2, boolean fullyEnclosed) { + boolean b = (distanceToPlane(getPlaneThroughPoints(ptC, ptD, ptA, vTemp, vTemp2, plane), pt) >= 0); + if (b != (distanceToPlane(getPlaneThroughPoints(ptA, ptD, ptB, vTemp, vTemp2, plane), pt) >= 0)) + return false; + if (b != (distanceToPlane(getPlaneThroughPoints(ptB, ptD, ptC, vTemp, vTemp2, plane), pt) >= 0)) + return false; + float d = distanceToPlane(getPlaneThroughPoints(ptA, ptB, ptC, vTemp, vTemp2, plane), pt); + if (fullyEnclosed) + return (b == (d >= 0)); + float d1 = distanceToPlane(plane, ptD); + return d1 * d <= 0 || Math.abs(d1) > Math.abs(d); + } + + + /** + * + * @param plane1 + * @param plane2 + * @return [ point, vector ] or [] + */ + public static Lst getIntersectionPP(P4 plane1, P4 plane2) { + float a1 = plane1.x; + float b1 = plane1.y; + float c1 = plane1.z; + float d1 = plane1.w; + float a2 = plane2.x; + float b2 = plane2.y; + float c2 = plane2.z; + float d2 = plane2.w; + V3 norm1 = V3.new3(a1, b1, c1); + V3 norm2 = V3.new3(a2, b2, c2); + V3 nxn = new V3(); + nxn.cross(norm1, norm2); + float ax = Math.abs(nxn.x); + float ay = Math.abs(nxn.y); + float az = Math.abs(nxn.z); + float x, y, z, diff; + int type = (ax > ay ? (ax > az ? 1 : 3) : ay > az ? 2 : 3); + switch(type) { + case 1: + x = 0; + diff = (b1 * c2 - b2 * c1); + if (Math.abs(diff) < 0.01) return null; + y = (c1 * d2 - c2 * d1) / diff; + z = (b2 * d1 - d2 * b1) / diff; + break; + case 2: + diff = (a1 * c2 - a2 * c1); + if (Math.abs(diff) < 0.01) return null; + x = (c1 * d2 - c2 * d1) / diff; + y = 0; + z = (a2 * d1 - d2 * a1) / diff; + break; + case 3: + default: + diff = (a1 * b2 - a2 * b1); + if (Math.abs(diff) < 0.01) return null; + x = (b1 * d2 - b2 * d1) / diff; + y = (a2 * d1 - d2 * a1) / diff; + z = 0; + } + Lstlist = new Lst(); + list.addLast(P3.new3(x, y, z)); + nxn.normalize(); + list.addLast(nxn); + return list; + } + + /** + * + * @param pt1 point on line + * @param v unit vector of line + * @param plane + * @param ptRet point of intersection of line with plane + * @param tempNorm + * @param vTemp + * @return ptRtet + */ + public static P3 getIntersection(P3 pt1, V3 v, + P4 plane, P3 ptRet, V3 tempNorm, V3 vTemp) { + getPlaneProjection(pt1, plane, ptRet, tempNorm); + tempNorm.set(plane.x, plane.y, plane.z); + tempNorm.normalize(); + if (v == null) + v = V3.newV(tempNorm); + float l_dot_n = v.dot(tempNorm); + if (Math.abs(l_dot_n) < 0.01) return null; + vTemp.sub2(ptRet, pt1); + ptRet.scaleAdd2(vTemp.dot(tempNorm) / l_dot_n, v, pt1); + return ptRet; + } + + /* + * public static Point3f getTriangleIntersection(Point3f a1, Point3f a2, + * Point3f a3, Point4f plane, Point3f b1, Point3f b2, Point3f b3, Vector3f + * vNorm, Vector3f vTemp, Point3f ptRet, Point3f ptTemp, Vector3f vTemp2, + * Point4f pTemp, Vector3f vTemp3) { + * + * if (getTriangleIntersection(b1, b2, a1, a2, a3, vTemp, plane, vNorm, + * vTemp2, vTemp3, ptRet, ptTemp)) return ptRet; if + * (getTriangleIntersection(b2, b3, a1, a2, a3, vTemp, plane, vNorm, vTemp2, + * vTemp3, ptRet, ptTemp)) return ptRet; if (getTriangleIntersection(b3, b1, + * a1, a2, a3, vTemp, plane, vNorm, vTemp2, vTemp3, ptRet, ptTemp)) return + * ptRet; return null; } + */ + /* + * public static boolean getTriangleIntersection(Point3f b1, Point3f b2, + * Point3f a1, Point3f a2, Point3f a3, Vector3f vTemp, Point4f plane, Vector3f + * vNorm, Vector3f vTemp2, Vector3f vTemp3, Point3f ptRet, Point3f ptTemp) { + * if (distanceToPlane(plane, b1) * distanceToPlane(plane, b2) >= 0) return + * false; vTemp.sub(b2, b1); vTemp.normalize(); if (getIntersection(b1, vTemp, + * plane, ptRet, vNorm, vTemp2) != null) { if (isInTriangle(ptRet, a1, a2, a3, + * vTemp, vTemp2, vTemp3)) return true; } return false; } private static + * boolean isInTriangle(Point3f p, Point3f a, Point3f b, Point3f c, Vector3f + * v0, Vector3f v1, Vector3f v2) { // from + * http://www.blackpawn.com/texts/pointinpoly/default.html // Compute + * barycentric coordinates v0.sub(c, a); v1.sub(b, a); v2.sub(p, a); float + * dot00 = v0.dot(v0); float dot01 = v0.dot(v1); float dot02 = v0.dot(v2); + * float dot11 = v1.dot(v1); float dot12 = v1.dot(v2); float invDenom = 1 / + * (dot00 * dot11 - dot01 * dot01); float u = (dot11 * dot02 - dot01 * dot12) + * * invDenom; float v = (dot00 * dot12 - dot01 * dot02) * invDenom; return (u + * > 0 && v > 0 && u + v < 1); } + */ + + /** + * Closed-form solution of absolute orientation requiring 1:1 mapping of + * positions. + * + * @param centerAndPoints + * @param retStddev + * @return unit quaternion representation rotation + * + * @author hansonr Bob Hanson + * + */ + public static Quat calculateQuaternionRotation(P3[][] centerAndPoints, + float[] retStddev) { + /* + * see Berthold K. P. Horn, + * "Closed-form solution of absolute orientation using unit quaternions" J. + * Opt. Soc. Amer. A, 1987, Vol. 4, pp. 629-642 + * http://www.opticsinfobase.org/viewmedia.cfm?uri=josaa-4-4-629&seq=0 + * + * + * A similar treatment was developed independently (and later!) by G. + * Kramer, in G. R. Kramer, + * "Superposition of Molecular Structures Using Quaternions" Molecular + * Simulation, 1991, Vol. 7, pp. 113-119. + * + * In that treatment there is a lot of unnecessary calculation along the + * trace of matrix M (eqn 20). I'm not sure why the extra x^2 + y^2 + z^2 + + * x'^2 + y'^2 + z'^2 is in there, but they are unnecessary and only + * contribute to larger numerical averaging errors and additional processing + * time, as far as I can tell. Adding aI, where a is a scalar and I is the + * 4x4 identity just offsets the eigenvalues but doesn't change the + * eigenvectors. + * + * and Lydia E. Kavraki, "Molecular Distance Measures" + * http://cnx.org/content/m11608/latest/ + */ + + + retStddev[1] = Float.NaN; + Quat q = new Quat(); + P3[] ptsA = centerAndPoints[0]; + P3[] ptsB = centerAndPoints[1]; + int nPts = ptsA.length - 1; + if (nPts < 2 || ptsA.length != ptsB.length) + return q; + double Sxx = 0, Sxy = 0, Sxz = 0, Syx = 0, Syy = 0, Syz = 0, Szx = 0, Szy = 0, Szz = 0; + P3 ptA = new P3(); + P3 ptB = new P3(); + P3 ptA0 = ptsA[0]; + P3 ptB0 = ptsB[0]; + for (int i = nPts + 1; --i >= 1;) { + ptA.sub2(ptsA[i], ptA0); + ptB.sub2(ptsB[i], ptB0); + Sxx += (double) ptA.x * (double) ptB.x; + Sxy += (double) ptA.x * (double) ptB.y; + Sxz += (double) ptA.x * (double) ptB.z; + Syx += (double) ptA.y * (double) ptB.x; + Syy += (double) ptA.y * (double) ptB.y; + Syz += (double) ptA.y * (double) ptB.z; + Szx += (double) ptA.z * (double) ptB.x; + Szy += (double) ptA.z * (double) ptB.y; + Szz += (double) ptA.z * (double) ptB.z; + } + retStddev[0] = getRmsd(centerAndPoints, q); + double[][] N = new double[4][4]; + N[0][0] = Sxx + Syy + Szz; + N[0][1] = N[1][0] = Syz - Szy; + N[0][2] = N[2][0] = Szx - Sxz; + N[0][3] = N[3][0] = Sxy - Syx; + + N[1][1] = Sxx - Syy - Szz; + N[1][2] = N[2][1] = Sxy + Syx; + N[1][3] = N[3][1] = Szx + Sxz; + + N[2][2] = -Sxx + Syy - Szz; + N[2][3] = N[3][2] = Syz + Szy; + + N[3][3] = -Sxx - Syy + Szz; + + // this construction prevents JavaScript from requiring preloading of Eigen + + float[] v = ((EigenInterface) Interface.getInterface("javajs.util.Eigen")) + .setM(N).getEigenvectorsFloatTransposed()[3]; + q = Quat.newP4(P4.new4(v[1], v[2], v[3], v[0])); + retStddev[1] = getRmsd(centerAndPoints, q); + return q; + } + + /** + * Fills a 4x4 matrix with rotation-translation of mapped points A to B. + * If centerA is null, this is a standard 4x4 rotation-translation matrix; + * otherwise, this 4x4 matrix is a rotation around a vector through the center of ptsA, + * and centerA is filled with that center; + * Prior to Jmol 14.3.12_2014.02.14, when used from the JmolScript compare() function, + * this method returned the second of these options instead of the first. + * + * @param ptsA + * @param ptsB + * @param m 4x4 matrix to be returned + * @param centerA return center of rotation; if null, then standard 4x4 matrix is returned + * @return stdDev + */ + public static float getTransformMatrix4(Lst ptsA, Lst ptsB, M4 m, + P3 centerA) { + P3[] cptsA = getCenterAndPoints(ptsA); + P3[] cptsB = getCenterAndPoints(ptsB); + float[] retStddev = new float[2]; + Quat q = calculateQuaternionRotation(new P3[][] { cptsA, cptsB }, + retStddev); + M3 r = q.getMatrix(); + if (centerA == null) + r.rotate(cptsA[0]); + else + centerA.setT(cptsA[0]); + V3 t = V3.newVsub(cptsB[0], cptsA[0]); + m.setMV(r, t); + return retStddev[1]; + } + + /** + * from a list of points, create an array that includes the center + * point as the first point. This array is used as a starting point for + * a quaternion analysis of superposition. + * + * @param vPts + * @return array of points with first point center + */ + public static P3[] getCenterAndPoints(Lst vPts) { + int n = vPts.size(); + P3[] pts = new P3[n + 1]; + pts[0] = new P3(); + if (n > 0) { + for (int i = 0; i < n; i++) { + pts[0].add(pts[i + 1] = vPts.get(i)); + } + pts[0].scale(1f / n); + } + return pts; + } + + public static float getRmsd(P3[][] centerAndPoints, Quat q) { + double sum2 = 0; + P3[] ptsA = centerAndPoints[0]; + P3[] ptsB = centerAndPoints[1]; + P3 cA = ptsA[0]; + P3 cB = ptsB[0]; + int n = ptsA.length - 1; + P3 ptAnew = new P3(); + + for (int i = n + 1; --i >= 1;) { + ptAnew.sub2(ptsA[i], cA); + q.transform2(ptAnew, ptAnew).add(cB); + sum2 += ptAnew.distanceSquared(ptsB[i]); + } + return (float) Math.sqrt(sum2 / n); + } + +} diff --git a/src/javajs/util/MessagePackReader.java b/src/javajs/util/MessagePackReader.java new file mode 100644 index 0000000..e9cdb7b --- /dev/null +++ b/src/javajs/util/MessagePackReader.java @@ -0,0 +1,703 @@ +package javajs.util; + +import java.util.Hashtable; +import java.util.Map; + +import javajs.api.GenericBinaryDocumentReader; + +/** + * A simple MessagePack reader. See https://github.com/msgpack/msgpack/blob/master/spec.md + * with very few dependencies. + * + * Nuances: + * + * Does not implement unsigned int32 or int64 (delivers simple integers in all cases). + * Does not use doubles; just floats + * + * Note: + * + * homogeneousArrays == true will deliver null for empty array. + * + * + * Use in MMTF: + * + * + * BufferedInputStream bs = [whatever] + * + * GenericBinaryDocument binaryDoc = new javajs.util.BinaryDocument(); + * + * binaryDoc.setStream(bs, true); + * + * + * map = (new MessagePackReader(binaryDoc, true)).readMap(); + * + * entities = (Object[]) map.get("entityList"); + * + * float[] x = (float[]) decode((byte[]) map.get("xCoordList")) + * + * + * @author Bob Hanson hansonr@stolaf.edu + */ + +public class MessagePackReader { + + private GenericBinaryDocumentReader doc; + + private boolean isHomo;// homogeneous arrays -- use int[] not Integer + + // these maps must be checked for the specific number of bits, in the following order: + private final static int POSITIVEFIXINT_x80 = 0x80; //0xxxxxxx + private final static int FIXMAP_xF0 = 0x80; //1000xxxx +// private final static int FIXARRAY_xF0 = 0x90; //1001xxxx + private final static int FIXSTR_xE0 = 0xa0; //101xxxxx + private final static int NEGATIVEFIXINT_xE0 = 0xe0; //111xxxxx + private final static int DEFINITE_xE0 = 0xc0; //110xxxxx + + private final static int NIL = 0xc0; +// private final static int (NEVERUSED) = 0xc1; + private final static int FALSE = 0xc2; + private final static int TRUE = 0xc3; + private final static int BIN8 = 0xc4; + private final static int BIN16 = 0xc5; + private final static int BIN32 = 0xc6; + private final static int EXT8 = 0xc7; + private final static int EXT16 = 0xc8; + private final static int EXT32 = 0xc9; + private final static int FLOAT32 = 0xca; + private final static int FLOAT64 = 0xcb; + private final static int UINT8 = 0xcc; + private final static int UINT16 = 0xcd; + private final static int UINT32 = 0xce; + private final static int UINT64 = 0xcf; + private final static int INT8 = 0xd0; + private final static int INT16 = 0xd1; + private final static int INT32 = 0xd2; + private final static int INT64 = 0xd3; + private final static int FIXEXT1 = 0xd4; + private final static int FIXEXT2 = 0xd5; + private final static int FIXEXT4 = 0xd6; + private final static int FIXEXT8 = 0xd7; + private final static int FIXEXT16 = 0xd8; + private final static int STR8 = 0xd9; + private final static int STR16 = 0xda; + private final static int STR32 = 0xdb; + private final static int ARRAY16 = 0xdc; + private final static int ARRAY32 = 0xdd; + private final static int MAP16 = 0xde; + private final static int MAP32 = 0xdf; + + public MessagePackReader(GenericBinaryDocumentReader binaryDoc, boolean isHomogeneousArrays) { + isHomo = isHomogeneousArrays; + doc = binaryDoc; + } + + @SuppressWarnings("unchecked") + public Map readMap() throws Exception { + return (Map) getNext(null, 0); + } + + public Object getNext(Object array, int pt) throws Exception { + int b = doc.readByte() & 0xFF; + int be0 = b & 0xE0; + if ((b & POSITIVEFIXINT_x80) == 0) { + if (array != null) { + ((int[]) array)[pt] = b; + return null; + } + return Integer.valueOf(b); + } + switch (be0) { + case NEGATIVEFIXINT_xE0: + b = BC.intToSignedInt(b | 0xFFFFFF00); + if (array != null) { + ((int[]) array)[pt] = b; + return null; + } + return Integer.valueOf(b); + case FIXSTR_xE0: { + String s = doc.readString(b & 0x1F); + if (array != null) { + ((String[]) array)[pt] = s; + return null; + } + return s; + } + case FIXMAP_xF0: + return ((b & 0xF0) == FIXMAP_xF0 ? getMap(b & 0x0F) : getArray(b & 0x0F)); + case DEFINITE_xE0: + switch (b) { + case NIL: + return null; + case FALSE: + return Boolean.FALSE; + case TRUE: + return Boolean.TRUE; + case EXT8: + return getObject(doc.readUInt8()); + case EXT16: + return getObject(doc.readUnsignedShort()); + case EXT32: + return getObject(doc.readInt()); // should be unsigned int + case FIXEXT1: + return getObject(1); + case FIXEXT2: + return getObject(2); + case FIXEXT4: + return getObject(4); + case FIXEXT8: + return getObject(8); + case FIXEXT16: + return getObject(16); + case ARRAY16: + return getArray(doc.readUnsignedShort()); + case ARRAY32: + return getArray(doc.readInt()); + case MAP16: + return getMap(doc.readUnsignedShort()); + case MAP32: + return getMap(doc.readInt()); + + // binary arrays: + + case BIN8: + return doc.readBytes(doc.readUInt8()); + case BIN16: + return doc.readBytes(doc.readUnsignedShort()); + case BIN32: + return doc.readBytes(doc.readInt()); + } + if (array == null) { + switch (b) { + case FLOAT32: + return Float.valueOf(doc.readFloat()); + case FLOAT64: + return Float.valueOf((float) doc.readDouble()); + case UINT8: + return Integer.valueOf(doc.readUInt8()); + case UINT16: + return Integer.valueOf(doc.readUnsignedShort()); + case UINT32: + return Integer.valueOf(doc.readInt()); // technically should be UInt32 + case UINT64: + return Long.valueOf(doc.readLong()); // should be unsigned long; incompatible with JavaScript! + case INT8: + return Integer.valueOf(doc.readByte()); + case INT16: + return Integer.valueOf(doc.readShort()); + case INT32: + return Integer.valueOf(doc.readInt()); // should be Unsigned Int here + case INT64: + return Long.valueOf(doc.readLong()); + case STR8: + return doc.readString(doc.readUInt8()); + case STR16: + return doc.readString(doc.readShort()); + case STR32: + return doc.readString(doc.readInt()); + } + } else { + switch (b) { + case FLOAT32: + ((float[]) array)[pt] = doc.readFloat(); + break; + case FLOAT64: + ((float[]) array)[pt] = (float) doc.readDouble(); + break; + case UINT8: + ((int[]) array)[pt] = doc.readUInt8(); + break; + case UINT16: + ((int[]) array)[pt] = doc.readUnsignedShort(); + break; + case UINT32: + ((int[]) array)[pt] = doc.readInt(); // should be unsigned int + break; + case UINT64: + ((int[]) array)[pt] = (int) doc.readLong(); // should be unsigned long; incompatible with JavaScript! + break; + case INT8: + ((int[]) array)[pt] = doc.readByte(); + break; + case INT16: + ((int[]) array)[pt] = doc.readShort(); + break; + case INT32: + ((int[]) array)[pt] = doc.readInt(); // should be Unsigned Int here + break; + case INT64: + ((int[]) array)[pt] = (int) doc.readLong(); + break; + case STR8: + ((String[]) array)[pt] = doc.readString(doc.readUInt8()); + break; + case STR16: + ((String[]) array)[pt] = doc.readString(doc.readShort()); + break; + case STR32: + ((String[]) array)[pt] = doc.readString(doc.readInt()); + break; + } + } + } + return null; + } + + private Object getObject(int n) throws Exception { + return new Object[] { Integer.valueOf(doc.readUInt8()), doc.readBytes(n) }; + } + + private Object getArray(int n) throws Exception { + if (isHomo) { + if (n == 0) + return null; + Object v = getNext(null, 0); + if (v instanceof Integer) { + int[] a = new int[n]; + a[0] = ((Integer) v).intValue(); + v = a; + } else if (v instanceof Float) { + float[] a = new float[n]; + a[0] = ((Float) v).floatValue(); + v = a; + } else if (v instanceof String) { + String[] a = new String[n]; + a[0] = (String) v; + v = a; + } else { + Object[] o = new Object[n]; + o[0] = v; + for (int i = 1; i < n; i++) + o[i] = getNext(null, 0); + return o; + } + for (int i = 1; i < n; i++) + getNext(v, i); + return v; + } + Object[] o = new Object[n]; + for (int i = 0; i < n; i++) + o[i] = getNext(null, 0); + return o; + } + + private Object getMap(int n) throws Exception { + Map map = new Hashtable(); + for (int i = 0; i < n; i++) { + String key = getNext(null, 0).toString(); + //Logger.info(key); + + Object value = getNext(null, 0); + if (value == null) { + //Logger.info("null value for " + key); + } else { + map.put(key, value); + } + } + return map; + } + + /////////////// MMTF MessagePack decoding /////////////// + + /** + * This single method takes care of all MMTF needs. + * + * See https://github.com/rcsb/mmtf/blob/master/spec.md + * + * @param b + * + * @return array of int, char, or float, depending upon the type + */ + public static Object decode(byte[] b) { + int type = BC.bytesToInt(b, 0, true); + int n = BC.bytesToInt(b, 4, true); + int param = BC.bytesToInt(b, 8, true); + switch (type) { + case 1: + return getFloats(b, n, 1); + case 2: // 1-byte + case 3: // 2-byte + case 4: // 4-byte + return getInts(b, n); + case 5: + return rldecode32ToStr(b); + case 6: + return rldecode32ToChar(b, n); + case 7: + return rldecode32(b, n); + case 8: + return rldecode32Delta(b, n); + case 9: + return rldecodef(b, n, param); + case 10: + return unpack16Deltaf(b, n, param); + case 11: + return getFloats(b, n, param); + case 12: // two-byte + case 13: // one-byte + return unpackf(b, 14 - type, n, param); + case 14: // two-byte + case 15: // one-byte + return unpack(b, 16 - type, n); + default: + System.out.println("MMTF type " + type + " not found!"); + return null; + } + } + + /** + * mmtf type 1 and 11 + * + * byte[4] to float32 + * + * @param b + * @param n + * @param divisor + * @return array of floats + */ + public static float[] getFloats(byte[] b, int n, float divisor) { + if (b == null) + return null; + float[] a = new float[n]; + try { + switch ((b.length - 12) / n) { + case 2: + for (int i = 0, j = 12; i < n; i++, j += 2) + a[i] = BC.bytesToShort(b, j, false) / divisor; + break; + case 4: + for (int i = 0, j = 12; i < n; i++, j += 4) + a[i] = BC.bytesToFloat(b, j, false); + break; + } + } catch (Exception e) { + } + return a; + } + + /** + * mmtf types 2-4 + * + * Decode a byte array into a byte, short, or int array. + * + * @param b + * @param n + * + * @return array of integers + */ + public static int[] getInts(byte[] b, int n) { + if (b == null) + return null; + int[] a = new int[n]; + switch ((b.length - 12) / n) { + case 1: + for (int i = 0, j = 12; i < n; i++, j++) + a[i] = b[j]; + break; + case 2: + for (int i = 0, j = 12; i < n; i++, j += 2) + a[i] = BC.bytesToShort(b, j, true); + break; + case 4: + for (int i = 0, j = 12; i < n; i++, j += 4) + a[i] = BC.bytesToInt(b, j, true); + break; + } + return a; + } + + /** + * mmtf type 5 + * + * Decode each four bytes as a 1- to 4-character string label where a 0 byte + * indicates end-of-string. + * + * @param b + * a byte array + * @return String[] + */ + public static String[] rldecode32ToStr(byte[] b) { + String[] id = new String[(b.length - 12) / 4]; + out: for (int i = 0, len = id.length, pt = 12; i < len; i++) { + SB sb = new SB(); + for (int j = 0; j < 4; j++) { + switch (b[pt]) { + case 0: + id[i] = sb.toString(); + pt += 4 - j; + continue out; + default: + sb.appendC((char) b[pt++]); + if (j == 3) + id[i] = sb.toString(); + continue; + } + } + } + return id; + } + + /** + * mmtf type 6 + * + * Decode an array of int32 using run-length decoding to one char per int. + * + * @param b + * @param n + * @return array of characters + */ + public static char[] rldecode32ToChar(byte[] b, int n) { + if (b == null) + return null; + char[] ret = new char[n]; + for (int i = 0, pt = 3; i < n;) { + char val = (char) b[((pt++) << 2) + 3]; + for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) + ret[i++] = val; + } + return ret; + } + + /** + * mmtf type 7 + * + * Decode an array of int32 using run-length decoding. + * + * @param b + * @param n + * @return array of integers + */ + public static int[] rldecode32(byte[] b, int n) { + if (b == null) + return null; + int[] ret = new int[n]; + for (int i = 0, pt = 3; i < n;) { + int val = BC.bytesToInt(b, (pt++) << 2, true); + for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) + ret[i++] = val; + } + return ret; + } + + /** + * mmtf type 8 + * + * Decode an array of int32 using run-length decoding of a difference array. + * + * @param b + * @param n + * @return array of integers + */ + public static int[] rldecode32Delta(byte[] b, int n) { + if (b == null) + return null; + int[] ret = new int[n]; + for (int i = 0, pt = 3, val = 0; i < n;) { + int diff = BC.bytesToInt(b, (pt++) << 2, true); + for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) + ret[i++] = (val = val + diff); + } + return ret; + } + + /** + * mmtf type 9 + * + * Decode an array of int32 using run-length decoding and divide by a divisor + * to give a float32. + * + * @param b + * @param n + * @param divisor + * @return array of floats + */ + public static float[] rldecodef(byte[] b, int n, float divisor) { + if (b == null) + return null; + float[] ret = new float[n]; + for (int i = 0, pt = 3; i < n;) { + int val = BC.bytesToInt(b, (pt++) << 2, true); + for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) + ret[i++] = val / divisor; + } + return ret; + } + + /** + * + * mmtf type 10 + * + * Decode an array of int16 using run-length decoding of a difference array. + * + * @param b + * @param n + * @param divisor + * @return array of floats + */ + public static float[] unpack16Deltaf(byte[] b, int n, float divisor) { + if (b == null) + return null; + float[] ret = new float[n]; + for (int i = 0, pt = 6, val = 0, buf = 0; i < n;) { + int diff = BC.bytesToShort(b, (pt++) << 1, true); + if (diff == Short.MAX_VALUE || diff == Short.MIN_VALUE) { + buf += diff; + } else { + ret[i++] = (val = val + diff + buf) / divisor; + buf = 0; + } + } + return ret; + } + + /** + * + * mmtf type 12 and 13 + * + * Unpack an array of int8 or int16 to int32 and divide to give a float32. + * + * untested + * + * @param b + * @param nBytes + * @param n + * @param divisor + * @return array of floats + */ + public static float[] unpackf(byte[] b, int nBytes, int n, float divisor) { + if (b == null) + return null; + float[] ret = new float[n]; + switch (nBytes) { + case 1: + for (int i = 0, pt = 12, offset = 0; i < n;) { + int val = b[pt++]; + if (val == Byte.MAX_VALUE || val == Byte.MIN_VALUE) { + offset += val; + } else { + ret[i++] = (val + offset) / divisor; + offset = 0; + } + } + break; + case 2: + for (int i = 0, pt = 6, offset = 0; i < n;) { + int val = BC.bytesToShort(b, (pt++) << 1, true); + if (val == Short.MAX_VALUE || val == Short.MIN_VALUE) { + offset += val; + } else { + ret[i++] = (val + offset) / divisor; + offset = 0; + } + } + break; + } + return ret; + } + + /** + * + * mmtf type 14 and 15 + * + * Unpack an array of int8 or int16 to int32. + * + * untested + * + * @param b + * @param nBytes + * @param n + * @return array of integers + */ + public static int[] unpack(byte[] b, int nBytes, int n) { + if (b == null) + return null; + int[] ret = new int[n]; + switch (nBytes) { + case 1: + for (int i = 0, pt = 12, offset = 0; i < n;) { + int val = b[pt++]; + if (val == Byte.MAX_VALUE || val == Byte.MIN_VALUE) { + offset += val; + } else { + ret[i++] = val + offset; + offset = 0; + } + } + break; + case 2: + for (int i = 0, pt = 6, offset = 0; i < n;) { + int val = BC.bytesToShort(b, (pt++) << 1, true); + if (val == Short.MAX_VALUE || val == Short.MIN_VALUE) { + offset += val; + } else { + ret[i++] = val + offset; + offset = 0; + } + } + break; + } + return ret; + } + + ///** + //* Decode an array of int16 using run-length decoding + //* of a difference array. + //* + //* @param b + //* @param n + //* @param i0 + //* @return array of integers + //*/ + //public static int[] rldecode16Delta(byte[] b, int n, int i0) { + //if (b == null) + //return null; + //int[] ret = new int[n]; + //for (int i = 0, pt = i0 / 2, val = 0; i < n;) { + //int diff = BC.bytesToShort(b, (pt++) << 1, true); + //for (int j = BC.bytesToShort(b, (pt++) << 1, true); --j >= 0;) + //ret[i++] = (val = val + diff); + //} + //return ret; + //} + + ///** + //* Do a split delta to a float[] array + //* @param xyz label "x", "y", "z", or "bFactor" + //* @param factor for dividing in the end -- 1000f or 100f + //* @return float[] + //* + //*/ + //public static float[] getFloatsSplit(String xyz, float factor) { + //byte[] big = (byte[]) map.get(xyz + "Big"); + //return (big == null ? null : splitDelta(big, + //(byte[]) map.get(xyz + "Small"), fileAtomCount, factor)); + //} + + ///** + //* Do a split delta to a float[] array + //* + //* @param big + //* [n m n m n m...] where n is a "big delta" and m is a number of + //* "small deltas + //* @param small + //* array containing the small deltas + //* @param n + //* the size of the final array + //* @param factor + //* to divide the final result by -- 1000f or 100f here + //* @return float[] + //*/ + //public static float[] splitDelta(byte[] big, byte[] small, int n, float factor) { + //float[] ret = new float[n]; + //for (int i = 0, smallpt = 0, val = 0, datapt = 0, len = big.length >> 2; i < len; i++) { + //ret[datapt++] = (val = val + BC.bytesToInt(big, i << 2, true)) / factor; + //if (++i < len) + //for (int j = BC.bytesToInt(big, i << 2, true); --j >= 0; smallpt++) + //ret[datapt++] = (val = val + BC.bytesToShort(small, smallpt << 1, true)) + // / factor; + //} + //return ret; + //} + // + + +} diff --git a/src/javajs/util/OC.java b/src/javajs/util/OC.java new file mode 100644 index 0000000..81cda57 --- /dev/null +++ b/src/javajs/util/OC.java @@ -0,0 +1,418 @@ +package javajs.util; + +import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import javajs.J2SIgnoreImport; +import javajs.api.BytePoster; +import javajs.api.GenericOutputChannel; +import javajs.api.js.J2SObjectInterface; + +/** + * + * A generic output method. JmolOutputChannel can be used to: + * + * add characters to a StringBuffer + * using fileName==null, append() and toString() + * + * add bytes utilizing ByteArrayOutputStream + * using writeBytes(), writeByteAsInt(), append()*, and bytesAsArray() + * *append() can be used as long as os==ByteArrayOutputStream + * or it is not used before one of the writeByte methods. + * + * output characters to a FileOutputStream + * using os==FileOutputStream, asWriter==true, append(), and closeChannel() + * + * output bytes to a FileOutputStream + * using os==FileOutputStream, writeBytes(), writeByteAsInt(), append(), and closeChannel() + * + * post characters or bytes to a remote server + * using fileName=="http://..." or "https://...", + * writeBytes(), writeByteAsInt(), append(), and closeChannel() + * + * send characters or bytes to a JavaScript function + * when JavaScript and (typeof fileName == "function") + * + * if fileName equals ";base64,", then the data are base64-encoded + * prior to writing, and closeChannel() returns the data. + * + * @author hansonr Bob Hanson hansonr@stolaf.edu 9/2013 + * + * + */ + +@J2SIgnoreImport({ java.io.FileOutputStream.class }) +public class OC extends OutputStream implements GenericOutputChannel { + + private BytePoster bytePoster; // only necessary for writing to http:// or https:// + private String fileName; + private BufferedWriter bw; + private boolean isLocalFile; + private int byteCount; + private boolean isCanceled; + private boolean closed; + private OutputStream os; + private SB sb; + private String type; + private boolean isBase64; + private OutputStream os0; + private byte[] bytes; // preset bytes; output only + + public boolean bigEndian = true; + + @Override + public boolean isBigEndian() { + return bigEndian; + } + + public void setBigEndian(boolean TF) { + bigEndian = TF; + } + + public OC setParams(BytePoster bytePoster, String fileName, + boolean asWriter, OutputStream os) { + this.bytePoster = bytePoster; + isBase64 = ";base64,".equals(fileName); + if (isBase64) { + fileName = null; + os0 = os; + os = null; + } + this.fileName = fileName; + this.os = os; + isLocalFile = (fileName != null && !isRemote(fileName)); + if (asWriter && !isBase64 && os != null) + bw = Rdr.getBufferedWriter(os, null); + return this; + } + + public OC setBytes(byte[] b) { + bytes = b; + return this; + } + + public String getFileName() { + return fileName; + } + + public String getName() { + return (fileName == null ? null : fileName.substring(fileName.lastIndexOf("/") + 1)); + } + + public int getByteCount() { + return byteCount; + } + + /** + * + * @param type user-identified type (PNG, JPG, etc) + */ + public void setType(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + /** + * will go to string buffer if bw == null and os == null + * + * @param s + * @return this, for chaining like a standard StringBuffer + * + */ + public OC append(String s) { + try { + if (bw != null) { + bw.write(s); + } else if (os == null) { + if (sb == null) + sb = new SB(); + sb.append(s); + } else { + byte[] b = s.getBytes(); + os.write(b, 0, b.length); + byteCount += b.length; + return this; + } + } catch (IOException e) { + // ignore + } + byteCount += s.length(); // not necessarily exactly correct if unicode + return this; + } + + @Override + public void reset() { + sb = null; + initOS(); + } + + + private void initOS() { + if (sb != null) { + String s = sb.toString(); + reset(); + append(s); + return; + } + try { + /** + * @j2sNative + * + * this.os = null; + */ + { + if (os instanceof FileOutputStream) { + os.close(); + os = new FileOutputStream(fileName); + } else { + os = null; + } + } + if (os == null) + os = new ByteArrayOutputStream(); + if (bw != null) { + bw.close(); + bw = Rdr.getBufferedWriter(os, null); + } + } catch (Exception e) { + // not perfect here. + System.out.println(e.toString()); + } + byteCount = 0; + } + + /** + * @param b + */ + @Override + public void writeByteAsInt(int b) { + if (os == null) + initOS(); + { + try { + os.write(b); + } catch (IOException e) { + } + } + byteCount++; + } + + @Override + public void write(byte[] buf, int i, int len) { + if (os == null) + initOS(); + try { + os.write(buf, i, len); + } catch (IOException e) { + } + byteCount += len; + } + + @Override + public void writeShort(short i) { + if (isBigEndian()) { + writeByteAsInt(i >> 8); + writeByteAsInt(i); + } else { + writeByteAsInt(i); + writeByteAsInt(i >> 8); + } + } + + @Override + public void writeLong(long b) { + if (isBigEndian()) { + writeInt((int) ((b >> 32) & 0xFFFFFFFFl)); + writeInt((int) (b & 0xFFFFFFFFl)); + } else { + writeByteAsInt((int) (b >> 56)); + writeByteAsInt((int) (b >> 48)); + writeByteAsInt((int) (b >> 40)); + writeByteAsInt((int) (b >> 32)); + writeByteAsInt((int) (b >> 24)); + writeByteAsInt((int) (b >> 16)); + writeByteAsInt((int) (b >> 8)); + writeByteAsInt((int) b); + } + } + + public void write(int b) { + // required by standard ZipOutputStream -- do not use, as it will break JavaScript methods + if (os == null) + initOS(); + try { + os.write(b); + } catch (IOException e) { + } + byteCount++; + } + + public void write(byte[] b) { + // not used in JavaScript due to overloading problem there + write(b, 0, b.length); + } + + public void cancel() { + isCanceled = true; + closeChannel(); + } + + @Override + @SuppressWarnings({ "unused" }) + public String closeChannel() { + if (closed) + return null; + // can't cancel file writers + try { + if (bw != null) { + bw.flush(); + bw.close(); + } else if (os != null) { + os.flush(); + os.close(); + } + if (os0 != null && isCanceled) { + os0.flush(); + os0.close(); + } + } catch (Exception e) { + // ignore closing issues + } + if (isCanceled) { + closed = true; + return null; + } + if (fileName == null) { + if (isBase64) { + String s = getBase64(); + if (os0 != null) { + os = os0; + append(s); + } + sb = new SB(); + sb.append(s); + isBase64 = false; + return closeChannel(); + } + return (sb == null ? null : sb.toString()); + } + closed = true; + J2SObjectInterface jmol = null; + Object _function = null; + /** + * @j2sNative + * + * jmol = self.J2S || Jmol; _function = (typeof this.fileName == "function" ? + * this.fileName : null); + * + */ + { + if (!isLocalFile) { + String ret = postByteArray(); // unsigned applet could do this + if (ret.startsWith("java.net")) + byteCount = -1; + return ret; + } + } + if (jmol != null) { + Object data = (sb == null ? toByteArray() : sb.toString()); + if (_function == null) + jmol._doAjax(fileName, null, data, sb == null); + else + jmol._apply(_function, data); + } + return null; + } + + public boolean isBase64() { + return isBase64; + } + + public String getBase64() { + return Base64.getBase64(toByteArray()).toString(); + } + + public byte[] toByteArray() { + return (bytes != null ? bytes : os instanceof ByteArrayOutputStream ? ((ByteArrayOutputStream)os).toByteArray() : null); + } + + @Override + @Deprecated + public void close() { + closeChannel(); + } + + @Override + public String toString() { + if (bw != null) + try { + bw.flush(); + } catch (IOException e) { + // TODO + } + if (sb != null) + return closeChannel(); + return byteCount + " bytes"; + } + + private String postByteArray() { + byte[] bytes = (sb == null ? toByteArray() : sb.toString().getBytes()); + return bytePoster.postByteArray(fileName, bytes); + } + + public final static String[] urlPrefixes = { "http:", "https:", "sftp:", "ftp:", + "file:" }; + // note that SFTP is not supported + public final static int URL_LOCAL = 4; + + public static boolean isRemote(String fileName) { + if (fileName == null) + return false; + int itype = urlTypeIndex(fileName); + return (itype >= 0 && itype != URL_LOCAL); + } + + public static boolean isLocal(String fileName) { + if (fileName == null) + return false; + int itype = urlTypeIndex(fileName); + return (itype < 0 || itype == URL_LOCAL); + } + + public static int urlTypeIndex(String name) { + if (name == null) + return -2; // local unsigned applet + for (int i = 0; i < urlPrefixes.length; ++i) { + if (name.startsWith(urlPrefixes[i])) { + return i; + } + } + return -1; + } + + @Override + public void writeInt(int i) { + if (bigEndian) { + writeByteAsInt(i >> 24); + writeByteAsInt(i >> 16); + writeByteAsInt(i >> 8); + writeByteAsInt(i); + } else { + writeByteAsInt(i); + writeByteAsInt(i >> 8); + writeByteAsInt(i >> 16); + writeByteAsInt(i >> 24); + } + } + + public void writeFloat(float x) { + writeInt(x == 0 ? 0 : Float.floatToIntBits(x)); + } + +} diff --git a/src/javajs/util/P3.java b/src/javajs/util/P3.java new file mode 100644 index 0000000..edfebfe --- /dev/null +++ b/src/javajs/util/P3.java @@ -0,0 +1,59 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + + + +/** + * A 3 element point that is represented by single precision floating point + * x,y,z coordinates. + * + * @version specification 1.1, implementation $Revision: 1.10 $, $Date: + * 2006/09/08 20:20:20 $ + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 + * for unique constructor and method names + * for the optimization of compiled JavaScript using Java2Script. + * + * + */ +public class P3 extends T3 { + + public static P3 newP(T3 t) { + P3 p = new P3(); + p.x = t.x; + p.y = t.y; + p.z = t.z; + return p; + } + + private static P3 unlikely; + + public static P3 getUnlikely() { + return (unlikely == null ? unlikely = new3((float) Math.PI, (float) Math.E, (float) (Math.PI * Math.E)) : unlikely); + } + + public static P3 new3(float x, float y, float z) { + P3 p = new P3(); + p.x = x; + p.y = y; + p.z = z; + return p; + } + +} diff --git a/src/javajs/util/P3i.java b/src/javajs/util/P3i.java new file mode 100644 index 0000000..381aede --- /dev/null +++ b/src/javajs/util/P3i.java @@ -0,0 +1,44 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + + + +/** + * A 3 element point that is represented by signed integer x,y,z coordinates. + * + * @since Java 3D 1.2 + * @version specification 1.2, implementation $Revision: 1.9 $, $Date: + * 2006/07/28 17:01:33 $ + * @author Kenji hiranabe + * + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 for unique + * constructor and method names for the optimization of compiled + * JavaScript using Java2Script. + * + */ +public class P3i extends T3i { + + public static P3i new3(int x, int y, int z) { + P3i pt = new P3i(); + pt.x = x; + pt.y = y; + pt.z = z; + return pt; + } +} diff --git a/src/javajs/util/P4.java b/src/javajs/util/P4.java new file mode 100644 index 0000000..2145696 --- /dev/null +++ b/src/javajs/util/P4.java @@ -0,0 +1,63 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + + + +/** + * A 4 element point that is represented by single precision floating point + * x,y,z,w coordinates. + * + * @version specification 1.1, implementation $Revision: 1.9 $, $Date: + * 2006/07/28 17:01:32 $ + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 + * for unique constructor and method names + * for the optimization of compiled JavaScript using Java2Script. + * + */ +public class P4 extends T4 { + + public static P4 new4(float x, float y, float z, float w) { + P4 pt = new P4(); + pt.set4(x, y, z, w); + return pt; + } + + public static P4 newPt(P4 value) { + P4 pt = new P4(); + pt.set4(value.x, value.y, value.z, value.w); + return pt; + } + + /** + * Returns the distance between this point and point p1. + * + * @param p1 + * the other point + * @return the distance between these two points + */ + public final float distance4(P4 p1) { + double dx = x - p1.x; + double dy = y - p1.y; + double dz = z - p1.z; + double dw = w - p1.w; + return (float) Math.sqrt(dx * dx + dy * dy + dz * dz + dw * dw); + } + +} diff --git a/src/javajs/util/PT.java b/src/javajs/util/PT.java new file mode 100644 index 0000000..c544967 --- /dev/null +++ b/src/javajs/util/PT.java @@ -0,0 +1,1543 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2007-04-26 16:57:51 -0500 (Thu, 26 Apr 2007) $ + * $Revision: 7502 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package javajs.util; + +import java.lang.reflect.Array; +import java.util.Map; +import java.util.Map.Entry; + +import javajs.J2SIgnoreImport; +import javajs.api.JSONEncodable; + +/** + * a combination of Parsing and Text-related utility classes + * + * @author hansonr + * + */ + +@J2SIgnoreImport(value = { java.lang.reflect.Array.class }) +public class PT { + + public static int parseInt(String str) { + return parseIntNext(str, new int[] {0}); + } + + public static int parseIntNext(String str, int[] next) { + int cch = str.length(); + if (next[0] < 0 || next[0] >= cch) + return Integer.MIN_VALUE; + return parseIntChecked(str, cch, next); + } + + public static int parseIntChecked(String str, int ichMax, int[] next) { + boolean digitSeen = false; + int value = 0; + int ich = next[0]; + if (ich < 0) + return Integer.MIN_VALUE; + int ch; + while (ich < ichMax && isWhiteSpace(str, ich)) + ++ich; + boolean negative = false; + if (ich < ichMax && str.charAt(ich) == 45) { //"-" + negative = true; + ++ich; + } + while (ich < ichMax && (ch = str.charAt(ich)) >= 48 && ch <= 57) { + value = value * 10 + (ch - 48); + digitSeen = true; + ++ich; + } + if (!digitSeen)// || !checkTrailingText(str, ich, ichMax)) + value = Integer.MIN_VALUE; + else if (negative) + value = -value; + next[0] = ich; + return value; + } + + public static boolean isWhiteSpace(String str, int ich) { + char ch; + return (ich >= 0 && ((ch = str.charAt(ich)) == ' ' || ch == '\t' || ch == '\n')); + } + + /** + * A float parser that is 30% faster than Float.parseFloat(x) and also accepts + * x.yD+-n + * + * @param str + * @param ichMax + * @param next + * pointer; incremented + * @param isStrict + * @return value or Float.NaN + */ + public static float parseFloatChecked(String str, int ichMax, int[] next, + boolean isStrict) { + boolean digitSeen = false; + int ich = next[0]; + if (isStrict && str.indexOf('\n') != str.lastIndexOf('\n')) + return Float.NaN; + while (ich < ichMax && isWhiteSpace(str, ich)) + ++ich; + boolean negative = false; + if (ich < ichMax && str.charAt(ich) == '-') { + ++ich; + negative = true; + } + // looks crazy, but if we don't do this, Google Closure Compiler will + // write code that Safari will misinterpret in a VERY nasty way -- + // getting totally confused as to long integers and double values + + // This is Safari figuring out the values of the numbers on the line (x, y, then z): + + // ATOM 1241 CD1 LEU A 64 -2.206 36.532 31.576 1.00 60.60 C + // e=1408749273 + // -e =-1408749273 + // ATOM 1241 CD1 LEU A 64 -2.206 36.532 31.576 1.00 60.60 C + // e=-1821066134 + // e=36.532 + // ATOM 1241 CD1 LEU A 64 -2.206 36.532 31.576 1.00 60.60 C + // e=-1133871366 + // e=31.576 + // + // "e" values are just before and after the "value = -value" statement. + + int ch = 0; + float ival = 0f; + float ival2 = 0f; + while (ich < ichMax && (ch = str.charAt(ich)) >= 48 && ch <= 57) { + ival = (ival * 10f) + (ch - 48)*1f; + ++ich; + digitSeen = true; + } + boolean isDecimal = false; + int iscale = 0; + int nzero = (ival == 0 ? -1 : 0); + if (ch == '.') { + isDecimal = true; + while (++ich < ichMax && (ch = str.charAt(ich)) >= 48 && ch <= 57) { + digitSeen = true; + if (nzero < 0) { + if (ch == 48) { + nzero--; + continue; + } + nzero = -nzero; + } + if (iscale < decimalScale.length) { + ival2 = (ival2 * 10f) + (ch - 48)*1f; + iscale++; + } + } + } + float value; + + // Safari breaks here intermittently converting integers to floats + + if (!digitSeen) { + value = Float.NaN; + } else if (ival2 > 0) { + value = ival2 * decimalScale[iscale - 1]; + if (nzero > 1) { + if (nzero - 2 < decimalScale.length) { + value *= decimalScale[nzero - 2]; + } else { + value *= Math.pow(10, 1 - nzero); + } + } else { + value += ival; + } + } else { + value = ival; + } + boolean isExponent = false; + if (ich < ichMax && (ch == 69 || ch == 101 || ch == 68)) { // E e D + isExponent = true; + if (++ich >= ichMax) + return Float.NaN; + ch = str.charAt(ich); + if ((ch == '+') && (++ich >= ichMax)) + return Float.NaN; + next[0] = ich; + int exponent = parseIntChecked(str, ichMax, next); + if (exponent == Integer.MIN_VALUE) + return Float.NaN; + if (exponent > 0 && exponent <= tensScale.length) + value *= tensScale[exponent - 1]; + else if (exponent < 0 && -exponent <= decimalScale.length) + value *= decimalScale[-exponent - 1]; + else if (exponent != 0) + value *= Math.pow(10, exponent); + } else { + next[0] = ich; // the exponent code finds its own ichNextParse + } + // believe it or not, Safari reports the long-equivalent of the + // float value here, then later the float value, after no operation! + if (negative) + value = -value; + if (value == Float.POSITIVE_INFINITY) + value = Float.MAX_VALUE; + return (!isStrict || (!isExponent || isDecimal) + && checkTrailingText(str, next[0], ichMax) ? value : Float.NaN); + } + + public final static float[] tensScale = { 10f, 100f, 1000f, 10000f, 100000f, 1000000f }; + public final static float[] decimalScale = { + 0.1f, + 0.01f, + 0.001f, + 0.0001f, + 0.00001f, + 0.000001f, + 0.0000001f, + 0.00000001f, + 0.000000001f + }; + public static boolean checkTrailingText(String str, int ich, int ichMax) { + //number must be pure -- no additional characters other than white space or ; + char ch; + while (ich < ichMax && (isWhitespace(ch = str.charAt(ich)) || ch == ';')) + ++ich; + return (ich == ichMax); + } + + public static float[] parseFloatArray(String str) { + return parseFloatArrayNext(str, new int[1], null, null, null); + } + + public static int parseFloatArrayInfested(String[] tokens, float[] data) { + int len = data.length; + int nTokens = tokens.length; + int n = 0; + int max = 0; + for (int i = 0; i >= 0 && i < len && n < nTokens; i++) { + float f; + while (Float.isNaN(f = parseFloat(tokens[n++])) + && n < nTokens) { + } + if (!Float.isNaN(f)) + data[(max = i)] = f; + if (n == nTokens) + break; + } + return max + 1; + } + + /** + * @param str + * @param next + * @param f + * @param strStart or null + * @param strEnd or null + * @return array of float values + * + */ + public static float[] parseFloatArrayNext(String str, int[] next, float[] f, + String strStart, String strEnd) { + int n = 0; + int pt = next[0]; + if (pt >= 0) { + if (strStart != null) { + int p = str.indexOf(strStart, pt); + if (p >= 0) + next[0] = p + strStart.length(); + } + str = str.substring(next[0]); + pt = (strEnd == null ? -1 : str.indexOf(strEnd)); + if (pt < 0) + pt = str.length(); + else + str = str.substring(0, pt); + next[0] += pt + 1; + String[] tokens = getTokens(str); + if (f == null) + f = new float[tokens.length]; + n = parseFloatArrayInfested(tokens, f); + } + if (f == null) + return new float[0]; + for (int i = n; i < f.length; i++) + f[i] = Float.NaN; + return f; + } + + public static float parseFloatRange(String str, int ichMax, int[] next) { + int cch = str.length(); + if (ichMax > cch) + ichMax = cch; + if (next[0] < 0 || next[0] >= ichMax) + return Float.NaN; + return parseFloatChecked(str, ichMax, next, false); + } + + public static float parseFloatNext(String str, int[] next) { + int cch = (str == null ? -1 : str.length()); + return (next[0] < 0 || next[0] >= cch ? Float.NaN : parseFloatChecked(str, cch, next, false)); + } + + public static float parseFloatStrict(String str) { + // checks trailing characters and does not allow "1E35" to be float + int cch = str.length(); + if (cch == 0) + return Float.NaN; + return parseFloatChecked(str, cch, new int[] {0}, true); + } + + public static float parseFloat(String str) { + return parseFloatNext(str, new int[] {0}); + } + + public static int parseIntRadix(String s, int i) throws NumberFormatException { + /** + * + * JavaScript uses parseIntRadix + * + * @j2sNative + * + * return Integer.parseIntRadix(s, i); + * + */ + { + return Integer.parseInt(s, i); + } + } + + public static String[] getTokens(String line) { + return getTokensAt(line, 0); + } + + public static String parseToken(String str) { + return parseTokenNext(str, new int[] {0}); + } + + public static String parseTrimmed(String str) { + return parseTrimmedRange(str, 0, str.length()); + } + + public static String parseTrimmedAt(String str, int ichStart) { + return parseTrimmedRange(str, ichStart, str.length()); + } + + public static String parseTrimmedRange(String str, int ichStart, int ichMax) { + int cch = str.length(); + if (ichMax < cch) + cch = ichMax; + if (cch < ichStart) + return ""; + return parseTrimmedChecked(str, ichStart, cch); + } + + public static String[] getTokensAt(String line, int ich) { + if (line == null) + return null; + int cchLine = line.length(); + if (ich < 0 || ich > cchLine) + return null; + int tokenCount = countTokens(line, ich); + String[] tokens = new String[tokenCount]; + int[] next = new int[1]; + next[0] = ich; + for (int i = 0; i < tokenCount; ++i) + tokens[i] = parseTokenChecked(line, cchLine, next); + return tokens; + } + + public static int countChar(String line, char c) { + int n = 0; + for (int i = line.lastIndexOf(c) + 1; --i >= 0;) + if (line.charAt(i) == c) + n++; + return n; + } + + public static int countTokens(String line, int ich) { + int tokenCount = 0; + if (line != null) { + int ichMax = line.length(); + while (true) { + while (ich < ichMax && isWhiteSpace(line, ich)) + ++ich; + if (ich == ichMax) + break; + ++tokenCount; + do { + ++ich; + } while (ich < ichMax && !isWhiteSpace(line, ich)); + } + } + return tokenCount; + } + + public static String parseTokenNext(String str, int[] next) { + int cch = str.length(); + return (next[0] < 0 || next[0] >= cch ? null : parseTokenChecked(str, cch, next)); + } + + public static String parseTokenRange(String str, int ichMax, int[] next) { + int cch = str.length(); + if (ichMax > cch) + ichMax = cch; + return (next[0] < 0 || next[0] >= ichMax ? null : parseTokenChecked(str, ichMax, next)); + } + + public static String parseTokenChecked(String str, int ichMax, int[] next) { + int ich = next[0]; + while (ich < ichMax && isWhiteSpace(str, ich)) + ++ich; + int ichNonWhite = ich; + while (ich < ichMax && !isWhiteSpace(str, ich)) + ++ich; + next[0] = ich; + return (ichNonWhite == ich ? null : str.substring(ichNonWhite, ich)); + } + + public static String parseTrimmedChecked(String str, int ich, int ichMax) { + while (ich < ichMax && isWhiteSpace(str, ich)) + ++ich; + int ichLast = ichMax - 1; + while (ichLast >= ich && isWhiteSpace(str, ichLast)) + --ichLast; + return (ichLast < ich ? "" : str.substring(ich, ichLast + 1)); + } + + public static double dVal(String s) throws NumberFormatException { + /** + * @j2sNative + * + * if(s==null) + * throw new NumberFormatException("null"); + * var d=parseFloat(s); + * if(isNaN(d)) + * throw new NumberFormatException("Not a Number : "+s); + * return d + * + */ + { + return Double.valueOf(s).doubleValue(); + } + } + + public static float fVal(String s) throws NumberFormatException { + /** + * @j2sNative + * + * return this.dVal(s); + */ + { + return Float.parseFloat(s); + } + } + + public static int parseIntRange(String str, int ichMax, int[] next) { + int cch = str.length(); + if (ichMax > cch) + ichMax = cch; + return (next[0] < 0 || next[0] >= ichMax ? Integer.MIN_VALUE : parseIntChecked(str, ichMax, next)); + } + + /** + * parses a string array for floats. Returns NaN for nonfloats. + * + * @param tokens the strings to parse + * @param data the array to fill + */ + public static void parseFloatArrayData(String[] tokens, float[] data) { + parseFloatArrayDataN(tokens, data, data.length); + } + + /** + * parses a string array for floats. Returns NaN for nonfloats or missing data. + * + * @param tokens the strings to parse + * @param data the array to fill + * @param nData the number of elements + */ + public static void parseFloatArrayDataN(String[] tokens, float[] data, int nData) { + for (int i = nData; --i >= 0;) + data[i] = (i >= tokens.length ? Float.NaN : parseFloat(tokens[i])); + } + + /** + * + * proper splitting, even for Java 1.3 -- if the text ends in the run, + * no new line is appended. + * + * @param text + * @param run + * @return String array + */ + public static String[] split(String text, String run) { + if (text.length() == 0) + return new String[0]; + int n = 1; + int i = text.indexOf(run); + String[] lines; + int runLen = run.length(); + if (i < 0 || runLen == 0) { + lines = new String[1]; + lines[0] = text; + return lines; + } + int len = text.length() - runLen; + for (; i >= 0 && i < len; n++) + i = text.indexOf(run, i + runLen); + lines = new String[n]; + i = 0; + int ipt = 0; + int pt = 0; + for (; (ipt = text.indexOf(run, i)) >= 0 && pt + 1 < n;) { + lines[pt++] = text.substring(i, ipt); + i = ipt + runLen; + } + if (text.indexOf(run, len) != len) + len += runLen; + lines[pt] = text.substring(i, len); + return lines; + } + + public final static float FLOAT_MIN_SAFE = 2E-45f; + // Float.MIN_VALUE (1.45E-45) is not reliable with JavaScript because of the float/double difference there + + /// general static string-parsing class /// + + // next[0] tracks the pointer within the string so these can all be static. + // but the methods parseFloat, parseInt, parseToken, parseTrimmed, and getTokens do not require this. + +// public static String concatTokens(String[] tokens, int iFirst, int iEnd) { +// String str = ""; +// String sep = ""; +// for (int i = iFirst; i < iEnd; i++) { +// if (i < tokens.length) { +// str += sep + tokens[i]; +// sep = " "; +// } +// } +// return str; +// } + + public static String getQuotedStringAt(String line, int ipt0) { + int[] next = new int[] { ipt0 }; + return getQuotedStringNext(line, next); + } + + /** + * + * @param line + * @param next passes [current pointer] + * @return quoted string -- does NOT unescape characters + */ + public static String getQuotedStringNext(String line, int[] next) { + int i = next[0]; + if (i < 0 || (i = line.indexOf("\"", i)) < 0) + return ""; + int pt = i + 1; + int len = line.length(); + while (++i < len && line.charAt(i) != '"') + if (line.charAt(i) == '\\') + i++; + next[0] = i + 1; + return line.substring(pt, i); + } + + /** + * CSV format -- escaped quote is "" WITHIN "..." + * + * + * @param line + * @param next int[2] filled with [ptrQuote1, ptrAfterQuote2] + * next[1] will be -1 if unmatched quotes are found (continuation on next line) + * @return unescaped string or null + */ + public static String getCSVString(String line, int[] next) { + int i = next[1]; + if (i < 0 || (i = line.indexOf("\"", i)) < 0) + return null; + int pt = next[0] = i; + int len = line.length(); + boolean escaped = false; + boolean haveEscape = false; + while (++i < len + && (line.charAt(i) != '"' || (escaped = (i + 1 < len && line.charAt(i + 1) == '"')))) + if (escaped) { + escaped = false; + haveEscape = true; + i++; + } + if (i >= len) { + next[1] = -1; + return null; // unmatched + } + next[1] = i + 1; + String s = line.substring(pt + 1, i); + return (haveEscape ? rep(rep(s, "\"\"", "\0"), "\0","\"") : s); + } + + public static boolean isOneOf(String key, String semiList) { + if (semiList.length() == 0) + return false; + if (semiList.charAt(0) != ';') + semiList = ";" + semiList + ";"; + return key.indexOf(";") < 0 && semiList.indexOf(';' + key + ';') >= 0; + } + + public static String getQuotedAttribute(String info, String name) { + int i = info.indexOf(name + "="); + return (i < 0 ? null : getQuotedStringAt(info, i)); + } + + public static float approx(float f, float n) { + return Math.round (f * n) / n; + } + + /** + * Does a clean ITERATIVE replace of strFrom in str with strTo. + * Thus, rep("Testttt", "tt","t") becomes "Test". + * + * @param str + * @param strFrom + * @param strTo + * @return replaced string + */ + public static String rep(String str, String strFrom, String strTo) { + if (str == null || strFrom.length() == 0 || str.indexOf(strFrom) < 0) + return str; + boolean isOnce = (strTo.indexOf(strFrom) >= 0); + do { + str = str.replace(strFrom, strTo); + } while (!isOnce && str.indexOf(strFrom) >= 0); + return str; + } + + public static String formatF(float value, int width, int precision, + boolean alignLeft, boolean zeroPad) { + return formatS(DF.formatDecimal(value, precision), width, 0, alignLeft, zeroPad); + } + + /** + * + * @param value + * @param width + * @param precision + * @param alignLeft + * @param zeroPad + * @param allowOverflow IGNORED + * @return formatted string + */ + public static String formatD(double value, int width, int precision, + boolean alignLeft, boolean zeroPad, boolean allowOverflow) { + return formatS(DF.formatDecimal((float)value, -1 - precision), width, 0, alignLeft, zeroPad); + } + + /** + * + * @param value + * @param width number of columns + * @param precision precision > 0 ==> precision = number of characters max from left + * precision < 0 ==> -1 - precision = number of char. max from right + * @param alignLeft + * @param zeroPad generally for numbers turned strings + * @return formatted string + */ + public static String formatS(String value, int width, int precision, + boolean alignLeft, boolean zeroPad) { + if (value == null) + return ""; + int len = value.length(); + if (precision != Integer.MAX_VALUE && precision > 0 + && precision < len) + value = value.substring(0, precision); + else if (precision < 0 && len + precision >= 0) + value = value.substring(len + precision + 1); + + int padLength = width - value.length(); + if (padLength <= 0) + return value; + boolean isNeg = (zeroPad && !alignLeft && value.charAt(0) == '-'); + char padChar = (zeroPad ? '0' : ' '); + char padChar0 = (isNeg ? '-' : padChar); + + SB sb = new SB(); + if (alignLeft) + sb.append(value); + sb.appendC(padChar0); + for (int i = padLength; --i > 0;) + // this is correct, not >= 0 + sb.appendC(padChar); + if (!alignLeft) + sb.append(isNeg ? padChar + value.substring(1) : value); + return sb.toString(); + } + + /** + * Does a clean replace of any of the characters in str with chrTo + * If strTo contains strFrom, then only a single pass is done. + * Otherwise, multiple passes are made until no more replacements can be made. + * + * @param str + * @param strFrom + * @param chTo + * @return replaced string + */ + public static String replaceWithCharacter(String str, String strFrom, + char chTo) { + if (str == null) + return null; + for (int i = strFrom.length(); --i >= 0;) + str = str.replace(strFrom.charAt(i), chTo); + return str; + } + + /** + * Does a clean replace of any of the characters in str with strTo + * If strTo contains strFrom, then only a single pass is done. + * Otherwise, multiple passes are made until no more replacements can be made. + * + * @param str + * @param strFrom + * @param strTo + * @return replaced string + */ + public static String replaceAllCharacters(String str, String strFrom, + String strTo) { + for (int i = strFrom.length(); --i >= 0;) { + String chFrom = strFrom.substring(i, i + 1); + str = rep(str, chFrom, strTo); + } + return str; + } + + public static String trim(String str, String chars) { + if (str == null || str.length() == 0) + return str; + if (chars.length() == 0) + return str.trim(); + int len = str.length(); + int k = 0; + while (k < len && chars.indexOf(str.charAt(k)) >= 0) + k++; + int m = str.length() - 1; + while (m > k && chars.indexOf(str.charAt(m)) >= 0) + m--; + return str.substring(k, m + 1); + } + + public static String trimQuotes(String value) { + return (value != null && value.length() > 1 && value.startsWith("\"") + && value.endsWith("\"") ? value.substring(1, value.length() - 1) + : value); + } + + public static boolean isNonStringPrimitive(Object info) { + // note that we don't use Double, Float, or Integer here + // because in JavaScript those would be false for unwrapped primitives + // coming from equivalent of Array.get() + // Strings will need their own escaped processing + + return info instanceof Number || info instanceof Boolean; + } + + private static Object arrayGet(Object info, int i) { + /** + * + * Note that info will be a primitive in JavaScript + * but a wrapped primitive in Java. + * + * @j2sNative + * + * return info[i]; + */ + { + return Array.get(info, i); + } + } + + @SuppressWarnings("unchecked") + public static String toJSON(String infoType, Object info) { + if (info == null) + return packageJSON(infoType, null); + if (isNonStringPrimitive(info)) + return packageJSON(infoType, info.toString()); + String s = null; + SB sb = null; + while (true) { + if (info instanceof String) { + s = (String) info; + /** + * @j2sNative + * + * if (typeof s == "undefined") s = "null" + * + */ + {} + + if (s.indexOf("{\"") != 0) { + //don't doubly fix JSON strings when retrieving status + // what about \1 \2 \3 etc.? + s = esc(s); + } + break; + } + if (info instanceof JSONEncodable) { + // includes javajs.util.BS, org.jmol.script.SV + if ((s = ((JSONEncodable) info).toJSON()) == null) + s = "null"; // perhaps a list has a null value (group3List, for example) + break; + } + sb = new SB(); + if (info instanceof Map) { + sb.append("{ "); + String sep = ""; + for (String key : ((Map) info).keySet()) { + sb.append(sep).append( + packageJSON(key, toJSON(null, ((Map) info).get(key)))); + sep = ","; + } + sb.append(" }"); + break; + } + if (info instanceof Lst) { + sb.append("[ "); + int n = ((Lst) info).size(); + for (int i = 0; i < n; i++) { + if (i > 0) + sb.appendC(','); + sb.append(toJSON(null, ((Lst) info).get(i))); + } + sb.append(" ]"); + break; + } + if (info instanceof M34) { + // M4 extends M3 + int len = (info instanceof M4 ? 4 : 3); + float[] x = new float[len]; + M34 m = (M34) info; + sb.appendC('['); + for (int i = 0; i < len; i++) { + if (i > 0) + sb.appendC(','); + m.getRow(i, x); + sb.append(toJSON(null, x)); + } + sb.appendC(']'); + break; + } + s = nonArrayString(info); + if (s == null) { + sb.append("["); + int n = AU.getLength(info); + for (int i = 0; i < n; i++) { + if (i > 0) + sb.appendC(','); + sb.append(toJSON(null, arrayGet(info, i))); + } + sb.append("]"); + break; + } + info = info.toString(); + } + return packageJSON(infoType, (s == null ? sb.toString() : s)); + } + + /** + * Checks to see if an object is an array (including typed arrays), and if it is, returns null; + * otherwise it returns the string equivalent of that object. + * + * @param x + * @return String or null + */ + public static String nonArrayString(Object x) { + /** + * @j2sNative + * + * return (x.constructor == Array || x.__BYTESIZE ? null : x.toString()); + * + */ + { + try { + Array.getLength(x); + return null; + } catch (Exception e) { + return x.toString(); + } + } + } + + public static String byteArrayToJSON(byte[] data) { + SB sb = new SB(); + sb.append("["); + int n = data.length; + for (int i = 0; i < n; i++) { + if (i > 0) + sb.appendC(','); + sb.appendI(data[i] & 0xFF); + } + sb.append("]"); + return sb.toString(); + } + + public static String packageJSON(String infoType, String info) { + return (infoType == null ? info : "\"" + infoType + "\": " + info); + } + + public static String escapeUrl(String url) { + url = rep(url, "\n", ""); + url = rep(url, "%", "%25"); + url = rep(url, "#", "%23"); + url = rep(url, "[", "%5B"); + url = rep(url, "\\", "%5C"); + url = rep(url, "]", "%5D"); + url = rep(url, " ", "%20"); + return url; + } + + private final static String escapable = "\\\\\tt\rr\nn\"\""; + + public static String esc(String str) { + if (str == null || str.length() == 0) + return "\"\""; + boolean haveEscape = false; + int i = 0; + for (; i < escapable.length(); i += 2) + if (str.indexOf(escapable.charAt(i)) >= 0) { + haveEscape = true; + break; + } + if (haveEscape) + while (i < escapable.length()) { + int pt = -1; + char ch = escapable.charAt(i++); + char ch2 = escapable.charAt(i++); + SB sb = new SB(); + int pt0 = 0; + while ((pt = str.indexOf(ch, pt + 1)) >= 0) { + sb.append(str.substring(pt0, pt)).appendC('\\').appendC(ch2); + pt0 = pt + 1; + } + sb.append(str.substring(pt0, str.length())); + str = sb.toString(); + } + return "\"" + escUnicode(str) + "\""; + } + + public static String escUnicode(String str) { + for (int i = str.length(); --i >= 0;) + if (str.charAt(i) > 0x7F) { + String s = "0000" + Integer.toHexString(str.charAt(i)); + str = str.substring(0, i) + "\\u" + s.substring(s.length() - 4) + + str.substring(i + 1); + } + return str; + } + + /** + * ensures that a float turned to string has a decimal point + * + * @param f + * @return string version of float + */ + public static String escF(float f) { + String sf = "" + f; + /** + * @j2sNative + * + * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0) + * sf += ".0"; + */ + { + } + return sf; + } + public static String join(String[] s, char c, int i0) { + if (s.length < i0) + return null; + SB sb = new SB(); + sb.append(s[i0++]); + for (int i = i0; i < s.length; i++) + sb.appendC(c).append(s[i]); + return sb.toString(); + } + + /** + * a LIKE "x" a is a string and equals x + * + * a LIKE "*x" a is a string and ends with x + * + * a LIKE "x*" a is a string and starts with x + * + * a LIKE "*x*" a is a string and contains x + * + * @param a + * @param b + * @return a LIKE b + */ + public static boolean isLike(String a, String b) { + boolean areEqual = a.equals(b); + if (areEqual) + return true; + boolean isStart = b.startsWith("*"); + boolean isEnd = b.endsWith("*"); + return (!isStart && !isEnd) ? areEqual + : isStart && isEnd ? b.length() == 1 || a.contains(b.substring(1, b.length() - 1)) + : isStart ? a.endsWith(b.substring(1)) + : a.startsWith(b.substring(0, b.length() - 1)); + } + + public static Object getMapValueNoCase(Map h, String key) { + if ("this".equals(key)) + return h; + Object val = h.get(key); + if (val == null) + for (Entry e : h.entrySet()) + if (e.getKey().equalsIgnoreCase(key)) + return e.getValue(); + return val; + } + + public static String clean(String s) { + return rep(replaceAllCharacters(s, " \t\n\r", " "), " ", " ").trim(); + } + + /** + * + * fdup duplicates p or q formats for formatCheck + * and the format() function. + * + * @param f + * @param pt + * @param n + * @return %3.5q%3.5q%3.5q%3.5q or %3.5p%3.5p%3.5p + */ + public static String fdup(String f, int pt, int n) { + char ch; + int count = 0; + for (int i = pt; --i >= 1; ) { + if (isDigit(ch = f.charAt(i))) + continue; + switch (ch) { + case '.': + if (count++ != 0) + return f; + continue; + case '-': + if (i != 1 && f.charAt(i - 1) != '.') + return f; + continue; + default: + return f; + } + } + String s = f.substring(0, pt + 1); + SB sb = new SB(); + for (int i = 0; i < n; i++) + sb.append(s); + sb.append(f.substring(pt + 1)); + return sb.toString(); + } + + /** + * generic string formatter based on formatLabel in Atom + * + * + * @param strFormat .... %width.precisionKEY.... + * @param key any string to match + * @param strT replacement string or null + * @param floatT replacement float or Float.NaN + * @param doubleT replacement double or Double.NaN -- for exponential + * @param doOne mimic sprintf + * @return formatted string + */ + + private static String formatString(String strFormat, String key, String strT, + float floatT, double doubleT, boolean doOne) { + if (strFormat == null) + return null; + if ("".equals(strFormat)) + return ""; + int len = key.length(); + if (strFormat.indexOf("%") < 0 || len == 0 || strFormat.indexOf(key) < 0) + return strFormat; + + String strLabel = ""; + int ich, ichPercent, ichKey; + for (ich = 0; (ichPercent = strFormat.indexOf('%', ich)) >= 0 + && (ichKey = strFormat.indexOf(key, ichPercent + 1)) >= 0;) { + if (ich != ichPercent) + strLabel += strFormat.substring(ich, ichPercent); + ich = ichPercent + 1; + if (ichKey > ichPercent + 6) { + strLabel += '%'; + continue;//%12.10x + } + try { + boolean alignLeft = false; + if (strFormat.charAt(ich) == '-') { + alignLeft = true; + ++ich; + } + boolean zeroPad = false; + if (strFormat.charAt(ich) == '0') { + zeroPad = true; + ++ich; + } + char ch; + int width = 0; + while ((ch = strFormat.charAt(ich)) >= '0' && (ch <= '9')) { + width = (10 * width) + (ch - '0'); + ++ich; + } + int precision = Integer.MAX_VALUE; + boolean isExponential = false; + if (strFormat.charAt(ich) == '.') { + ++ich; + if ((ch = strFormat.charAt(ich)) == '-') { + isExponential = (strT == null); + ++ich; + } + if ((ch = strFormat.charAt(ich)) >= '0' && ch <= '9') { + precision = ch - '0'; + ++ich; + } + if (isExponential) + precision = -precision; + } + String st = strFormat.substring(ich, ich + len); + if (!st.equals(key)) { + ich = ichPercent + 1; + strLabel += '%'; + continue; + } + ich += len; + if (!Float.isNaN(floatT)) // 'f' + strLabel += formatF(floatT, width, precision, alignLeft, + zeroPad); + else if (strT != null) // 'd' 'i' or 's' + strLabel += formatS(strT, width, precision, alignLeft, + zeroPad); + else if (!Double.isNaN(doubleT)) // 'e' + strLabel += formatD(doubleT, width, precision - 1, alignLeft, + zeroPad, true); + if (doOne) + break; + } catch (IndexOutOfBoundsException ioobe) { + ich = ichPercent; + break; + } + } + strLabel += strFormat.substring(ich); + //if (strLabel.length() == 0) + //return null; + return strLabel; + } + + public static String formatStringS(String strFormat, String key, String strT) { + return formatString(strFormat, key, strT, Float.NaN, Double.NaN, false); + } + + public static String formatStringF(String strFormat, String key, float floatT) { + return formatString(strFormat, key, null, floatT, Double.NaN, false); + } + + public static String formatStringI(String strFormat, String key, int intT) { + return formatString(strFormat, key, "" + intT, Float.NaN, Double.NaN, false); + } + + /** + * sprintf emulation uses (almost) c++ standard string formats + * + * 's' string 'i' or 'd' integer, 'e' double, 'f' float, 'p' point3f 'q' + * quaternion/plane/axisangle with added "i" (equal to the insipid "d" -- + * digits?) + * + * @param strFormat + * @param list + * a listing of what sort of data will be found in Object[] values, in + * order: s string, f float, i integer, d double, p point3f, q + * quaternion/point4f, S String[], F float[], I int[], and D double[] + * @param values + * Object[] containing above types + * @return formatted string + */ + public static String sprintf(String strFormat, String list, Object[] values) { + if (values == null) + return strFormat; + int n = list.length(); + if (n == values.length) + try { + for (int o = 0; o < n; o++) { + if (values[o] == null) + continue; + switch (list.charAt(o)) { + case 's': + strFormat = formatString(strFormat, "s", (String) values[o], + Float.NaN, Double.NaN, true); + break; + case 'f': + strFormat = formatString(strFormat, "f", null, ((Float) values[o]) + .floatValue(), Double.NaN, true); + break; + case 'i': + strFormat = formatString(strFormat, "d", "" + values[o], Float.NaN, + Double.NaN, true); + strFormat = formatString(strFormat, "i", "" + values[o], Float.NaN, + Double.NaN, true); + break; + case 'd': + strFormat = formatString(strFormat, "e", null, Float.NaN, + ((Double) values[o]).doubleValue(), true); + break; + case 'p': + T3 pVal = (T3) values[o]; + strFormat = formatString(strFormat, "p", null, pVal.x, Double.NaN, + true); + strFormat = formatString(strFormat, "p", null, pVal.y, Double.NaN, + true); + strFormat = formatString(strFormat, "p", null, pVal.z, Double.NaN, + true); + break; + case 'q': + T4 qVal = (T4) values[o]; + strFormat = formatString(strFormat, "q", null, qVal.x, Double.NaN, + true); + strFormat = formatString(strFormat, "q", null, qVal.y, Double.NaN, + true); + strFormat = formatString(strFormat, "q", null, qVal.z, Double.NaN, + true); + strFormat = formatString(strFormat, "q", null, qVal.w, Double.NaN, + true); + break; + case 'S': + String[] sVal = (String[]) values[o]; + for (int i = 0; i < sVal.length; i++) + strFormat = formatString(strFormat, "s", sVal[i], Float.NaN, + Double.NaN, true); + break; + case 'F': + float[] fVal = (float[]) values[o]; + for (int i = 0; i < fVal.length; i++) + strFormat = formatString(strFormat, "f", null, fVal[i], + Double.NaN, true); + break; + case 'I': + int[] iVal = (int[]) values[o]; + for (int i = 0; i < iVal.length; i++) + strFormat = formatString(strFormat, "d", "" + iVal[i], Float.NaN, + Double.NaN, true); + for (int i = 0; i < iVal.length; i++) + strFormat = formatString(strFormat, "i", "" + iVal[i], Float.NaN, + Double.NaN, true); + break; + case 'D': + double[] dVal = (double[]) values[o]; + for (int i = 0; i < dVal.length; i++) + strFormat = formatString(strFormat, "e", null, Float.NaN, + dVal[i], true); + } + + } + return rep(strFormat, "%%", "%"); + } catch (Exception e) { + // + } + System.out.println("TextFormat.sprintf error " + list + " " + strFormat); + return rep(strFormat, "%", "?"); + } + + /** + * + * formatCheck checks p and q formats and duplicates if necessary + * "%10.5p xxxx" ==> "%10.5p%10.5p%10.5p xxxx" + * + * @param strFormat + * @return f or dupicated format + */ + public static String formatCheck(String strFormat) { + if (strFormat == null || strFormat.indexOf('p') < 0 && strFormat.indexOf('q') < 0) + return strFormat; + strFormat = rep(strFormat, "%%", "\1"); + strFormat = rep(strFormat, "%p", "%6.2p"); + strFormat = rep(strFormat, "%q", "%6.2q"); + String[] format = split(strFormat, "%"); + SB sb = new SB(); + sb.append(format[0]); + for (int i = 1; i < format.length; i++) { + String f = "%" + format[i]; + int pt; + if (f.length() >= 3) { + if ((pt = f.indexOf('p')) >= 0) + f = fdup(f, pt, 3); + if ((pt = f.indexOf('q')) >= 0) + f = fdup(f, pt, 4); + } + sb.append(f); + } + return sb.toString().replace('\1', '%'); + } + + public static void leftJustify(SB s, String s1, String s2) { + s.append(s2); + int n = s1.length() - s2.length(); + if (n > 0) + s.append(s1.substring(0, n)); + } + + public static void rightJustify(SB s, String s1, String s2) { + int n = s1.length() - s2.length(); + if (n > 0) + s.append(s1.substring(0, n)); + s.append(s2); + } + + public static String safeTruncate(float f, int n) { + if (f > -0.001 && f < 0.001) + f = 0; + return (f + " ").substring(0,n); + } + + public static boolean isWild(String s) { + return s != null && (s.indexOf("*") >= 0 || s.indexOf("?") >= 0); + } + + /** + * A general non-regex (for performance) text matcher that utilizes ? and *. + * + * ??? means "at most three" characters if at beginning or end; + * "exactly three" otherwise + * \1 in search is a stand-in for actual ? + * + * @param search + * the string to search + * @param match + * the match string + * @param checkStar + * @param allowInitialStar + * @return true if found + */ + public static boolean isMatch(String search, String match, boolean checkStar, + boolean allowInitialStar) { + // search == match --> true + if (search.equals(match)) + return true; + int mLen = match.length(); + // match == "" --> false + if (mLen == 0) + return false; + boolean isStar0 = (checkStar && allowInitialStar ? match.charAt(0) == '*' + : false); + // match == "*" --> true + if (mLen == 1 && isStar0) + return true; + boolean isStar1 = (checkStar && match.endsWith("*")); + boolean haveQ = (match.indexOf('?') >= 0); + // match == "**" --> true + // match == "*xxx*" --> search contains "xxx" + // match == "*xxx" --> search ends with "xxx" + // match == "xxx*" --> search starts with "xxx" + if (!haveQ) { + if (isStar0) + return (isStar1 ? (mLen < 3 || search.indexOf(match.substring(1, + mLen - 1)) >= 0) : search.endsWith(match.substring(1))); + else if (isStar1) + return search.startsWith(match.substring(0, mLen - 1)); + } + int sLen = search.length(); + // pad match with "?" -- same as * + String qqqq = "????"; + int nq = 4; + while (nq < sLen) { + qqqq += qqqq; + nq += 4; + } + if (checkStar) { + if (isStar0) { + match = qqqq + match.substring(1); + mLen += nq - 1; + } + if (isStar1) { + match = match.substring(0, mLen - 1) + qqqq; + mLen += nq - 1; + } + } + // length of match < length of search --> false + if (mLen < sLen) + return false; + + // -- each ? matches ONE character if not at end + // -- extra ? at end ignored + + // (allowInitialStar == true) + // -- extra ? at beginning reduced to match length + + int ich = 0; + while (mLen > sLen) { + if (allowInitialStar && match.charAt(ich) == '?') { + ++ich; + } else if (match.charAt(ich + mLen - 1) != '?') { + return false; + } + --mLen; + } + + // both are effectively same length now. + // \1 is stand-in for "?" + + for (int i = sLen; --i >= 0;) { + char chm = match.charAt(ich + i); + if (chm == '?') + continue; + char chs = search.charAt(i); + if (chm != chs && (chm != '\1' || chs != '?')) + return false; + } + return true; + } + + public static String replaceQuotedStrings(String s, Lst list, + Lst newList) { + int n = list.size(); + for (int i = 0; i < n; i++) { + String name = list.get(i); + String newName = newList.get(i); + if (!newName.equals(name)) + s = rep(s, "\"" + name + "\"", "\"" + newName + + "\""); + } + return s; + } + + public static String replaceStrings(String s, Lst list, + Lst newList) { + int n = list.size(); + for (int i = 0; i < n; i++) { + String name = list.get(i); + String newName = newList.get(i); + if (!newName.equals(name)) + s = rep(s, name, newName); + } + return s; + } + + public static boolean isDigit(char ch) { + // just way simpler code than Character.isDigit(ch); + int c = ch; + return (48 <= c && c <= 57); + } + + public static boolean isUpperCase(char ch) { + int c = ch; + return (65 <= c && c <= 90); + } + + public static boolean isLowerCase(char ch) { + int c = ch; + return (97 <= c && c <= 122); + } + + public static boolean isLetter(char ch) { + // just way simpler code than Character.isLetter(ch); + int c = ch; + return (65 <= c && c <= 90 || 97 <= c && c <= 122); + } + + public static boolean isLetterOrDigit(char ch) { + // just way simpler code than Character.isLetterOrDigit(ch); + int c = ch; + return (65 <= c && c <= 90 || 97 <= c && c <= 122 || 48 <= c && c <= 57); + } + + public static boolean isWhitespace(char ch) { + int c = ch; + return (c >= 0x1c && c <= 0x20 || c >= 0x9 && c <= 0xd); + } + + public static final float FRACTIONAL_PRECISION = 100000f; + public static final float CARTESIAN_PRECISION = 10000f; + + public static void fixPtFloats(T3 pt, float f) { + //this will equate float and double as long as -256 <= x <= 256 + pt.x = Math.round(pt.x * f) / f; + pt.y = Math.round(pt.y * f) / f; + pt.z = Math.round(pt.z * f) / f; + } + + public static double fixDouble(double d, double f) { + return Math.round(d * f) / f; + } + + /** + * parse a float or "float/float" + * @param s + * @return a/b + */ + public static float parseFloatFraction(String s) { + int pt = s.indexOf("/"); + return (pt < 0 ? parseFloat(s) : parseFloat(s.substring(0, pt)) + / parseFloat(s.substring(pt + 1))); + } + +//static { +// +// double d = 790.8999998888; +// float x = 790.8999998888f; +// for (int i = 0; i < 50; i++) { +// System.out.println(x + " " + d); +// System.out.println(Math.round(x * 100000) / 100000f); +// System.out.println(Math.round(d * 100000) / 100000.); +// System.out.println(Math.round(x * 10000) / 10000f); +// System.out.println(Math.round(d * 10000) / 10000.); +// x+=1; +// d+=1; +// } +// System.out.println(100.123456789f); +//} + +// static { +// long t; +// char c = '0'; +// t = System.currentTimeMillis(); +// for (int i = 0; i < 10000000; i++) { +// boolean b = PT.isUpperCase(c); +// } +// System.out.println(System.currentTimeMillis() - t); +// +// t = System.currentTimeMillis(); +// for (int i = 0; i < 10000000; i++) { +// boolean b = Character.isUpperCase(c); +// } +// System.out.println(System.currentTimeMillis() - t); +// +// t = System.currentTimeMillis(); +// for (int i = 0; i < 10000000; i++) { +// boolean b = PT.isUpperCase(c); +// } +// System.out.println(System.currentTimeMillis() - t); +// +// System.out.println("PT test"); +// } +} diff --git a/src/javajs/util/Quat.java b/src/javajs/util/Quat.java new file mode 100644 index 0000000..5777308 --- /dev/null +++ b/src/javajs/util/Quat.java @@ -0,0 +1,818 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2007-04-05 09:07:28 -0500 (Thu, 05 Apr 2007) $ + * $Revision: 7326 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2003-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.util; + +/* + * Standard UNIT quaternion math -- for rotation. + * + * All rotations can be represented as two identical quaternions. + * This is because any rotation can be considered from either end of the + * rotational axis -- either as a + rotation or a - rotation. This code + * is designed to always maintain the quaternion with a rotation in the + * [0, PI) range. + * + * This ensures that the reported theta is always positive, and the normal + * reported is always associated with a positive theta. + * + * @author Bob Hanson, hansonr@stolaf.edu 6/2008 + * + */ + +public class Quat { + public float q0, q1, q2, q3; + private M3 mat; + + private final static P4 qZero = new P4(); + private static final double RAD_PER_DEG = Math.PI / 180; + + public Quat() { + q0 = 1; + } + + public static Quat newQ(Quat q) { + Quat q1 = new Quat(); + q1.set(q); + return q1; + } + + public static Quat newVA(T3 v, float theta) { + Quat q = new Quat(); + q.setTA(v, theta); + return q; + } + + public static Quat newM(M3 mat) { + Quat q = new Quat(); + q.setM(M3.newM3(mat)); + return q; + } + + public static Quat newAA(A4 a) { + Quat q = new Quat(); + q.setAA(a); + return q; + } + + public static Quat newP4(P4 pt) { + Quat q = new Quat(); + q.setP4(pt); + return q; + } + + /** + * Note that q0 is the last parameter here + * + * @param q1 + * @param q2 + * @param q3 + * @param q0 + * @return {q1 q2 q3 q0} + */ + public static Quat new4(float q1, float q2, float q3, float q0) { + Quat q = new Quat(); + if (q0 < -1) { + q.q0 = -1; + return q; + } + if (q0 > 1) { + q.q0 = 1; + return q; + } + q.q0 = q0; + q.q1 = q1; + q.q2 = q2; + q.q3 = q3; + return q; + } + + public void set(Quat q) { + q0 = q.q0; + q1 = q.q1; + q2 = q.q2; + q3 = q.q3; + } + + /** + * {x y z w} --> {q1 q2 q3 q0} and factored + * + * @param pt + */ + private void setP4(P4 pt) { + float factor = (pt == null ? 0 : pt.distance4(qZero)); + if (factor == 0) { + q0 = 1; + return; + } + q0 = pt.w / factor; + q1 = pt.x / factor; + q2 = pt.y / factor; + q3 = pt.z / factor; + } + + /** + * q = (cos(theta/2), sin(theta/2) * n) + * + * @param pt + * @param theta + */ + public void setTA(T3 pt, float theta) { + if (pt.x == 0 && pt.y == 0 && pt.z == 0) { + q0 = 1; + return; + } + double fact = (Math.sin(theta / 2 * RAD_PER_DEG) / Math.sqrt(pt.x + * pt.x + pt.y * pt.y + pt.z * pt.z)); + q0 = (float) (Math.cos(theta / 2 * RAD_PER_DEG)); + q1 = (float) (pt.x * fact); + q2 = (float) (pt.y * fact); + q3 = (float) (pt.z * fact); + } + + public void setAA(A4 a) { + A4 aa = A4.newAA(a); + if (aa.angle == 0) + aa.y = 1; + setM(new M3().setAA(aa)); + } + + private void setM(M3 mat) { + + /* + * Changed 7/16/2008 to double precision for 11.5.48. + * + * + * + * RayTrace Software Package, release 3.0. May 3, 2006. + * + * Mathematics Subpackage (VrMath) + * + * Author: Samuel R. Buss + * + * Software is "as-is" and carries no warranty. It may be used without + * restriction, but if you modify it, please change the filenames to + * prevent confusion between different versions. Please acknowledge + * all use of the software in any publications or products based on it. + * + * Bug reports: Sam Buss, sbuss@ucsd.edu. + * Web page: http://math.ucsd.edu/~sbuss/MathCG + + // Use Shepperd's algorithm, which is stable, does not lose + // significant precision and uses only one sqrt. + // J. Guidance and Control, 1 (1978) 223-224. + + * + * + * Except, that code has errors. + * + * CORRECTIONS (as noted below) of Quaternion.cpp. I have reported the bug. + * + * -- Bob Hanson + * + * theory: + * cos(theta/2)^2 = (cos(theta) + 1)/2 + * and + * trace = (1-x^2)ct + (1-y^2)ct + (1-z^2)ct + 1 = 2cos(theta) + 1 + * or + * cos(theta) = (trace - 1)/2 + * + * so in general, + * + * w = cos(theta/2) + * = sqrt((cos(theta)+1)/2) + * = sqrt((trace-1)/4+1/2) + * = sqrt((trace+1)/4) + * = sqrt(trace+1)/2 + * + * but there are precision issues, so we allow for other situations. + * note -- trace >= 0.5 when cos(theta) >= -0.25 (-104.48 <= theta <= 104.48). + * this code cleverly matches the precision in all four options. + * + */ + + this.mat = mat; + + double trace = mat.m00 + mat.m11 + mat.m22; + double temp; + double w, x, y, z; + if (trace >= 0.5) { + w = Math.sqrt(1.0 + trace); + x = (mat.m21 - mat.m12) / w; + y = (mat.m02 - mat.m20) / w; + z = (mat.m10 - mat.m01) / w; + } else if ((temp = mat.m00 + mat.m00 - trace) >= 0.5) { + x = Math.sqrt(1.0 + temp); + w = (mat.m21 - mat.m12) / x; + y = (mat.m10 + mat.m01) / x; + z = (mat.m20 + mat.m02) / x; + } else if ((temp = mat.m11 + mat.m11 - trace) >= 0.5 + || mat.m11 > mat.m22) { + y = Math.sqrt(1.0 + temp); + w = (mat.m02 - mat.m20) / y; + x = (mat.m10 + mat.m01) / y; + z = (mat.m21 + mat.m12) / y; + } else { + z = Math.sqrt(1.0 + mat.m22 + mat.m22 - trace); + w = (mat.m10 - mat.m01) / z; + x = (mat.m20 + mat.m02) / z; // was - + y = (mat.m21 + mat.m12) / z; // was - + } + + q0 = (float) (w * 0.5); + q1 = (float) (x * 0.5); + q2 = (float) (y * 0.5); + q3 = (float) (z * 0.5); + + /* + * Originally from http://www.gamedev.net/community/forums/topic.asp?topic_id=448380 + * later algorithm was adapted from Visualizing Quaternions, by Andrew J. Hanson + * (Morgan Kaufmann, 2006), page 446 + * + * HOWEVER, checking with AxisAngle4f and Quat4f equivalents, it was found that + * BOTH of these sources produce inverted quaternions. So here we do an inversion. + * + * This correction was made in 11.5.42 6/19/2008 -- Bob Hanson + * + * former algorithm used: + * / + + double tr = mat.m00 + mat.m11 + mat.m22; //Matrix trace + double s; + double[] q = new double[4]; + if (tr > 0) { + s = Math.sqrt(tr + 1); + q0 = (float) (0.5 * s); + s = 0.5 / s; // = 1/q0 + q1 = (float) ((mat.m21 - mat.m12) * s); + q2 = (float) ((mat.m02 - mat.m20) * s); + q3 = (float) ((mat.m10 - mat.m01) * s); + } else { + float[][] m = new float[][] { new float[3], new float[3], new float[3] }; + mat.getRow(0, m[0]); + mat.getRow(1, m[1]); + mat.getRow(2, m[2]); + + //Find out the biggest element along the diagonal + float max = Math.max(mat.m11, mat.m00); + int i = (mat.m22 > max ? 2 : max == mat.m11 ? 1 : 0); + int j = (i + 1) % 3; + int k = (j + 1) % 3; + s = -Math.sqrt(1 + m[i][i] - m[j][j] - m[k][k]); + // 0 = 1 + (1-x^2)ct + x^2 -(1-y^2)ct - y^2 - (1-z^2)ct - z^2 + // 0 = 1 - ct + (x^2 - y^2 - z^2) - (x^2 - y^2 - z^2)ct + // 0 = 1 - ct + 2x^2 - 1 - (2x^2)ct + ct + // 0 = 2x^2(1 - ct) + // theta = 0 (but then trace = 1 + 1 + 1 = 3) + // or x = 0. + q[i] = s * 0.5; + if (s != 0) + s = 0.5 / s; // = 1/q[i] + q[j] = (m[i][j] + m[j][i]) * s; + q[k] = (m[i][k] + m[k][i]) * s; + q0 = (float) ((m[k][j] - m[j][k]) * s); + q1 = (float) q[0]; // x + q2 = (float) q[1]; // y + q3 = (float) q[2]; // z + } + + */ + } + + /* + * if qref is null, "fix" this quaternion + * otherwise, return a quaternion that is CLOSEST to the given quaternion + * that is, one that gives a positive dot product + * + */ + public void setRef(Quat qref) { + if (qref == null) { + mul(getFixFactor()); + return; + } + if (dot(qref) >= 0) + return; + q0 *= -1; + q1 *= -1; + q2 *= -1; + q3 *= -1; + } + + /** + * returns a quaternion frame based on three points (center, x, and any point in xy plane) + * or two vectors (vA, vB). + * + * @param center (null for vA/vB option) + * @param x + * @param xy + * @return quaternion for frame + */ + public static final Quat getQuaternionFrame(P3 center, T3 x, + T3 xy) { + V3 vA = V3.newV(x); + V3 vB = V3.newV(xy); + if (center != null) { + vA.sub(center); + vB.sub(center); + } + return getQuaternionFrameV(vA, vB, null, false); + } + + /** + * Create a quaternion based on a frame + * @param vA + * @param vB + * @param vC + * @param yBased + * @return quaternion + */ + public static final Quat getQuaternionFrameV(V3 vA, V3 vB, + V3 vC, boolean yBased) { + if (vC == null) { + vC = new V3(); + vC.cross(vA, vB); + if (yBased) + vA.cross(vB, vC); + } + V3 vBprime = new V3(); + vBprime.cross(vC, vA); + vA.normalize(); + vBprime.normalize(); + vC.normalize(); + M3 mat = new M3(); + mat.setColumnV(0, vA); + mat.setColumnV(1, vBprime); + mat.setColumnV(2, vC); + + /* + * + * Verification tests using Quat4f and AngleAxis4f: + * + System.out.println("quaternion frame matrix: " + mat); + + Point3f pt2 = new Point3f(); + mat.transform(Point3f.new3(1, 0, 0), pt2); + System.out.println("vA=" + vA + " M(100)=" + pt2); + mat.transform(Point3f.new3(0, 1, 0), pt2); + System.out.println("vB'=" + vBprime + " M(010)=" + pt2); + mat.transform(Point3f.new3(0, 0, 1), pt2); + System.out.println("vC=" + vC + " M(001)=" + pt2); + Quat4f q4 = new Quat4f(); + q4.set(mat); + System.out.println("----"); + System.out.println("Quat4f: {" + q4.w + " " + q4.x + " " + q4.y + " " + q4.z + "}"); + System.out.println("Quat4f: 2xy + 2wz = m10: " + (2 * q4.x * q4.y + 2 * q4.w * q4.z) + " = " + mat.m10); + + */ + + Quat q = newM(mat); + + /* + System.out.println("Quaternion mat from q \n" + q.getMatrix()); + System.out.println("Quaternion: " + q.getNormal() + " " + q.getTheta()); + AxisAngle4f a = new AxisAngle4f(); + a.set(mat); + Vector3f v = Vector3f.new3(a.x, a.y, a.z); + v.normalize(); + System.out.println("angleAxis: " + v + " "+(a.angle/Math.PI * 180)); + */ + + return q; + } + + public M3 getMatrix() { + if (mat == null) + setMatrix(); + return mat; + } + + private void setMatrix() { + mat = new M3(); + // q0 = w, q1 = x, q2 = y, q3 = z + mat.m00 = q0 * q0 + q1 * q1 - q2 * q2 - q3 * q3; + mat.m01 = 2 * q1 * q2 - 2 * q0 * q3; + mat.m02 = 2 * q1 * q3 + 2 * q0 * q2; + mat.m10 = 2 * q1 * q2 + 2 * q0 * q3; + mat.m11 = q0 * q0 - q1 * q1 + q2 * q2 - q3 * q3; + mat.m12 = 2 * q2 * q3 - 2 * q0 * q1; + mat.m20 = 2 * q1 * q3 - 2 * q0 * q2; + mat.m21 = 2 * q2 * q3 + 2 * q0 * q1; + mat.m22 = q0 * q0 - q1 * q1 - q2 * q2 + q3 * q3; + } + + public Quat add(float x) { + // scalar theta addition (degrees) + return newVA(getNormal(), getTheta() + x); + } + + public Quat mul(float x) { + // scalar theta multiplication + return (x == 1 ? new4(q1, q2, q3, q0) : + newVA(getNormal(), getTheta() * x)); + } + + public Quat mulQ(Quat p) { + return new4( + q0 * p.q1 + q1 * p.q0 + q2 * p.q3 - q3 * p.q2, + q0 * p.q2 + q2 * p.q0 + q3 * p.q1 - q1 * p.q3, + q0 * p.q3 + q3 * p.q0 + q1 * p.q2 - q2 * p.q1, + q0 * p.q0 - q1 * p.q1 - q2 * p.q2 - q3 * p.q3); + } + + public Quat div(Quat p) { + // unit quaternions assumed -- otherwise would scale by 1/p.dot(p) + return mulQ(p.inv()); + } + + public Quat divLeft(Quat p) { + // unit quaternions assumed -- otherwise would scale by 1/p.dot(p) + return this.inv().mulQ(p); + } + + public float dot(Quat q) { + return this.q0 * q.q0 + this.q1 * q.q1 + this.q2 * q.q2 + this.q3 * q.q3; + } + + public Quat inv() { + return new4(-q1, -q2, -q3, q0); + } + + public Quat negate() { + return new4(-q1, -q2, -q3, -q0); + } + + /** + * ensures + * + * 1) q0 > 0 + * or + * 2) q0 = 0 and q1 > 0 + * or + * 3) q0 = 0 and q1 = 0 and q2 > 0 + * or + * 4) q0 = 0 and q1 = 0 and q2 = 0 and q3 > 0 + * + * @return 1 or -1 + * + */ + + private float getFixFactor() { + return (q0 < 0 || + q0 == 0 && (q1 < 0 || q1 == 0 && (q2 < 0 || q2 == 0 && q3 < 0)) ? -1 : 1); + } + + public V3 getVector(int i) { + return getVectorScaled(i, 1f); + } + + public V3 getVectorScaled(int i, float scale) { + if (i == -1) { + scale *= getFixFactor(); + return V3.new3(q1 * scale, q2 * scale, q3 * scale); + } + if (mat == null) + setMatrix(); + V3 v = new V3(); + mat.getColumnV(i, v); + if (scale != 1f) + v.scale(scale); + return v; + } + + /** + * + * @return vector such that 0 <= angle <= 180 + */ + public V3 getNormal() { + V3 v = getRawNormal(this); + v.scale(getFixFactor()); + return v; + } + + private static V3 getRawNormal(Quat q) { + V3 v = V3.new3(q.q1, q.q2, q.q3); + if (v.length() == 0) + return V3.new3(0, 0, 1); + v.normalize(); + return v; + } + + /** + * + * @return 0 <= angle <= 180 in degrees + */ + public float getTheta() { + return (float) (Math.acos(Math.abs(q0)) * (2 * 180 / Math.PI)); + } + + public float getThetaRadians() { + return (float) (Math.acos(Math.abs(q0)) * 2); + } + + /** + * + * @param v0 + * @return vector option closest to v0 + * + */ + public V3 getNormalDirected(V3 v0) { + V3 v = getNormal(); + if (v.x * v0.x + v.y * v0.y + v.z * v0.z < 0) { + v.scale(-1); + } + return v; + } + + public V3 get3dProjection(V3 v3d) { + v3d.set(q1, q2, q3); + return v3d; + } + + /** + * + * @param axisAngle + * @return fill in theta of axisAngle such that + */ + public P4 getThetaDirected(P4 axisAngle) { + //fills in .w; + float theta = getTheta(); + V3 v = getNormal(); + if (axisAngle.x * q1 + axisAngle.y * q2 + axisAngle.z * q3 < 0) { + v.scale(-1); + theta = -theta; + } + axisAngle.set4(v.x, v.y, v.z, theta); + return axisAngle; + } + + /** + * + * @param vector a vector, same as for getNormalDirected + * @return return theta + */ + public float getThetaDirectedV(V3 vector) { + //fills in .w; + float theta = getTheta(); + V3 v = getNormal(); + if (vector.x * q1 + vector.y * q2 + vector.z * q3 < 0) { + v.scale(-1); + theta = -theta; + } + return theta; + } + + /** + * Quaternions are saved as {q1, q2, q3, q0} + * + * While this may seem odd, it is so that for any point4 -- + * planes, axisangles, and quaternions -- we can use the + * first three coordinates to determine the relavent axis + * the fourth then gives us offset to {0,0,0} (plane), + * rotation angle (axisangle), and cos(theta/2) (quaternion). + * @return {x y z w} (unnormalized) + */ + public P4 toPoint4f() { + return P4.new4(q1, q2, q3, q0); // x,y,z,w + } + + public A4 toAxisAngle4f() { + double theta = 2 * Math.acos(Math.abs(q0)); + double sinTheta2 = Math.sin(theta/2); + V3 v = getNormal(); + if (sinTheta2 < 0) { + v.scale(-1); + theta = Math.PI - theta; + } + return A4.newVA(v, (float) theta); + } + + public T3 transform2(T3 pt, T3 ptNew) { + if (mat == null) + setMatrix(); + mat.rotate2(pt, ptNew); + return ptNew; + } + + public Quat leftDifference(Quat q2) { + //dq = q.leftDifference(qnext);//q.inv().mul(qnext); + Quat q2adjusted = (this.dot(q2) < 0 ? q2.negate() : q2); + return inv().mulQ(q2adjusted); + } + + public Quat rightDifference(Quat q2) { + //dq = qnext.rightDifference(q);//qnext.mul(q.inv()); + Quat q2adjusted = (this.dot(q2) < 0 ? q2.negate() : q2); + return mulQ(q2adjusted.inv()); + } + + /** + * + * Java axisAngle / plane / Point4f format + * all have the format {x y z w} + * so we go with that here as well + * + * @return "{q1 q2 q3 q0}" + */ + @Override + public String toString() { + return "{" + q1 + " " + q2 + " " + q3 + " " + q0 + "}"; + } + + /** + * + * @param data1 + * @param data2 + * @param nMax > 0 --> limit to this number + * @param isRelative + * + * @return pairwise array of data1 / data2 or data1 \ data2 + */ + public static Quat[] div(Quat[] data1, Quat[] data2, int nMax, boolean isRelative) { + int n; + if (data1 == null || data2 == null || (n = Math.min(data1.length, data2.length)) == 0) + return null; + if (nMax > 0 && n > nMax) + n = nMax; + Quat[] dqs = new Quat[n]; + for (int i = 0; i < n; i++) { + if (data1[i] == null || data2[i] == null) + return null; + dqs[i] = (isRelative ? data1[i].divLeft(data2[i]) : data1[i].div(data2[i])); + } + return dqs; + } + + public static Quat sphereMean(Quat[] data, float[] retStddev, float criterion) { + // Samuel R. Buss, Jay P. Fillmore: + // Spherical averages and applications to spherical splines and interpolation. + // ACM Trans. Graph. 20(2): 95-126 (2001) + if (data == null || data.length == 0) + return new Quat(); + if (retStddev == null) + retStddev = new float[1]; + if (data.length == 1) { + retStddev[0] = 0; + return newQ(data[0]); + } + float diff = Float.MAX_VALUE; + float lastStddev = Float.MAX_VALUE; + Quat qMean = simpleAverage(data); + int maxIter = 100; // typically goes about 5 iterations + int iter = 0; + while (diff > criterion && lastStddev != 0 && iter < maxIter) { + qMean = newMean(data, qMean); + retStddev[0] = stdDev(data, qMean); + diff = Math.abs(retStddev[0] - lastStddev); + lastStddev = retStddev[0]; + //Logger.info(++iter + " sphereMean " + qMean + " stddev=" + lastStddev + " diff=" + diff); + } + return qMean; + } + + /** + * Just a starting point. + * get average normal vector + * scale normal by average projection of vectors onto it + * create quaternion from this 3D projection + * + * @param ndata + * @return approximate average + */ + private static Quat simpleAverage(Quat[] ndata) { + V3 mean = V3.new3(0, 0, 1); + // using the directed normal ensures that the mean is + // continually added to and never subtracted from + V3 v = ndata[0].getNormal(); + mean.add(v); + for (int i = ndata.length; --i >= 0;) + mean.add(ndata[i].getNormalDirected(mean)); + mean.sub(v); + mean.normalize(); + float f = 0; + // the 3D projection of the quaternion is [sin(theta/2)]*n + // so dotted with the normalized mean gets us an approximate average for sin(theta/2) + for (int i = ndata.length; --i >= 0;) + f += Math.abs(ndata[i].get3dProjection(v).dot(mean)); + if (f != 0) + mean.scale(f / ndata.length); + // now convert f to the corresponding cosine instead of sine + f = (float) Math.sqrt(1 - mean.lengthSquared()); + if (Float.isNaN(f)) + f = 0; + return newP4(P4.new4(mean.x, mean.y, mean.z, f)); + } + + private static Quat newMean(Quat[] data, Quat mean) { + /* quaternion derivatives nicely take care of producing the necessary + * metric. Since dq gives us the normal with the smallest POSITIVE angle, + * we just scale by that -- using degrees. + * No special normalization is required. + * + * The key is that the mean has been set up already, and dq.getTheta() + * will always return a value between 0 and 180. True, for groupings + * where dq swings wildly -- 178, 182, 178, for example -- there will + * be problems, but the presumption here is that there is a REASONABLE + * set of data. Clearly there are spherical data sets that simply cannot + * be assigned a mean. (For example, where the three projected points + * are equally distant on the sphere. We just can't worry about those + * cases here. Rather, if there is any significance to the data, + * there will be clusters of projected points, and the analysis will + * be meaningful. + * + * Note that the hemisphere problem drops out because dq.getNormal() and + * dq.getTheta() will never return (n, 182 degrees) but will + * instead return (-n, 2 degrees). That's just what we want in that case. + * + * Note that the projection in this case is to 3D -- a set of vectors + * in space with lengths proportional to theta (not the sin(theta/2) + * that is associated with a quaternion map). + * + * This is officially an "exponential" or "hyperbolic" projection. + * + */ + V3 sum = new V3(); + V3 v; + Quat q, dq; + //System.out.println("newMean mean " + mean); + for (int i = data.length; --i >= 0;) { + q = data[i]; + dq = q.div(mean); + v = dq.getNormal(); + v.scale(dq.getTheta()); + sum.add(v); + } + sum.scale(1f/data.length); + Quat dqMean = newVA(sum, sum.length()); + //System.out.println("newMean dqMean " + dqMean + " " + dqMean.getNormal() + " " + dqMean.getTheta()); + return dqMean.mulQ(mean); + } + + /** + * @param data + * @param mean + * @return standard deviation in units of degrees + */ + private static float stdDev(Quat[] data, Quat mean) { + // the quaternion dot product gives q0 for dq (i.e. q / mean) + // that is, cos(theta/2) for theta between them + double sum2 = 0; + int n = data.length; + for (int i = n; --i >= 0;) { + float theta = data[i].div(mean).getTheta(); + sum2 += theta * theta; + } + return (float) Math.sqrt(sum2 / n); + } + + public float[] getEulerZYZ() { + // http://www.swarthmore.edu/NatSci/mzucker1/e27/diebel2006attitude.pdf + double rA, rB, rG; + if (q1 == 0 && q2 == 0) { + float theta = getTheta(); + // pure Z rotation - ambiguous + return new float[] { q3 < 0 ? -theta : theta , 0, 0 }; + } + rA = Math.atan2(2 * (q2 * q3 + q0 * q1), 2 * (-q1 * q3 + q0 * q2 )); + rB = Math.acos(q3 * q3 - q2 * q2 - q1 * q1 + q0 * q0); + rG = Math.atan2( 2 * (q2 * q3 - q0 * q1), 2 * (q0 * q2 + q1 * q3)); + return new float[] {(float) (rA / RAD_PER_DEG), (float) (rB / RAD_PER_DEG), (float) (rG / RAD_PER_DEG)}; + } + + public float[] getEulerZXZ() { + // NOT http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles + // http://www.swarthmore.edu/NatSci/mzucker1/e27/diebel2006attitude.pdf + double rA, rB, rG; + if (q1 == 0 && q2 == 0) { + float theta = getTheta(); + // pure Z rotation - ambiguous + return new float[] { q3 < 0 ? -theta : theta , 0, 0 }; + } + rA = Math.atan2(2 * (q1 * q3 - q0 * q2), 2 * (q0 * q1 + q2 * q3 )); + rB = Math.acos(q3 * q3 - q2 * q2 - q1 * q1 + q0 * q0); + rG = Math.atan2( 2 * (q1 * q3 + q0 * q2), 2 * (-q2 * q3 + q0 * q1)); + return new float[] {(float) (rA / RAD_PER_DEG), (float) (rB / RAD_PER_DEG), (float) (rG / RAD_PER_DEG)}; + } + +} diff --git a/src/javajs/util/Rdr.java b/src/javajs/util/Rdr.java new file mode 100644 index 0000000..c357581 --- /dev/null +++ b/src/javajs/util/Rdr.java @@ -0,0 +1,560 @@ +/* $RCSfile$ + * $Author: hansonr $ + * $Date: 2007-04-05 09:07:28 -0500 (Thu, 05 Apr 2007) $ + * $Revision: 7326 $ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2003-2005 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package javajs.util; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.StringReader; +import java.io.UnsupportedEncodingException; + +import java.util.Map; + +import javajs.api.Interface; + + +import javajs.J2SIgnoreImport; +import javajs.api.GenericCifDataParser; +import javajs.api.GenericLineReader; +import javajs.api.GenericZipTools; + +/** + * A general helper class for a variety of stream and reader functionality + * including: + * + * stream and byte magic-number decoding for PNG, PNGJ, ZIP, and GZIP streams + * + * various stream/reader methods, including UTF-encoded stream reading + * + * reflection-protected access to a CIF parser and ZIP tools + * + * + * + * + */ +@J2SIgnoreImport(BufferedWriter.class) +public class Rdr implements GenericLineReader { + + BufferedReader reader; + + public Rdr(BufferedReader reader) { + this.reader = reader; + } + + @Override + public String readNextLine() throws Exception { + return reader.readLine(); + } + + public static Map readCifData(GenericCifDataParser parser, BufferedReader br) { + return parser.set(null, br, false).getAllCifData(); + } + + + /////////// + + + /** + * + * Read a UTF-8 byte array fully, converting it to a String. + * Called by Jmol's XMLReaders + * + * @param bis + * @return a UTF-8 string + */ + public static String bytesToUTF8String(byte[] bytes) { + return streamToUTF8String(new BufferedInputStream(new ByteArrayInputStream(bytes))); + } + + /** + * + * Read a UTF-8 stream fully, converting it to a String. + * Called by Jmol's XMLReaders + * + * @param bis + * @return a UTF-8 string + */ + public static String streamToUTF8String(BufferedInputStream bis) { + String[] data = new String[1]; + try { + readAllAsString(getBufferedReader(bis, "UTF-8"), -1, true, data, 0); + } catch (IOException e) { + } + return data[0]; + } + + /** + * Read an input stream fully, saving a byte array, then + * return a buffered reader to those bytes converted to string form. + * + * @param bis + * @param charSet + * @return Reader + * @throws IOException + */ + public static BufferedReader getBufferedReader(BufferedInputStream bis, String charSet) + throws IOException { + // could also just make sure we have a buffered input stream here. + if (getUTFEncodingForStream(bis) == Encoding.NONE) + return new BufferedReader(new InputStreamReader(bis, (charSet == null ? "UTF-8" : charSet))); + byte[] bytes = getLimitedStreamBytes(bis, -1); + bis.close(); + return getBR(charSet == null ? fixUTF(bytes) : new String(bytes, charSet)); + } + + /** + * This method is specifically for strings that are marked for UTF 8 or 16. + * + * @param bytes + * @return + */ + public static String fixUTF(byte[] bytes) { + Encoding encoding = getUTFEncoding(bytes); + if (encoding != Encoding.NONE) + try { + String s = new String(bytes, encoding.name().replace('_', '-')); + switch (encoding) { + case UTF8: + case UTF_16BE: + case UTF_16LE: + // extra byte at beginning removed + s = s.substring(1); + break; + default: + break; + } + return s; + } catch (UnsupportedEncodingException e) { + System.out.println(e); + } + return new String(bytes); + } + + private static Encoding getUTFEncoding(byte[] bytes) { + if (bytes.length >= 3 && (bytes[0] & 0xFF) == 0xEF && (bytes[1] & 0xFF) == 0xBB && (bytes[2] & 0xFF) == 0xBF) + return Encoding.UTF8; + if (bytes.length >= 4 && (bytes[0] & 0xFF) == 0 && (bytes[1] & 0xFF) == 0 + && (bytes[2] & 0xFF) == 0xFE && (bytes[3] & 0xFF) == 0xFF) + return Encoding.UTF_32BE; + if (bytes.length >= 4 && (bytes[0] & 0xFF) == 0xFF && (bytes[1] & 0xFF) == 0xFE + && (bytes[2] & 0xFF) == 0 && (bytes[3] & 0xFF) == 0) + return Encoding.UTF_32LE; + if (bytes.length >= 2 && (bytes[0] & 0xFF) == 0xFF && (bytes[1] & 0xFF) == 0xFE) + return Encoding.UTF_16LE; + if (bytes.length >= 2 && (bytes[0] & 0xFF) == 0xFE && (bytes[1] & 0xFF) == 0xFF) + return Encoding.UTF_16BE; + return Encoding.NONE; + + } + + ////////// stream type checking ////////// + + + private static Encoding getUTFEncodingForStream(BufferedInputStream is) throws IOException { + /** + * @j2sNative + * + * is.resetStream(); + * + */ + { + } + byte[] abMagic = new byte[4]; + abMagic[3] = 1; + try{ + is.mark(5); + } catch (Exception e) { + return Encoding.NONE; + } + is.read(abMagic, 0, 4); + is.reset(); + return getUTFEncoding(abMagic); + } + + public static boolean isBase64(SB sb) { + return (sb.indexOf(";base64,") == 0); + } + + public static boolean isCompoundDocumentS(InputStream is) { + return isCompoundDocumentB(getMagic(is, 8)); + } + + public static boolean isCompoundDocumentB(byte[] bytes) { + return (bytes.length >= 8 && (bytes[0] & 0xFF) == 0xD0 + && (bytes[1] & 0xFF) == 0xCF && (bytes[2] & 0xFF) == 0x11 + && (bytes[3] & 0xFF) == 0xE0 && (bytes[4] & 0xFF) == 0xA1 + && (bytes[5] & 0xFF) == 0xB1 && (bytes[6] & 0xFF) == 0x1A + && (bytes[7] & 0xFF) == 0xE1); + } + + public static boolean isGzipS(InputStream is) { + return isGzipB(getMagic(is, 2)); + } + + public static boolean isGzipB(byte[] bytes) { + return (bytes != null && bytes.length >= 2 + && (bytes[0] & 0xFF) == 0x1F && (bytes[1] & 0xFF) == 0x8B); + } + + public static boolean isPickleS(InputStream is) { + return isPickleB(getMagic(is, 2)); + } + + public static boolean isPickleB(byte[] bytes) { + return (bytes != null && bytes.length >= 2 + && (bytes[0] & 0xFF) == 0x7D && (bytes[1] & 0xFF) == 0x71); + } + + public static boolean isMessagePackS(InputStream is) { + return isMessagePackB(getMagic(is, 1)); + } + + public static boolean isMessagePackB(byte[] bytes) { + // look for 'map' start + return (bytes != null && bytes.length >= 1 && (bytes[0] & 0xFF) == 0xDE); + } + + public static boolean isPngZipStream(InputStream is) { + return isPngZipB(getMagic(is, 55)); + } + + public static boolean isPngZipB(byte[] bytes) { + // \0PNGJ starting at byte 50 + return (bytes[50] == 0 && bytes[51] == 0x50 && bytes[52] == 0x4E && bytes[53] == 0x47 && bytes[54] == 0x4A); + } + + public static boolean isZipS(InputStream is) { + return isZipB(getMagic(is, 4)); + } + + public static boolean isZipB(byte[] bytes) { + return (bytes.length >= 4 + && bytes[0] == 0x50 //PK<03><04> + && bytes[1] == 0x4B + && bytes[2] == 0x03 + && bytes[3] == 0x04); + } + + public static byte[] getMagic(InputStream is, int n) { + byte[] abMagic = new byte[n]; + /** + * @j2sNative + * + * is.resetStream(); + * + */ + { + } + try { + is.mark(n + 1); + is.read(abMagic, 0, n); + } catch (IOException e) { + } + try { + is.reset(); + } catch (IOException e) { + } + return abMagic; + } + + public static String guessMimeTypeForBytes(byte[] bytes) { + // only options here are JPEG, PNG, GIF, and BMP + switch (bytes.length < 2 ? -1 : bytes[1]) { + case 0: + return "image/jpg"; // 0xFF 0x00 ... + case 0x49: + return "image/gif"; // GIF89a... + case 0x4D: + return "image/BMP"; // BM... + case 0x50: + return "image/png"; + default: + return "image/unknown"; + } + } + + + ////////// stream/byte methods /////////// + + public static BufferedInputStream getBIS(byte[] bytes) { + return new BufferedInputStream(new ByteArrayInputStream(bytes)); + } + + public static BufferedReader getBR(String string) { + return new BufferedReader(new StringReader(string)); + } + + /** + * Drill down into a GZIP stack until no more layers. + * @param jzt + * + * @param bis + * @return non-gzipped buffered input stream. + * + * @throws IOException + */ + public static BufferedInputStream getUnzippedInputStream(GenericZipTools jzt, BufferedInputStream bis) throws IOException { + while (isGzipS(bis)) + bis = new BufferedInputStream(jzt.newGZIPInputStream(bis)); + return bis; + } + + /** + * Allow for base64-encoding check. + * + * @param sb + * @return byte array + */ + public static byte[] getBytesFromSB(SB sb) { + return (isBase64(sb) ? Base64.decodeBase64(sb.substring(8)) : sb.toBytes(0, -1)); + } + + /** + * Read a an entire BufferedInputStream for its bytes, and + * either return them or leave them in the designated output channel. + * + * @param bis + * @param out a destination output channel, or null + * @return byte[] (if out is null) or a message indicating length (if not) + * + * @throws IOException + */ + public static Object getStreamAsBytes(BufferedInputStream bis, + OC out) throws IOException { + byte[] buf = new byte[1024]; + byte[] bytes = (out == null ? new byte[4096] : null); + int len = 0; + int totalLen = 0; + while ((len = bis.read(buf, 0, 1024)) > 0) { + totalLen += len; + if (out == null) { + if (totalLen >= bytes.length) + bytes = AU.ensureLengthByte(bytes, totalLen * 2); + System.arraycopy(buf, 0, bytes, totalLen - len, len); + } else { + out.write(buf, 0, len); + } + } + bis.close(); + if (out == null) { + return AU.arrayCopyByte(bytes, totalLen); + } + return totalLen + " bytes"; + } + + /** + * Read a possibly limited number of bytes (when n > 0) from a stream, + * leaving the stream open. + * + * @param is an input stream, not necessarily buffered. + * @param n the maximum number of bytes to read, or -1 for all + * @return the bytes read + * + * @throws IOException + */ + public static byte[] getLimitedStreamBytes(InputStream is, long n) + throws IOException { + + //Note: You cannot use InputStream.available() to reliably read + // zip data from the web. + + int buflen = (n > 0 && n < 1024 ? (int) n : 1024); + byte[] buf = new byte[buflen]; + byte[] bytes = new byte[n < 0 ? 4096 : (int) n]; + int len = 0; + int totalLen = 0; + if (n < 0) + n = Integer.MAX_VALUE; + while (totalLen < n && (len = is.read(buf, 0, buflen)) > 0) { + totalLen += len; + if (totalLen > bytes.length) + bytes = AU.ensureLengthByte(bytes, totalLen * 2); + System.arraycopy(buf, 0, bytes, totalLen - len, len); + if (n != Integer.MAX_VALUE && totalLen + buflen > bytes.length) + buflen = bytes.length - totalLen; + + } + if (totalLen == bytes.length) + return bytes; + buf = new byte[totalLen]; + System.arraycopy(bytes, 0, buf, 0, totalLen); + return buf; + } + + /** + * This method fills data[i] with string data from a file that may or may not + * be binary even though it is being read by a reader. It is meant to be used + * simple text-based files only. + * + * @param br + * @param nBytesMax + * @param allowBinary + * @param data + * @param i + * @return true if data[i] holds the data; false if data[i] holds an error message. + */ + public static boolean readAllAsString(BufferedReader br, int nBytesMax, boolean allowBinary, String[] data, int i) { + try { + SB sb = SB.newN(8192); + String line; + if (nBytesMax < 0) { + line = br.readLine(); + if (allowBinary || line != null && line.indexOf('\0') < 0 + && (line.length() != 4 || line.charAt(0) != 65533 + || line.indexOf("PNG") != 1)) { + sb.append(line).appendC('\n'); + while ((line = br.readLine()) != null) + sb.append(line).appendC('\n'); + } + } else { + int n = 0; + int len; + while (n < nBytesMax && (line = br.readLine()) != null) { + if (nBytesMax - n < (len = line.length()) + 1) + line = line.substring(0, nBytesMax - n - 1); + sb.append(line).appendC('\n'); + n += len + 1; + } + } + br.close(); + data[i] = sb.toString(); + return true; + } catch (Exception ioe) { + data[i] = ioe.toString(); + return false; + } + } + + + /////////// PNGJ support ///////////// + + + /** + * Look at byte 50 for "\0PNGJxxxxxxxxx+yyyyyyyyy" where xxxxxxxxx is a byte + * offset to the JMOL data and yyyyyyyyy is the length of the data. + * + * @param bis + * @return same stream or byte stream + */ + + /** + * Retrieve the two numbers in a PNG iTXt tag indicating the + * file pointer for the start of the ZIP data as well as its length. + * + * @param bis + * @param pt_count + */ + static void getPngZipPointAndCount(BufferedInputStream bis, int[] pt_count) { + bis.mark(75); + try { + byte[] data = getLimitedStreamBytes(bis, 74); + bis.reset(); + int pt = 0; + for (int i = 64, f = 1; --i > 54; f *= 10) + pt += (data[i] - '0') * f; + int n = 0; + for (int i = 74, f = 1; --i > 64; f *= 10) + n += (data[i] - '0') * f; + pt_count[0] = pt; + pt_count[1] = n; + } catch (Throwable e) { + pt_count[1] = 0; + } + } + + /** + * Either advance a PNGJ stream to its zip file data or pull out the ZIP data + * bytes and create a new stream for them from which a ZIP utility can start + * extracting files. + * + * @param bis + * @param asNewStream + * @return new buffered ByteArrayInputStream, possibly with no data if there is an error + */ + public static BufferedInputStream getPngZipStream(BufferedInputStream bis, boolean asNewStream) { + if (!isPngZipStream(bis)) + return bis; + byte[] data = new byte[0]; + bis.mark(75); + try { + int pt_count[] = new int[2]; + getPngZipPointAndCount(bis, pt_count); + if (pt_count[1] != 0) { + int pt = pt_count[0]; + while (pt > 0) + pt -= bis.skip(pt); + if (!asNewStream) + return bis; + data = getLimitedStreamBytes(bis, pt_count[1]); + } + } catch (Throwable e) { + } finally { + try { + if (asNewStream) + bis.close(); + } catch (Exception e) { + // ignore + } + } + return getBIS(data); + } + + /** We define a request for zip file extraction by vertical bar: + * zipName|interiorFileName. These may be nested if there is a + * zip file contained in a zip file. + * + * @param fileName + * @return filename trimmed of interior fileName + * + */ + public static String getZipRoot(String fileName) { + int pt = fileName.indexOf("|"); + return (pt < 0 ? fileName : fileName.substring(0, pt)); + } + + public static BufferedWriter getBufferedWriter(OutputStream os, String charSetName) { + OutputStreamWriter osw = (OutputStreamWriter) Interface.getInstanceWithParams("java.io.OutputStreamWriter", + new Class[] { java.io.OutputStream.class, String.class }, new Object[] {os, charSetName}); + /** + * @j2sNative + * return osw.getBufferedWriter(); + * + */ + { + return new BufferedWriter(osw); + } + } + + +} + diff --git a/src/javajs/util/SB.java b/src/javajs/util/SB.java new file mode 100644 index 0000000..578acf4 --- /dev/null +++ b/src/javajs/util/SB.java @@ -0,0 +1,357 @@ + +package javajs.util; + +import java.nio.charset.Charset; + +import javajs.J2SIgnoreImport; + +/** + * Interesting thing here is that JavaScript is 3x faster than Java in handling strings. + * + * Java StringBuilder is final, unfortunately. I guess they weren't thinking about Java2Script! + * + * The reason we have to do this that several overloaded append methods is WAY too expensive + * + */ + +@J2SIgnoreImport({java.lang.StringBuilder.class, java.nio.charset.Charset.class}) +public class SB { + + private java.lang.StringBuilder sb; + String s; // used by JavaScript only; no Java references + + //TODO: JS experiment with using array and .push() here + + public SB() { + /** + * @j2sNative + * + * this.s = ""; + * + */ + { + sb = new java.lang.StringBuilder(); + } + } + + public static SB newN(int n) { + /** + * @j2sNative + * return new javajs.util.SB(); + */ + { + // not perfect, because it requires defining sb twice. + // We can do better... + SB sb = new SB(); + sb.sb = new java.lang.StringBuilder(n); + return sb; + } + } + + public static SB newS(String s) { + /** + * @j2sNative + * + * var sb = new javajs.util.SB(); + * sb.s = s; + * return sb; + * + */ + { + SB sb = new SB(); + sb.sb = new java.lang.StringBuilder(s); + return sb; + } + } + + public SB append(String s) { + /** + * @j2sNative + * + * this.s += s + * + */ + { + sb.append(s); + } + return this; + } + + public SB appendC(char c) { + /** + * @j2sNative + * + * this.s += c; + */ + { + sb.append(c); + } + return this; + + } + + public SB appendI(int i) { + /** + * @j2sNative + * + * this.s += i + * + */ + { + sb.append(i); + } + return this; + } + + public SB appendB(boolean b) { + /** + * @j2sNative + * + * this.s += b + * + */ + { + sb.append(b); + } + return this; + } + + /** + * note that JavaScript could drop off the ".0" in "1.0" + * @param f + * @return this + */ + public SB appendF(float f) { + /** + * @j2sNative + * + * var sf = "" + f; + * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0) + * sf += ".0" ; + * this.s += sf; + * + */ + { + sb.append(f); + } + return this; + } + + public SB appendD(double d) { + /** + * @j2sNative + * + * var sf = "" + d; + * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0) + * sf += ".0" ; + * this.s += sf; + * + */ + { + sb.append(d); + } + return this; + } + + public SB appendSB(SB buf) { + /** + * @j2sNative + * + * this.s += buf.s; + * + */ + { + sb.append(buf.sb); + } + return this; + } + + public SB appendO(Object data) { + if (data != null) { + /** + * @j2sNative + * + * this.s += data.toString(); + * + */ + { + sb.append(data); + } + } + return this; + } + + public void appendCB(char[] cb, int off, int len) { + /** + * @j2sNative + * + * this.s += cb.slice(off,off+len).join(""); + * + */ + { + sb.append(cb, off, len); + } + } + + @Override + public String toString() { + /** + * @j2sNative + * + * return this.s; + * + */ + { + return sb.toString(); + } + } + + public int length() { + /** + * @j2sNative + * + * return this.s.length; + * + */ + { + return sb.length(); + } + } + + public int indexOf(String s) { + /** + * @j2sNative + * + * return this.s.indexOf(s); + * + */ + { + return sb.indexOf(s); + } + } + + public char charAt(int i) { + /** + * @j2sNative + * + * return this.s.charAt(i); + * + */ + { + return sb.charAt(i); + } + } + + public int charCodeAt(int i) { + /** + * @j2sNative + * + * return this.s.charCodeAt(i); + * + */ + { + return sb.codePointAt(i); + } + } + + public void setLength(int n) { + /** + * @j2sNative + * + * this.s = this.s.substring(0, n); + */ + { + sb.setLength(n); + } + } + + public int lastIndexOf(String s) { + /** + * @j2sNative + * + * return this.s.lastIndexOf(s); + */ + { + return sb.lastIndexOf(s); + } + } + + public int indexOf2(String s, int i) { + /** + * @j2sNative + * + * return this.s.indexOf(s, i); + */ + { + return sb.indexOf(s, i); + } + } + + public String substring(int i) { + /** + * @j2sNative + * + * return this.s.substring(i); + */ + { + return sb.substring(i); + } + } + + public String substring2(int i, int j) { + /** + * @j2sNative + * + * return this.s.substring(i, j); + */ + { + return sb.substring(i, j); + } + } + + /** + * simple byte conversion properly implementing UTF-8. + * Used for base64 conversion and allows for offset + * + * @param off + * @param len + * or -1 for full length (then off must = 0) + * @return byte[] + */ + public byte[] toBytes(int off, int len) { + if (len == 0) + return new byte[0]; + Charset cs; + /** + * + * just a string in JavaScript + * + * @j2sNative + * + * cs = "UTF-8"; + * + */ + { + cs = Charset.forName("UTF-8"); + } + return (len > 0 ? substring2(off, off + len) + : off == 0 ? toString() + : substring2(off, length() - off)).getBytes(cs); + } + + public void replace(int start, int end, String str) { + /** + * @j2sNative + * + * this.s = this.s.substring(0, start) + str + this.s.substring(end); + */ + { + sb.replace(start, end, str); + } + } + + public void insert(int offset, String str) { + replace(offset, offset, str); + } + +} diff --git a/src/javajs/util/StringDataReader.java b/src/javajs/util/StringDataReader.java new file mode 100644 index 0000000..e78a6f8 --- /dev/null +++ b/src/javajs/util/StringDataReader.java @@ -0,0 +1,51 @@ +/* $RCSfile$ + * $Author$ + * $Date$ + * $Revision$ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2011 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package javajs.util; + +import java.io.StringReader; + + + + + +public class StringDataReader extends DataReader { + + public StringDataReader() { + super(); + } + + public StringDataReader(String data) { + super(new StringReader(data)); + } + + @Override + public DataReader setData(Object data) { + return new StringDataReader((String) data); + } +} \ No newline at end of file diff --git a/src/javajs/util/T3.java b/src/javajs/util/T3.java new file mode 100644 index 0000000..bac7404 --- /dev/null +++ b/src/javajs/util/T3.java @@ -0,0 +1,333 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + +import javajs.api.JSONEncodable; + +/** + * A generic 3 element tuple that is represented by single precision floating + * point x,y and z coordinates. + * + * @version specification 1.1, implementation $Revision: 1.10 $, $Date: + * 2006/09/08 20:20:20 $ + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 + * for unique constructor and method names + * for the optimization of compiled JavaScript using Java2Script. + * + */ +public abstract class T3 implements JSONEncodable { + + public float x, y, z; + + /** + * Sets the value of this tuple to the specified xyz coordinates. + * + * @param x + * the x coordinate + * @param y + * the y coordinate + * @param z + * the z coordinate + */ + public final void set(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } + + /** + * Sets the value of this tuple from the 3 values specified in the array. + * + * @param t + * the array of length 3 containing xyz in order + */ + public final void setA(float t[]) { + // ArrayIndexOutOfBounds is thrown if t.length < 3 + x = t[0]; + y = t[1]; + z = t[2]; + } + + /** + * Sets the value of this tuple to the value of the Tuple3f argument. + * + * @param t1 + * the tuple to be copied + */ + public final void setT(T3 t1) { + x = t1.x; + y = t1.y; + z = t1.z; + } + + /** + * Sets the value of this tuple to the vector sum of tuples t1 and t2. + * + * @param t1 + * the first tuple + * @param t2 + * the second tuple + */ + public final void add2(T3 t1, T3 t2) { + x = t1.x + t2.x; + y = t1.y + t2.y; + z = t1.z + t2.z; + } + + /** + * Sets the value of this tuple to the vector sum of itself and tuple t1. + * + * @param t1 + * the other tuple + */ + public final void add(T3 t1) { + x += t1.x; + y += t1.y; + z += t1.z; + } + + /** + * Computes the square of the distance between this point and point p1. + * + * @param p1 + * the other point + * @return the square of distance between these two points as a float + */ + public final float distanceSquared(T3 p1) { + double dx = x - p1.x; + double dy = y - p1.y; + double dz = z - p1.z; + return (float) (dx * dx + dy * dy + dz * dz); + } + + /** + * Returns the distance between this point and point p1. + * + * @param p1 + * the other point + * @return the distance between these two points + */ + public final float distance(T3 p1) { + return (float) Math.sqrt(distanceSquared(p1)); + } + + /** + * Sets the value of this tuple to the vector difference of tuple t1 and t2 + * (this = t1 - t2). + * + * @param t1 + * the first tuple + * @param t2 + * the second tuple + */ + public final void sub2(T3 t1, T3 t2) { + x = t1.x - t2.x; + y = t1.y - t2.y; + z = t1.z - t2.z; + } + + /** + * Sets the value of this tuple to the vector difference of itself and tuple + * t1 (this = this - t1). + * + * @param t1 + * the other tuple + */ + public final void sub(T3 t1) { + x -= t1.x; + y -= t1.y; + z -= t1.z; + } + + /** + * Sets the value of this tuple to the scalar multiplication of itself. + * + * @param s + * the scalar value + */ + public final void scale(float s) { + x *= s; + y *= s; + z *= s; + } + + /** + * Add {a b c} + * + * @param a + * @param b + * @param c + */ + public final void add3(float a, float b, float c) { + x += a; + y += b; + z += c; + } + + + /** + * {x*p.x, y*p.y, z*p.z) used for three-way scaling + * + * @param p + */ + public final void scaleT(T3 p) { + x *= p.x; + y *= p.y; + z *= p.z; + } + + + /** + * Sets the value of this tuple to the scalar multiplication of tuple t1 and + * then adds tuple t2 (this = s*t1 + t2). + * + * @param s + * the scalar value + * @param t1 + * the tuple to be multipled + * @param t2 + * the tuple to be added + */ + public final void scaleAdd2(float s, T3 t1, T3 t2) { + x = s * t1.x + t2.x; + y = s * t1.y + t2.y; + z = s * t1.z + t2.z; + } + + + /** + * average of two tuples + * + * @param a + * @param b + */ + public void ave(T3 a, T3 b) { + x = (a.x + b.x) / 2f; + y = (a.y + b.y) / 2f; + z = (a.z + b.z) / 2f; + } + + /** + * Vector dot product. Was in Vector3f; more useful here, though. + * + * @param v + * the other vector + * @return this.dot.v + */ + public final float dot(T3 v) { + return x * v.x + y * v.y + z * v.z; + } + + /** + * Returns the squared length of this vector. + * Was in Vector3f; more useful here, though. + * + * @return the squared length of this vector + */ + public final float lengthSquared() { + return x * x + y * y + z * z; + } + + /** + * Returns the length of this vector. + * Was in Vector3f; more useful here, though. + * + * @return the length of this vector + */ + public final float length() { + return (float) Math.sqrt(lengthSquared()); + } + + /** + * Normalizes this vector in place. + * Was in Vector3f; more useful here, though. + */ + public final void normalize() { + double d = length(); + + // zero-div may occur. + x /= d; + y /= d; + z /= d; + } + + /** + * Sets this tuple to be the vector cross product of vectors v1 and v2. + * + * @param v1 + * the first vector + * @param v2 + * the second vector + */ + public final void cross(T3 v1, T3 v2) { + set(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y + - v1.y * v2.x); + } + + /** + * Returns a hash number based on the data values in this object. Two + * different Tuple3f objects with identical data values (ie, returns true for + * equals(Tuple3f) ) will return the same hash number. Two vectors with + * different data members may return the same hash value, although this is not + * likely. + */ + @Override + public int hashCode() { + long bits = 1L; + bits = 31L * bits + floatToIntBits(x); + bits = 31L * bits + floatToIntBits(y); + bits = 31L * bits + floatToIntBits(z); + return (int) (bits ^ (bits >> 32)); + } + + static int floatToIntBits(float x) { + return (x == 0 ? 0 : Float.floatToIntBits(x)); + } + + /** + * Returns true if all of the data members of Tuple3f t1 are equal to the + * corresponding data members in this + * + * @param t1 + * the vector with which the comparison is made. + */ + @Override + public boolean equals(Object t1) { + if (!(t1 instanceof T3)) + return false; + T3 t2 = (T3) t1; + return (x == t2.x && y == t2.y && z == t2.z); + } + + /** + * Returns a string that contains the values of this Tuple3f. The form is + * (x,y,z). + * + * @return the String representation + */ + @Override + public String toString() { + return "{" + x + ", " + y + ", " + z + "}"; + } + + @Override + public String toJSON() { + return "[" + x + "," + y + "," + z + "]"; + } +} diff --git a/src/javajs/util/T3d.java b/src/javajs/util/T3d.java new file mode 100644 index 0000000..549b3df --- /dev/null +++ b/src/javajs/util/T3d.java @@ -0,0 +1,224 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + +/** + * A generic 3 element tuple that is represented by double precision floating + * point x,y and z coordinates. + * + * @version specification 1.1, implementation $Revision: 1.9 $, $Date: + * 2006/07/28 17:01:32 $ + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 + * for unique constructor and method names + * for the optimization of compiled JavaScript using Java2Script. + * + */ +public abstract class T3d { + /** + * The x coordinate. + */ + public double x; + + /** + * The y coordinate. + */ + public double y; + + /** + * The z coordinate. + */ + public double z; + + /** + * Constructs and initializes a Tuple3d to (0,0,0). + */ + public T3d() { + } + + /** + * Sets the value of this tuple to the specified xyz coordinates. + * + * @param x + * the x coordinate + * @param y + * the y coordinate + * @param z + * the z coordinate + */ + public final void set(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + /** + * Sets the value of this tuple from the 3 values specified in the array. + * + * @param t + * the array of length 3 containing xyz in order + */ + public final void setA(double t[]) { + // ArrayIndexOutOfBounds is thrown if t.length < 3 + x = t[0]; + y = t[1]; + z = t[2]; + } + + /** + * Sets the value of this tuple to the value of the Tuple3d argument. + * + * @param t1 + * the tuple to be copied + */ + public final void setT(T3d t1) { + x = t1.x; + y = t1.y; + z = t1.z; + } + + /** + * Sets the value of this tuple to the vector sum of tuples t1 and t2. + * + * @param t1 + * the first tuple + * @param t2 + * the second tuple + */ + public final void add2(T3d t1, T3d t2) { + x = t1.x + t2.x; + y = t1.y + t2.y; + z = t1.z + t2.z; + } + + /** + * Sets the value of this tuple to the vector sum of itself and tuple t1. + * + * @param t1 + * the other tuple + */ + public final void add(T3d t1) { + x += t1.x; + y += t1.y; + z += t1.z; + } + + /** + * Sets the value of this tuple to the vector difference of tuple t1 and t2 + * (this = t1 - t2). + * + * @param t1 + * the first tuple + * @param t2 + * the second tuple + */ + public final void sub2(T3d t1, T3d t2) { + x = t1.x - t2.x; + y = t1.y - t2.y; + z = t1.z - t2.z; + } + + /** + * Sets the value of this tuple to the vector difference of itself and tuple + * t1 (this = this - t1). + * + * @param t1 + * the other tuple + */ + public final void sub(T3d t1) { + x -= t1.x; + y -= t1.y; + z -= t1.z; + } + + /** + * Sets the value of this tuple to the scalar multiplication of itself. + * + * @param s + * the scalar value + */ + public final void scale(double s) { + x *= s; + y *= s; + z *= s; + } + + /** + * Sets the value of this tuple to the scalar multiplication of tuple t1 and + * then adds tuple t2 (this = s*t1 + t2). + * + * @param s + * the scalar value + * @param t1 + * the tuple to be multipled + * @param t2 + * the tuple to be added + */ + public final void scaleAdd(double s, T3d t1, T3d t2) { + x = s * t1.x + t2.x; + y = s * t1.y + t2.y; + z = s * t1.z + t2.z; + } + + /** + * Returns a hash number based on the data values in this object. Two + * different Tuple3d objects with identical data values (ie, returns true for + * equals(Tuple3d) ) will return the same hash number. Two vectors with + * different data members may return the same hash value, although this is not + * likely. + */ + @Override + public int hashCode() { + long xbits = doubleToLongBits0(x); + long ybits = doubleToLongBits0(y); + long zbits = doubleToLongBits0(z); + return (int) (xbits ^ (xbits >> 32) ^ ybits ^ (ybits >> 32) ^ zbits ^ (zbits >> 32)); + } + + static long doubleToLongBits0(double d) { + // Check for +0 or -0 + return (d == 0 ? 0 : Double.doubleToLongBits(d)); + } + + /** + * Returns true if all of the data members of Tuple3d t1 are equal to the + * corresponding data members in this + * + * @param t1 + * the vector with which the comparison is made. + */ + @Override + public boolean equals(Object t1) { + if (!(t1 instanceof T3d)) + return false; + T3d t2 = (T3d) t1; + return (this.x == t2.x && this.y == t2.y && this.z == t2.z); + } + + /** + * Returns a string that contains the values of this Tuple3d. The form is + * (x,y,z). + * + * @return the String representation + */ + @Override + public String toString() { + return "{" + x + ", " + y + ", " + z + "}"; + } + +} diff --git a/src/javajs/util/T3i.java b/src/javajs/util/T3i.java new file mode 100644 index 0000000..fc9fd96 --- /dev/null +++ b/src/javajs/util/T3i.java @@ -0,0 +1,150 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + +/** + * A 3-element tuple represented by signed integer x,y,z coordinates. + * + * @since Java 3D 1.2 + * @version specification 1.2, implementation $Revision: 1.9 $, $Date: + * 2006/07/28 17:01:32 $ + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 for unique + * constructor and method names for the optimization of compiled + * JavaScript using Java2Script. + * + */ +public abstract class T3i { + + /** + * The x coordinate. + */ + public int x; + + /** + * The y coordinate. + */ + public int y; + + /** + * The z coordinate. + */ + public int z; + + /** + * Constructs and initializes a Tuple3i to (0,0,0). + */ + public T3i() { + } + + /** + * Sets the value of this tuple to to the specified x, y, and z coordinates. + * + * @param x + * the x coordinate. + * @param y + * the y coordinate. + * @param z + * the z coordinate. + */ + public final void set(int x, int y, int z) { + this.x = x; + this.y = y; + this.z = z; + } + + /** + * Sets the value of this tuple to the value of tuple t1. + * + * @param t1 + * the tuple to be copied. + */ + public final void setT(T3i t1) { + x = t1.x; + y = t1.y; + z = t1.z; + } + + /** + * Sets the value of this tuple to the sum of itself and t1. + * + * @param t + * is the other tuple + */ + public final void add(T3i t) { + x += t.x; + y += t.y; + z += t.z; + } + + /** + * Sets the value of this tuple to the scalar multiplication of tuple t1 plus + * tuple t2 (this = s*t1 + t2). + * + * @param s + * the scalar value + * @param t1 + * the tuple to be multipled + * @param t2 + * the tuple to be added + */ + public final void scaleAdd(int s, T3i t1, T3i t2) { + x = s * t1.x + t2.x; + y = s * t1.y + t2.y; + z = s * t1.z + t2.z; + } + + /** + * Returns a hash number based on the data values in this object. Two + * different Tuple3i objects with identical data values (ie, returns true for + * equals(Tuple3i) ) will return the same hash number. Two vectors with + * different data members may return the same hash value, although this is not + * likely. + */ + @Override + public int hashCode() { + return x ^ y ^ z; + } + + /** + * Returns true if the Object o is of type Tuple3i and all of the data members + * of t are equal to the corresponding data members in this Tuple3i. + * + * @param o + * the object with which the comparison is made. + */ + @Override + public boolean equals(Object o) { + if (!(o instanceof T3i)) + return false; + T3i t = (T3i) o; + return (this.x == t.x && this.y == t.y && this.z == t.z); + } + + /** + * Returns a string that contains the values of this Tuple3i. The form is + * (x,y,z). + * + * @return the String representation + */ + @Override + public String toString() { + return "(" + x + ", " + y + ", " + z + ")"; + } + +} diff --git a/src/javajs/util/T4.java b/src/javajs/util/T4.java new file mode 100644 index 0000000..535dd08 --- /dev/null +++ b/src/javajs/util/T4.java @@ -0,0 +1,113 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + +/** + * A generic 4 element tuple that is represented by single precision floating + * point x,y,z and w coordinates. + * + * @version specification 1.1, implementation $Revision: 1.9 $, $Date: + * 2006/07/28 17:01:32 $ + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 + * for unique constructor and method names + * for the optimization of compiled JavaScript using Java2Script. + * + */ +public abstract class T4 extends T3 { + + /** + * The w coordinate. + */ + public float w; + + /** + * Sets the value of this tuple to the specified xyzw coordinates. + * + * @param x + * the x coordinate + * @param y + * the y coordinate + * @param z + * the z coordinate + * @param w + * the w coordinate + */ + public final void set4(float x, float y, float z, float w) { + this.x = x; + this.y = y; + this.z = z; + this.w = w; + } + + /** + * Sets the value of this tuple to the scalar multiplication of itself. + * + * @param s + * the scalar value + */ + public final void scale4(float s) { + scale(s); + w *= s; + } + + /** + * Returns a hash number based on the data values in this object. Two + * different Tuple4f objects with identical data values (ie, returns true for + * equals(Tuple4f) ) will return the same hash number. Two vectors with + * different data members may return the same hash value, although this is not + * likely. + */ + @Override + public int hashCode() { + return T3.floatToIntBits(x) ^ T3.floatToIntBits(y) + ^ T3.floatToIntBits(z) ^ T3.floatToIntBits(w); + } + + /** + * Returns true if all of the data members of Object are equal to the + * corresponding data members in this + * + * @param o + * the vector with which the comparison is made. + */ + @Override + public boolean equals(Object o) { + if (!(o instanceof T4)) + return false; + T4 t = (T4) o; + return (this.x == t.x && this.y == t.y && this.z == t.z && this.w == t.w); + } + + /** + * Returns a string that contains the values of this Tuple4f. The form is + * (x,y,z,w). + * + * @return the String representation + */ + @Override + public String toString() { + return "(" + x + ", " + y + ", " + z + ", " + w + ")"; + } + + @Override + public String toJSON() { + return "[" + x + ", " + y + ", " + z + ", " + w + "]"; + } + +} diff --git a/src/javajs/util/V3.java b/src/javajs/util/V3.java new file mode 100644 index 0000000..edc8451 --- /dev/null +++ b/src/javajs/util/V3.java @@ -0,0 +1,72 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. + */ +package javajs.util; + + +/** + * A 3-element vector that is represented by single precision floating point + * x,y,z coordinates. If this value represents a normal, then it should be + * normalized. + * + * @version specification 1.1, implementation $Revision: 1.10 $, $Date: + * 2006/10/03 19:52:30 $ + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 + * for unique constructor and method names + * for the optimization of compiled JavaScript using Java2Script. + * + */ +public class V3 extends T3 { + + public static V3 newV(T3 t) { + return V3.new3(t.x, t.y, t.z); + } + + public static V3 newVsub(T3 t1, T3 t2) { + return V3.new3(t1.x - t2.x, t1.y - t2.y,t1.z - t2.z); + } + + public static V3 new3(float x, float y, float z) { + V3 v = new V3(); + v.x = x; + v.y = y; + v.z = z; + return v; + } + + /** + * Returns the angle in radians between this vector and the vector parameter; + * the return value is constrained to the range [0,PI]. + * + * @param v1 + * the other vector + * @return the angle in radians in the range [0,PI] + */ + public final float angle(V3 v1) { + // return (double)Math.acos(dot(v1)/v1.length()/v.length()); + // Numerically, near 0 and PI are very bad condition for acos. + // In 3-space, |atan2(sin,cos)| is much stable. + + double xx = y * v1.z - z * v1.y; + double yy = z * v1.x - x * v1.z; + double zz = x * v1.y - y * v1.x; + double cross = Math.sqrt(xx * xx + yy * yy + zz * zz); + + return (float) Math.abs(Math.atan2(cross, dot(v1))); + } +} diff --git a/src/javajs/util/V3d.java b/src/javajs/util/V3d.java new file mode 100644 index 0000000..78b5a82 --- /dev/null +++ b/src/javajs/util/V3d.java @@ -0,0 +1,94 @@ +/* + Copyright (C) 1997,1998,1999 + Kenji Hiranabe, Eiwa System Management, Inc. + + This program is free software. + Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), + conforming to the Java(TM) 3D API specification by Sun Microsystems. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appear in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. + makes no representations about the suitability of this software for any + purpose. It is provided "AS IS" with NO WARRANTY. +*/ +package javajs.util; + + + + +/** + * A 3 element vector that is represented by double precision floating point + * x,y,z coordinates. If this value represents a normal, then it should be + * normalized. + * + * @version specification 1.1, implementation $Revision: 1.9 $, $Date: + * 2006/07/28 17:01:32 $ + * @author Kenji hiranabe + * + * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 + * for unique constructor and method names + * for the optimization of compiled JavaScript using Java2Script. + * + */ +public class V3d extends T3d { + + /** + * Sets this vector to be the vector cross product of vectors v1 and v2. + * + * @param v1 + * the first vector + * @param v2 + * the second vector + */ + public final void cross(V3d v1, V3d v2) { + // store on stack once for aliasing-safty + // i.e. safe when a.cross(a, b) + set(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y + - v1.y * v2.x); + } + + /** + * Normalizes this vector in place. + */ + public final void normalize() { + double d = length(); + + // zero-div may occur. + x /= d; + y /= d; + z /= d; + } + + /** + * Computes the dot product of the this vector and vector v. + * + * @param v + * the other vector + * @return this.dot.v + */ + public final double dot(V3d v) { + return x * v.x + y * v.y + z * v.z; + } + + /** + * Returns the squared length of this vector. + * + * @return the squared length of this vector + */ + public final double lengthSquared() { + return x * x + y * y + z * z; + } + + /** + * Returns the length of this vector. + * + * @return the length of this vector + */ + public final double length() { + return Math.sqrt(lengthSquared()); + } + +} diff --git a/src/javajs/util/XmlUtil.java b/src/javajs/util/XmlUtil.java new file mode 100644 index 0000000..3e30e4b --- /dev/null +++ b/src/javajs/util/XmlUtil.java @@ -0,0 +1,171 @@ +/* $RCSfile$ + * $Author$ + * $Date$ + * $Revision$ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2006 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package javajs.util; + +/** + * A very simplistic XML generator + */ + +public class XmlUtil { + + public XmlUtil() { + // Jmol's PropertyManager and JvxlCoder classes use reflection + } + public static void openDocument(SB data) { + data.append("\n"); + } + + public static void openTag(SB sb, String name) { + sb.append("<").append(name).append(">\n"); + } + + public static void openTagAttr(SB sb, String name, Object[] attributes) { + appendTagAll(sb, name, attributes, null, false, false); + sb.append("\n"); + } + + public static void closeTag(SB sb, String name) { + sb.append("\n"); + } + + public static void appendTagAll(SB sb, String name, + Object[] attributes, Object data, + boolean isCdata, boolean doClose) { + String closer = ">"; + if (name.endsWith("/")){ + name = name.substring(0, name.length() - 1); + if (data == null) { + closer = "/>\n"; + doClose = false; + } + } + sb.append("<").append(name); + if (attributes != null) + for (int i = 0; i < attributes.length; i++) { + Object o = attributes[i]; + if (o == null) + continue; + if (o instanceof Object[]) + for (int j = 0; j < ((Object[]) o).length; j+= 2) + appendAttrib(sb, ((Object[]) o)[j], ((Object[]) o)[j + 1]); + else + appendAttrib(sb, o, attributes[++i]); + } + sb.append(closer); + if (data != null) { + if (isCdata) + data = wrapCdata(data); + sb.appendO(data); + } + if (doClose) + closeTag(sb, name); + } + + /** + * wrap the string as character data, with replacements for [ noted + * as a list starting with * after the CDATA termination + * + * @param data + * @return wrapped text + */ + public static String wrapCdata(Object data) { + String s = "" + data; + return (s.indexOf("&") < 0 && s.indexOf("<") < 0 ? (s.startsWith("\n") ? "" : "\n") + s + : "", "]]]]>") + "]]>"); + } + + /** + * standard data" + * + * @param sb + * @param name + * @param attributes + * @param data + */ + public static void appendTagObj(SB sb, String name, + Object[] attributes, Object data) { + appendTagAll(sb, name, attributes, data, false, true); + } + + /** + * standard data" + * standard " + * + * @param sb + * @param name + * @param data + */ + public static void appendTag(SB sb, String name, Object data) { + if (data instanceof Object[]) + appendTagAll(sb, name, (Object[]) data, null, false, true); + else + appendTagAll(sb, name, null, data, false, true); + } + + /** + * " + * + * will convert ]]> to ]] > + * + * @param sb + * @param name + * @param attributes + * @param data + */ + public static void appendCdata(SB sb, String name, + Object[] attributes, String data) { + appendTagAll(sb, name, attributes, data, true, true); + } + + /** + * + * @param sb + * @param name + * @param value + */ + public static void appendAttrib(SB sb, Object name, Object value) { + if (value == null) + return; + + // note: <&" are disallowed but not checked for here + + sb.append(" ").appendO(name).append("=\"").appendO(value).append("\""); + } + +// /** +// * @param s +// * @return unwrapped text +// */ +// public static String unwrapCdata(String s) { +// return (s.startsWith("") ? +// PT.rep(s.substring(9, s.length()-3),"]]]]>", "]]>") : s); +// } +// + +} diff --git a/src/javajs/util/ZipData.java b/src/javajs/util/ZipData.java new file mode 100644 index 0000000..03a903b --- /dev/null +++ b/src/javajs/util/ZipData.java @@ -0,0 +1,79 @@ +/* $RCSfile$ + * $Author$ + * $Date$ + * $Revision$ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2006 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package javajs.util; + +import java.io.BufferedInputStream; + +import javajs.api.GenericZipTools; + + + + +public class ZipData { + boolean isEnabled = true; + byte[] buf; + int pt; + int nBytes; + + public ZipData(int nBytes) { + this.nBytes = nBytes; + } + + public int addBytes(byte[] byteBuf, int nSectorBytes, int nBytesRemaining) { + if (pt == 0) { + if (!Rdr.isGzipB(byteBuf)) { + isEnabled = false; + return -1; + } + buf = new byte[nBytesRemaining]; + } + int nToAdd = Math.min(nSectorBytes, nBytesRemaining); + System.arraycopy(byteBuf, 0, buf, pt, nToAdd); + pt += nToAdd; + return nBytesRemaining - nToAdd; + } + + public void addTo(GenericZipTools jzt, SB data) { + data.append(getGzippedBytesAsString(jzt, buf)); + } + + static String getGzippedBytesAsString(GenericZipTools jzt, byte[] bytes) { + try { + BufferedInputStream bis = jzt.getUnGzippedInputStream(bytes); + String s = ZipTools.getStreamAsString(bis); + bis.close(); + return s; + } catch (Exception e) { + return ""; + } + } + + +} + diff --git a/src/javajs/util/ZipTools.java b/src/javajs/util/ZipTools.java new file mode 100644 index 0000000..6714236 --- /dev/null +++ b/src/javajs/util/ZipTools.java @@ -0,0 +1,456 @@ +/* $RCSfile$ + * $Author$ + * $Date$ + * $Revision$ + * + * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017 + * for use in SwingJS via transpilation into JavaScript using Java2Script. + * + * Copyright (C) 2006 The Jmol Development Team + * + * Contact: jmol-developers@lists.sf.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package javajs.util; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javajs.J2SIgnoreImport; +import javajs.api.GenericZipInputStream; +import javajs.api.GenericZipTools; +import javajs.api.ZInputStream; + +import java.util.Map; +import java.util.zip.CRC32; +import java.util.zip.GZIPInputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; + + +/** + * Note the JSmol/HTML5 must use its own version of java.util.zip.ZipOutputStream. + * + */ +@J2SIgnoreImport({ java.util.zip.ZipOutputStream.class }) +public class ZipTools implements GenericZipTools { + + public ZipTools() { + // for reflection + } + + @Override + public ZInputStream newZipInputStream(InputStream is) { + return newZIS(is); + } + + private static ZInputStream newZIS(InputStream is) { + return (is instanceof ZInputStream ? (ZInputStream) is + : is instanceof BufferedInputStream ? new GenericZipInputStream(is) + : new GenericZipInputStream(new BufferedInputStream(is))); + } + + /** + * reads a ZIP file and saves all data in a Hashtable so that the files may be + * organized later in a different order. Also adds a #Directory_Listing entry. + * + * Files are bracketed by BEGIN Directory Entry and END Directory Entry lines, + * similar to CompoundDocument.getAllData. + * + * @param is + * @param subfileList + * @param name0 + * prefix for entry listing + * @param binaryFileList + * |-separated list of files that should be saved as xx xx xx hex byte + * strings. The directory listing is appended with ":asBinaryString" + * @param fileData + */ + @Override + public void getAllZipData(InputStream is, String[] subfileList, + String name0, String binaryFileList, String exclude, + Map fileData) { + ZipInputStream zis = (ZipInputStream) newZIS(is); + ZipEntry ze; + SB listing = new SB(); + binaryFileList = "|" + binaryFileList + "|"; + String prefix = PT.join(subfileList, '/', 1); + String prefixd = null; + if (prefix != null) { + prefixd = prefix.substring(0, prefix.indexOf("/") + 1); + if (prefixd.length() == 0) + prefixd = null; + } + try { + while ((ze = zis.getNextEntry()) != null) { + String name = ze.getName(); + if (prefix != null && prefixd != null + && !(name.equals(prefix) || name.startsWith(prefixd)) + || exclude != null && name.contains(exclude)) + continue; + //System.out.println("ziputil: " + name); + listing.append(name).appendC('\n'); + String sname = "|" + name.substring(name.lastIndexOf("/") + 1) + "|"; + boolean asBinaryString = (binaryFileList.indexOf(sname) >= 0); + byte[] bytes = Rdr.getLimitedStreamBytes(zis, ze.getSize()); + String str; + if (asBinaryString) { + str = getBinaryStringForBytes(bytes); + name += ":asBinaryString"; + } else { + str = Rdr.fixUTF(bytes); + } + str = "BEGIN Directory Entry " + name + "\n" + str + + "\nEND Directory Entry " + name + "\n"; + String key = name0 + "|" + name; + fileData.put(key, str); + } + } catch (Exception e) { + } + fileData.put("#Directory_Listing", listing.toString()); + } + + private String getBinaryStringForBytes(byte[] bytes) { + SB ret = new SB(); + for (int i = 0; i < bytes.length; i++) + ret.append(Integer.toHexString(bytes[i] & 0xFF)).appendC(' '); + return ret.toString(); + } + + /** + * iteratively drills into zip files of zip files to extract file content or + * zip file directory. Also works with JAR files. + * + * Does not return "__MACOS" paths + * + * @param bis + * @param list + * @param listPtr + * @param asBufferedInputStream + * for Pmesh + * @return directory listing or subfile contents + */ + @Override + public Object getZipFileDirectory(BufferedInputStream bis, String[] list, + int listPtr, boolean asBufferedInputStream) { + SB ret; + if (list == null || listPtr >= list.length) + return getZipDirectoryAsStringAndClose(bis); + bis = Rdr.getPngZipStream(bis, true); + String fileName = list[listPtr]; + ZipInputStream zis = new ZipInputStream(bis); + ZipEntry ze; + //System.out.println("fname=" + fileName); + try { + boolean isAll = (fileName.equals(".")); + if (isAll || fileName.lastIndexOf("/") == fileName.length() - 1) { + ret = new SB(); + while ((ze = zis.getNextEntry()) != null) { + String name = ze.getName(); + if (isAll || name.startsWith(fileName)) + ret.append(name).appendC('\n'); + } + String str = ret.toString(); + return (asBufferedInputStream ? Rdr.getBIS(str.getBytes()) : str); + } + int pt = fileName.indexOf(":asBinaryString"); + boolean asBinaryString = (pt > 0); + if (asBinaryString) + fileName = fileName.substring(0, pt); + fileName = fileName.replace('\\', '/'); + while ((ze = zis.getNextEntry()) != null + && !fileName.equals(ze.getName())) { + } + byte[] bytes = (ze == null ? null : Rdr.getLimitedStreamBytes(zis, + ze.getSize())); + ze = null; + zis.close(); + if (bytes == null) + return ""; + if (Rdr.isZipB(bytes) || Rdr.isPngZipB(bytes)) + return getZipFileDirectory(Rdr.getBIS(bytes), list, ++listPtr, + asBufferedInputStream); + if (asBufferedInputStream) + return Rdr.getBIS(bytes); + if (asBinaryString) { + ret = new SB(); + for (int i = 0; i < bytes.length; i++) + ret.append(Integer.toHexString(bytes[i] & 0xFF)).appendC(' '); + return ret.toString(); + } + if (Rdr.isGzipB(bytes)) + bytes = Rdr.getLimitedStreamBytes(getUnGzippedInputStream(bytes), -1); + return Rdr.fixUTF(bytes); + } catch (Exception e) { + return ""; + } + } + + @Override + public byte[] getZipFileContentsAsBytes(BufferedInputStream bis, + String[] list, int listPtr) { + byte[] ret = new byte[0]; + String fileName = list[listPtr]; + if (fileName.lastIndexOf("/") == fileName.length() - 1) + return ret; + try { + bis = Rdr.getPngZipStream(bis, true); + ZipInputStream zis = new ZipInputStream(bis); + ZipEntry ze; + while ((ze = zis.getNextEntry()) != null) { + if (!fileName.equals(ze.getName())) + continue; + byte[] bytes = Rdr.getLimitedStreamBytes(zis, ze.getSize()); + return ((Rdr.isZipB(bytes) || Rdr.isPngZipB(bytes)) && ++listPtr < list.length ? getZipFileContentsAsBytes( + Rdr.getBIS(bytes), list, listPtr) : bytes); + } + } catch (Exception e) { + } + return ret; + } + + @Override + public String getZipDirectoryAsStringAndClose(BufferedInputStream bis) { + SB sb = new SB(); + String[] s = new String[0]; + try { + s = getZipDirectoryOrErrorAndClose(bis, null); + bis.close(); + } catch (Exception e) { + System.out.println(e.toString()); + } + for (int i = 0; i < s.length; i++) + sb.append(s[i]).appendC('\n'); + return sb.toString(); + } + + @Override + public String[] getZipDirectoryAndClose(BufferedInputStream bis, + String manifestID) { + String[] s = new String[0]; + try { + s = getZipDirectoryOrErrorAndClose(bis, manifestID); + bis.close(); + } catch (Exception e) { + System.out.println(e.toString()); + } + return s; + } + + private String[] getZipDirectoryOrErrorAndClose(BufferedInputStream bis, + String manifestID) + throws IOException { + bis = Rdr.getPngZipStream(bis, true); + Lst v = new Lst(); + ZipInputStream zis = new ZipInputStream(bis); + ZipEntry ze; + String manifest = null; + while ((ze = zis.getNextEntry()) != null) { + String fileName = ze.getName(); + if (manifestID != null && fileName.startsWith(manifestID)) + manifest = getStreamAsString(zis); + else if (!fileName.startsWith("__MACOS")) // resource fork not nec. + v.addLast(fileName); + } + zis.close(); + if (manifestID != null) + v.add(0, manifest == null ? "" : manifest + "\n############\n"); + return v.toArray(new String[v.size()]); + } + + public static String getStreamAsString(InputStream is) throws IOException { + return Rdr.fixUTF(Rdr.getLimitedStreamBytes(is, -1)); + } + + @Override + public InputStream newGZIPInputStream(InputStream is) throws IOException { + return new BufferedInputStream(new GZIPInputStream(is, 512)); + } + + @Override + public BufferedInputStream getUnGzippedInputStream(byte[] bytes) { + try { + return Rdr.getUnzippedInputStream(this, Rdr.getBIS(bytes)); + } catch (Exception e) { + return null; + } + } + + @Override + public void addZipEntry(Object zos, String fileName) throws IOException { + ((ZipOutputStream) zos).putNextEntry(new ZipEntry(fileName)); + } + + @Override + public void closeZipEntry(Object zos) throws IOException { + ((ZipOutputStream) zos).closeEntry(); + } + + @Override + public Object getZipOutputStream(Object bos) { + /** + * @j2sNative + * + * return javajs.api.Interface.getInterface( + * "java.util.zip.ZipOutputStream").setZOS(bos); + * + */ + { + return new ZipOutputStream((OutputStream) bos); + } + } + + @Override + public int getCrcValue(byte[] bytes) { + CRC32 crc = new CRC32(); + crc.update(bytes, 0, bytes.length); + return (int) crc.getValue(); + } + + @Override + public void readFileAsMap(BufferedInputStream bis, Map bdata, String name) { + readFileAsMapStatic(bis, bdata, name); + } + + public static void readFileAsMapStatic(BufferedInputStream bis, + Map bdata, String name) { + int pt = (name == null ? -1 : name.indexOf("|")); + name = (pt >= 0 ? name.substring(pt + 1) : null); + try { + if (Rdr.isPngZipStream(bis)) { + boolean isImage = "_IMAGE_".equals(name); + if (name == null || isImage) + bdata.put((isImage ? "_DATA_" : "_IMAGE_"), new BArray(getPngImageBytes(bis))); + if (!isImage) + cacheZipContentsStatic(bis, name, bdata, true); + } else if (Rdr.isZipS(bis)) { + cacheZipContentsStatic(bis, name, bdata, true); + } else if (name == null){ + bdata.put("_DATA_", new BArray(Rdr.getLimitedStreamBytes(bis, -1))); + } else { + throw new IOException("ZIP file " + name + " not found"); + } + bdata.put("$_BINARY_$", Boolean.TRUE); + } catch (IOException e) { + bdata.clear(); + bdata.put("_ERROR_", e.getMessage()); + } + } + + @Override + public String cacheZipContents(BufferedInputStream bis, + String fileName, + Map cache, + boolean asByteArray) { + return cacheZipContentsStatic(bis, fileName, cache, asByteArray); + } + + /** + * + * @param bis + * @param fileName may end with "/" for a prefix or contain "|xxxx.xxx" for a specific file or be null + * @param cache + * @param asByteArray + * @return + */ + public static String cacheZipContentsStatic(BufferedInputStream bis, + String fileName, Map cache, boolean asByteArray) { + ZipInputStream zis = (ZipInputStream) newZIS(bis); + ZipEntry ze; + SB listing = new SB(); + long n = 0; + boolean isPath = (fileName != null && fileName.endsWith("/")); + boolean oneFile = (asByteArray && !isPath && fileName != null); + int pt = (oneFile ? fileName.indexOf("|") : -1); + String file0 = (pt >= 0 ? fileName : null); + if (pt >= 0) + fileName = fileName.substring(0, pt); + String prefix = (fileName == null ? "" : isPath ? fileName : fileName + "|"); + try { + while ((ze = zis.getNextEntry()) != null) { + String name = ze.getName(); + if (fileName != null) { + if (oneFile) { + if (!name.equalsIgnoreCase(fileName)) + continue; + } else { + listing.append(name).appendC('\n'); + } + } + long nBytes = ze.getSize(); + byte[] bytes = Rdr.getLimitedStreamBytes(zis, nBytes); + if (file0 != null) { + readFileAsMapStatic(Rdr.getBIS(bytes), cache, file0); + return null; + } + n += bytes.length; + Object o = (asByteArray ? new BArray(bytes) : bytes); + cache.put((oneFile ? "_DATA_" : prefix + name), o); + if (oneFile) + break; + } + zis.close(); + } catch (Exception e) { + try { + zis.close(); + } catch (IOException e1) { + } + return null; + } + if (n == 0 || fileName == null) + return null; + System.out.println("ZipTools cached " + n + " bytes from " + fileName); + return listing.toString(); + } + + private static byte[] getPngImageBytes(BufferedInputStream bis) { + try { + if (Rdr.isPngZipStream(bis)) { + int pt_count[] = new int[2]; + Rdr.getPngZipPointAndCount(bis, pt_count); + if (pt_count[1] != 0) + return deActivatePngZipB(Rdr.getLimitedStreamBytes(bis, pt_count[0])); + } + return Rdr.getLimitedStreamBytes(bis, -1); + } catch (IOException e) { + return null; + } + } + + /** + * Once a PNGJ image has been extracted, we want to red-line its + * iTXt "Jmol Type PNGJ" tag, since it is no longer associated with + * ZIP data. + * + * @param bytes + * @return disfigured bytes + * + */ + private static byte[] deActivatePngZipB(byte[] bytes) { + // \0PNGJ starting at byte 50 changed to \0 NGJ + if (Rdr.isPngZipB(bytes)) + bytes[51] = 32; + return bytes; + } + + + +} -- 1.7.10.2