Note that there are some explanatory texts on larger screens.

plurals
  1. POScala - how to explicitly choose which overloaded method to use when one arg must be null?
    primarykey
    data
    text
    <p>All,</p> <p>I'm doing some image manipulation in Scala by making use of BufferedImages and Raster objects. I am attempting to get all the pixels in the buffered image with the following code.</p> <pre><code>val raster = f.getRaster() // Preallocating the array causes ArrayIndexOutOfBoundsException .. http://forums.sun.com/thread.jspa?threadID=5297789 // RGB channels; val pixelBuffer = new Array[Int](width*height*3) val pixels = raster.getPixels(0,0,width,height,pixelBuffer) </code></pre> <p>Now, when I read in relatively large files, this works fine. When I read in 20x20 PNG files, I get an ArrayIndexOutOfBoundsException:</p> <pre><code>java.lang.ArrayIndexOutOfBoundsException: 1200 at sun.awt.image.ByteInterleavedRaster.getPixels(ByteInterleavedRaster.java:1050) </code></pre> <p>I've read <a href="http://forums.sun.com/thread.jspa?threadID=5297789" rel="noreferrer">online</a> that the way around this problem is to NOT preallocate the pixelBuffer, but instead to pass in a null value and use the one returned by the Raster.getPixels method.</p> <p>Here's my problem. When I do the naive approach and just pass Nil as the last argument:</p> <pre><code>val pixels = raster.getPixels(0,0,width,height,Nil) </code></pre> <p>I get the error</p> <pre><code>error: overloaded method value getPixels with alternatives (Int,Int,Int,Int,Array[Double])Array[Double] &lt;and&gt; (Int,Int,Int,Int,Array[Float])Array[Float] &lt;and&gt; (Int,Int,Int,Int,Array[Int])Array[Int] cannot be applied to (Int,Int,Int,Int,Nil.type) val pixels = raster.getPixels(0,0,width,height,Nil) </code></pre> <p>So obviously the compiler cannot determine which of the two methods I'm trying to call; it's ambiguous. If I were using Java, I would cast the null to make my intent explicit. I can't quite figure out how to get the same effect in Scala. Things I've tried:</p> <pre><code> val pixelBuffer:Array[Int] = Nil // Cannot instantiate an Array to Nil for some reason Nil.asInstanceOf(Array[Int]) // asInstanceOf is not a member of Nil </code></pre> <p>Any idea how to tell the compiler explicitly that I want the method with the Int array as last parameter rather than a Float array?</p> <p>EDIT: As an answer points out, I was getting Nil mixed up with null. Nil is an empty list. See the following <a href="http://javaforyou.wordpress.com/2009/07/12/nothingness/" rel="noreferrer">blog post</a></p> <p>Also, I should point out that the array out of bounds exception was my fault (as these things often are). The problem was I was assuming that the raster had 3 channels, but my image had 4 channels, since I had created it that way. I instead preallocate the array as follows:</p> <pre><code>val numChannels = raster.getNumBands() val pixelBuffer = new Array[Int](width*height*numChannels) val pixels = raster.getPixels(minX,minY,width,height,pixelBuffer) </code></pre> <p>Thanks for the help</p>
    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.
    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