From 992dcea3e94eead998942127a137cccd0882fe97 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 19 Nov 2010 14:12:37 +0100 Subject: Buffers float/double conversion: Add arg for given destination buffer; Add back conversion; Add unit test for arrays --- src/java/com/jogamp/common/nio/Buffers.java | 93 ++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 7 deletions(-) (limited to 'src/java/com/jogamp/common/nio') diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java index 7e5f8eb..a1ea320 100755 --- a/src/java/com/jogamp/common/nio/Buffers.java +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -544,22 +544,101 @@ public class Buffers { // Conversion routines // - public static float[] getFloatArray(double[] source) { - int i=source.length; - float[] dest = new float[i--]; - while(i>=0) { dest[i]=(float)source[i]; i--; } + /** + * @param source the source array + * @param soffset the offset + * @param dest the target array, if null, a new array is being created with size len. + * @param doffset the offset in the dest array + * @param len the payload of elements to be copied, if len < 0 then len = source.length - soffset + * @return the passed or newly created target array + */ + public static float[] getFloatArray(double[] source, int soffset, float[] dest, int doffset, int len) { + if(0>len) { + len = source.length - soffset; + } + if(len > source.length - soffset) { + throw new IllegalArgumentException("payload ("+len+") greater than remaining source bytes [len "+source.length+", offset "+soffset+"]"); + } + if(null==dest) { + dest = new float[len]; + doffset = 0; + } + if(len > dest.length - doffset) { + throw new IllegalArgumentException("payload ("+len+") greater than remaining dest bytes [len "+dest.length+", offset "+doffset+"]"); + } + for(int i=0; isource.remaining() + * @return the passed or newly created target buffer + */ + public static FloatBuffer getFloatBuffer(DoubleBuffer source, FloatBuffer dest) { + if(null == dest) { + dest = newDirectFloatBuffer(source.remaining()); + } + if( dest.remaining() < source.remaining() ) { + throw new IllegalArgumentException("payload ("+source.remaining()+") is greater than remaining dest bytes: "+dest.remaining()); + } while (source.hasRemaining()) { dest.put((float) source.get()); } return dest; } + /** + * @param source the source array + * @param soffset the offset + * @param dest the target array, if null, a new array is being created with size len. + * @param doffset the offset in the dest array + * @param len the payload of elements to be copied, if len < 0 then len = source.length - soffset + * @return the passed or newly created target array + */ + public static double[] getDoubleArray(float[] source, int soffset, double[] dest, int doffset, int len) { + if(0>len) { + len = source.length - soffset; + } + if(len > source.length - soffset) { + throw new IllegalArgumentException("payload ("+len+") greater than remaining source bytes [len "+source.length+", offset "+soffset+"]"); + } + if(null==dest) { + dest = new double[len]; + doffset = 0; + } + if(len > dest.length - doffset) { + throw new IllegalArgumentException("payload ("+len+") greater than remaining dest bytes [len "+dest.length+", offset "+doffset+"]"); + } + for(int i=0; isource.remaining() + * @return the passed or newly created target buffer + */ + public static DoubleBuffer getDoubleBuffer(FloatBuffer source, DoubleBuffer dest) { + if(null == dest) { + dest = newDirectDoubleBuffer(source.remaining()); + } + if( dest.remaining() < source.remaining() ) { + throw new IllegalArgumentException("payload ("+source.remaining()+") is greater than remaining dest bytes: "+dest.remaining()); + } + while (source.hasRemaining()) { + dest.put((double) source.get()); + } + return dest; + } + + //---------------------------------------------------------------------- // Convenient put methods with generic target Buffer // -- cgit v1.2.3