Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a functional and yet functional image processing library in Scala
    primarykey
    data
    text
    <p>We are developing a small image processing library for Scala (student project). The library is completely functional (i.e. no mutability). The raster of image is stored as <code>Stream[Stream[Int]]</code> to exploit the benefits of lazy evaluation with least efforts. However upon performing a few operations on an image the heap gets full and an <code>OutOfMemoryError</code> is thrown. (for example, up to 4 operations can be performed on a jpeg image sized 500 x 400, 35 kb before JVM heap runs out of space.)</p> <p>The approaches we have thought of are:</p> <ul> <li>Twiddling with JVM options and increase the heap size. (We don't know how to do this under IDEA - the IDE we are working with.)</li> <li>Choosing a different data structure than <code>Stream[Stream[Int]]</code>, the one which is more suited to the task of image processing. (Again we do not have much idea about the functional data structures beyond the simple <code>List</code> and <code>Stream</code>.)</li> </ul> <p>The last option we have is giving up on immutability and making it a mutable library (like the popular image processing libraries), which we don't really want to do. Please suggest us some way to keep this library functional and still functional, if you know what I mean.</p> <p>Thank you,<br> Siddharth Raina.</p> <p><strong>ADDENDUM:</strong> <br>For an image sized 1024 x 768, the JVM runs out of heap space even for a single mapping operation. Some example code from our test:</p> <pre><code>val image = Image from "E:/metallica.jpg" val redded = image.map(_ &amp; 0xff0000) redded.display(title = "Redded") </code></pre> <p>And the output:</p> <pre><code>"C:\Program Files (x86)\Java\jdk1.6.0_02\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 10.0.2\bin" -Dfile.encoding=windows-1252 -classpath "C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\rt.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.6.0_02\jre\lib\ext\sunpkcs11.jar;C:\new Ph\Phoebe\out\production\Phoebe;E:\Inventory\Marvin.jar;C:\scala-2.8.1.final\lib\scala-library.jar;C:\scala-2.8.1.final\lib\scala-swing.jar;C:\scala-2.8.1.final\lib\scala-dbc.jar;C:\new Ph;C:\scala-2.8.1.final\lib\scala-compiler.jar;E:\Inventory\commons-math-2.2.jar;E:\Inventory\commons-math-2.2-sources.jar;E:\Inventory\commons-math-2.2-javadoc.jar;E:\Inventory\jmathplot.jar;E:\Inventory\jmathio.jar;E:\Inventory\jmatharray.jar;E:\Inventory\Javax Media.zip;E:\Inventory\jai-core-1.1.3-alpha.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 10.0.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain phoebe.test.ImageTest Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at scala.collection.Iterator$class.toStream(Iterator.scala:1011) at scala.collection.IndexedSeqLike$Elements.toStream(IndexedSeqLike.scala:52) at scala.collection.Iterator$$anonfun$toStream$1.apply(Iterator.scala:1011) at scala.collection.Iterator$$anonfun$toStream$1.apply(Iterator.scala:1011) at scala.collection.immutable.Stream$Cons.tail(Stream.scala:565) at scala.collection.immutable.Stream$Cons.tail(Stream.scala:557) at scala.collection.immutable.Stream$$anonfun$map$1.apply(Stream.scala:168) at scala.collection.immutable.Stream$$anonfun$map$1.apply(Stream.scala:168) at scala.collection.immutable.Stream$Cons.tail(Stream.scala:565) at scala.collection.immutable.Stream$Cons.tail(Stream.scala:557) at scala.collection.immutable.Stream$$anonfun$flatten1$1$1.apply(Stream.scala:453) at scala.collection.immutable.Stream$$anonfun$flatten1$1$1.apply(Stream.scala:453) at scala.collection.immutable.Stream$Cons.tail(Stream.scala:565) at scala.collection.immutable.Stream$Cons.tail(Stream.scala:557) at scala.collection.immutable.Stream.length(Stream.scala:113) at scala.collection.SeqLike$class.size(SeqLike.scala:221) at scala.collection.immutable.Stream.size(Stream.scala:48) at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:388) at scala.collection.immutable.Stream.toArray(Stream.scala:48) at phoebe.picasso.Image.force(Image.scala:85) at phoebe.picasso.SimpleImageViewer.&lt;init&gt;(SimpleImageViewer.scala:10) at phoebe.picasso.Image.display(Image.scala:91) at phoebe.test.ImageTest$.main(ImageTest.scala:14) at phoebe.test.ImageTest.main(ImageTest.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115) Process finished with exit code 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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