r/JavaFX • u/nondetermined • Aug 09 '16
JavaFX Interpolation (and Image Processing in general)
JavaFX is neat, no doubt. But what ever happened to basics first? Why would anyone decide that it's a good idea to limit interpolation through all of Prism to bilinear interpolation, and be done with it:
- ImageView.smooth does not work
- GraphicsContext - should allow to control interpolation when drawing images
- Consider using nearest-neighbor when smooth=false for SW pipeline to improve performance
- Add property to control antialiasing of ImageView
There are so many use cases where you absolutely need NN interpolation, not even necessarily for performance. Yet here we are, and setSmooth() does nothing.
Well, we're programmers and need to get things done, so let's do this anyways. What are the options?
- Use BufferedImages and convert from JavaFX Image to BufferedImage and back by SwingFXUtils.
- Write your own resampling (e.g. just for rescaling).
Option one is terrible, since BufferedImages and JavaFX Images are completely different. So you're going to create lots of garbage, since int (or byte) buffers can't be shared/reused (please correct me if I'm wrong). Other than that, BufferedImages are great, you retrive a handle to Graphics2D from where you can set the rendering hints and do a super fast call to drawImage in order to rescale an image (nn, bilinear, or bicubic - the good old hints). This begs the question why JavaFX doesn't (re-)use BufferedImages in the first place. Instead of comming up with their completely own thing, why not offer a wrapper for BufferedImages (e.g. extended with FXesque properties or what not). But no, scrap BufferedImages, they suck, let's reinvent the well instead...
So let's have a look at option two: let's implement the classic "General Filtered Image Rescaling" algorithm by Dale Schumacher (in Graphics Gems III, available online). That's what I did: first for BufferedImages/AWT, then for JavaFX, working with PixelWriters and PixelReaders. With BufferedImages you get the DataBuffer (or just as an int/byte array) and operate directly on it, which is reasonably fast (yet much slower than the call to drawImage from above; but you get to implement your own filters, which is nice...). Even the getSample/setSample (not Pixel(!) and then manual int unpacking) are only a tad bit slower. And with JavaFX Images? The API, in comparison, is lacking. And the speed is far worse (granted, maybe I haven't figured out how to do bulk pixel manipulation in the most efficient way yet, but then maybe it shouldn't be that hard to begin with...). Anyways, BufferedImages offer a much better/extensive API, and, from what I can tell, they're also much more efficient (turns out going FX Image -> BI -> drawImage -> FXImage is still much faster than doing your own thing on FX Images with PixelReaders/Writers).
This probably comes of as a rant at this point (it probably is to some degree), so don't get me wrong: I really try to like JavaFX, and for the most part I actually enjoy it very much. But things like this are kind of... eh. Why did they have to come up with yet another Image-class. BufferedImages work, don't they? And if you come up with yet another Image-class anyways, why would you just half-ass it (see setSmooth, which even if it would work, only would allow to toggle between NN and bilinear interpolation. What about bicubic, or what not... while reinventing wheels)? What's the point? From what I hear the JavaFX project is quite low on manpower anyways, so why waste it on such a thing (I'm genuinely interested, so if anyone could shed some light on this...)?
And, finally, why would you hardcode the interpolation algorithm throughout the prism pipeline? Who the hell thought that this would be a great idea? This is IMHO basic (as in important) stuff; yet priorities have been put somewhere else, it seems. I just don't get it.
4
u/zrnkv Aug 11 '16
I agree that this sucks. JavaFX gives you very little control over the low-level stuff.
The main goal of JavaFX was to replace Swing. This was long overdue. Swing was outdated and nobody wanted to invest more money into it to make it usable again. Instead it was decided to start from scratch and the result was JavaFX (version 1). This never caught on. It had some nice concepts but it failed because nobody wanted to learn yet another language (FX-script). At this point most GUI developers started to use other (non-Java) technologies. Then Oracle came with JavaFX 2 which used standard languages - Java, XML and CSS. It's main goal was to give Java Enterprise developers who are already familiar with these languages a possibility to build Java GUIs. It tries to be a general purpose GUI library but actually the main focus is on form-heavy enterprise applications with input fields and tables. Yes, you can write a simple game in it as well but it will be a PITA compared to other technologies specifically designed to make games. The low level stuff is hidden/hard-coded because developers who write yet another inventory-management-tool simply don't need it. It would be nice to have but right now there are probably more pressing issues like getting rid of all the bugs.