Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a DataBufferUShort from a ByteBuffer
    primarykey
    data
    text
    <p>(Oracle Java 1.7, Eclipse Kepler, Ubuntu 12.04 64 bit)</p> <p>I need to create a <strong>DataBufferUShort</strong> from a <strong>ByteBuffer</strong> but all my attempts have failed at run time..</p> <p>How is this done, <em>por favor</em>?</p> <p>Here is one failure: ( java.lang.UnsupportedOperationException )</p> <pre><code> ShortBuffer sBuf = imageData.asShortBuffer(); // ByteBuffer DataBufferUShort dataBufferUShort = new DataBufferUShort(sBuf.array(), nPixels); </code></pre> <p>Here's the stack trace. Line 156 is the second line in the code snippet above.</p> <pre><code> Exception in thread "main" java.lang.UnsupportedOperationException at java.nio.ShortBuffer.array(ShortBuffer.java:959) at psw.iu.CreateImage.createGrayscaleImage(CreateImage.java:156) at psw.iu.TestCreateImage.&lt;init&gt;(TestCreateImage.java:48) at psw.iu.TestCreateImage.main(TestCreateImage.java:16) </code></pre> <p>Here's the method that creates the ByteBuffer</p> <pre><code> public static ByteBuffer createScaledData(WorkingByteBuffer rawWBB, ByteBuffer scaledByteBuffer) { rawWBB.byteBuffer.rewind(); // byteBuffer is a ByteBuffer scaledByteBuffer.rewind(); ShortBuffer inBuf = rawWBB.byteBuffer.asShortBuffer(); ShortBuffer outBuf = scaledByteBuffer.asShortBuffer(); inBuf.rewind(); outBuf.rewind(); int maxDN = MAX_USHORT; double scaleFactor = (double) maxDN / (double) range; while (inBuf.hasRemaining()) { int value = 0xffff &amp; inBuf.get(); int scaledValue = (int) ((double) (value - min) * scaleFactor + 0.5); if (scaledValue &lt; 0) scaledValue = 0; if (scaledValue &gt; maxDN) scaledValue = maxDN; outBuf.put((short) scaledValue); } return scaledByteBuffer; } </code></pre> <p>Edit This gives no errors but it's not the solution I was looking for.</p> <pre><code> imageData.rewind(); ShortBuffer shortBuffer = imageData.asShortBuffer(); shortBuffer.rewind(); DataBufferUShort dataBufferUShort = new DataBufferUShort( nLines * nPixPerLine); int index = 0; while(shortBuffer.hasRemaining()){ dataBufferUShort.setElem(index++, 0xffff &amp; shortBuffer.get()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload