Scaling Images in Java and Kotlin

Published at 08:09 on 18 July 2020

It’s sort of confusing. There’s a lot of ways to do it. And by “a lot” I mean a lot.

Despite the range of options, my initial attempts at downsizing images were disappointing, yielding ugly results. As an example, consider this:

To fully appreciate how bad that is, here’s what Gimp produces when asked to do the same thing:

Yes, the same thing. In both cases, I am requesting the image be scaled with the bicubic algorithm. Clearly, there is more processing going on in Gimp than simply bicubic scaling. Despite my attempts, I kept getting this sort of disappointing result as I experimented with the various ways of doing it.

Finally, I ran across a magic combination of API calls that yields acceptable results:

val nWidth = (imageIn.width * ratio).toInt()
val nHeight = (imageIn.height * ratio).toInt()
val imageOut = BufferedImage(nWidth, nHeight, imageIn.type)
imageOut.createGraphics().run {
    drawImage(imageIn.getScaledInstance(nWidth, nHeight, BufferedImage.SCALE_SMOOTH), 0, 0, null)

That code produced the following:

Not quite as good as what Gimp yields, but close. The takeaway is that getScaledInstance does at least some necessary extra processing, above and beyond the standard scaling, which is needed to produce acceptable results. I probably wouldn’t want to use it for high-resolution production work, but for generating screen-resolution images for sharing on the Web it’s perfectly adequate.

Update: Some more research reveals that getScaledInstance  with BufferedImage.SCALE_SMOOTH doesn’t use the bicubic algorithm after all; it uses an area-averaging algorithm which is even slower. Those details are mostly irrelevant, however: the important thing is that for my application, it delivers acceptable quality in an acceptable amount of time, which is more than can be said for all the other API calls.

The Two Biggest OM-D E-M5 Annoyances

Published at 09:34 on 22 July 2012

  1. It’s very complex. There’s lots of settings to master. This sort of comes with the territory when shooting digital, and I expected it.
  2. The official documentation is, quite frankly, awful. Olympus only ships a very abridged printed manual with the camera, expecting users to consult a PDF file for any detailed information. Not a reasonable expectation, really, given that I don’t carry my computer with me into the field. And even the unabridged PDF version of manual is incomplete; see below for an example. This does not necessarily “come with the territory” and I didn’t expect at all.

Probably the most egregious example of how lacking the documentation is is Olympus’s description of the Magnify function:

Press the button once to display the zoom frame, again to zoom in. Keep the button pressed to cancel zoom.

“Excellent!”, I thought. I’m planning on using the camera, via an adapter, with my old Pentax telephoto lenses (which, thanks to the crop factor, will be twice as telephoto as they were before). “A way to force the camera into an MF assist mode with a single button press will be quite useful.” So I used the setup menu to assign the Magnify function to a button.

And kept finding that button did nothing. I tried and tried pressing it for various lengths of time, pressing it while turning one or the other of the two dials, pressing it while holding the shutter button halfway, etc. Nothing ever worked. Until I happened across a blog post that prompted me to try it again, this time with the preview screen enabled to display a live view (normally it is off and one uses the EVF to preview the scene).

That did the trick, but it shouldn’t have taken me over a week of experimentation followed by a number of Google searches to figure out something which should have been explicitly described in the manual.

I’m still glad I bought it, mind you (and consequently just spent more money to round out a basic lens outfit for it). It’s just that using it would be a far better experience if only the manual were more complete.

More on the New EVIL Camera

Published at 19:57 on 16 July 2012

I’m still trying to withhold final judgement, but my most recent tests indicate that it really should work quite well for me.

Recently took it camping, and the results were somewhat spotty: sometimes I could nail the focus, sometimes it just seemed to have issues. But that was trying to take pictures in windy weather (read: subjects waving in the wind), and I didn’t have a tripod (read: my own shaky hands were a factor).

Sure, it has image stabilization, as pretty much all digital cameras these days do. But that only goes so far, and nothing works as well as turning IS off and mounting the camera on a tripod. Once I do that, focusing consistency seems to improve to what it is with a traditional film SLR.

As an example, I tried to focus on the anthers of the floret in the center of the picture, and so far as I can tell that is where the focus ended up being.

Sure, it’s a little wonky: the view isn’t exactly live and the lens doesn’t respond at the exact moment you twist the focus ring, and you hear a little motor racking the focus in or out a fraction of a second after you tweak the focus ring a little bit one way or the other.

But, wonky or not, it is possible to manually set the focus with a minimum of fuss or bother. Really, given how fiddly macro photography is in general, by the time I’m done fiddling with the tripod and getting the framing right, the tiny bit of extra time it might take to focus is pretty much dust on the gears.

And it’s not all a loss, either. That picture was taken in the shade at ISO 1600. There’s no way you can get ISO 1600 slide film: it’s just not made. And ISO 1600 print film is way grainier than the image I just got above. I’d have had to fiddle with an off-camera flash or exposure times in the range of a second (and hope a slight breeze wouldn’t ruin the shot) to get the same results on fine-grained slide film.

EVIL Works Pretty Good

Published at 19:36 on 16 July 2012

Contemporary EVIL (electronic viewfinder, interchangeable lens) cameras, that is.

Encouraged by looking through two recent electronic viewfinders (which have improved astoundingly in the past few years), I purchased an Olympus OM-D E-M5 yesterday. Just took a batch of macro pictures with it, mostly using manual focus. Unlike a compact digicam, it’s totally usable for macro photography. Which is good, because that’s a big part of why I purchased it.

Mind you, the EVF is still not as good as an optical viewfinder: the view is slightly delayed, and like a movie or TV set it’s a sequence of still pictures in rapid succession, not a true moving view. It’s also a discrete set of pixels with a noticeable grain to it. But the point is it’s close enough to a true live view on a ground-glass screen to be completely useful, even in fairly gloomy light.

In fact, it can make it surprisingly easy to focus in gloomy light, because what you’re looking at in such situations is an amplified view of the available light. It can be a little disconcerting at first to look in the finder and see a bright scene when photographing on the forest floor. On the minus side, there’s a limit to how bright the EVF can be, and it ends up looking surprisingly gloomy in bright sunny situations. It’s still far better than trying to shade the preview screen on a camera body while at the same time attempting to hold the camera still, however.

Focusing the lenses feels a little on the odd side, too. Even though focusing is done in the traditional way by turning a collar on the lens, that collar is just a digital encoder which causes the camera’s CPU to tell the lens to rack itself in or out as it is being turned. But again, it’s good enough to allow precise manual focusing; it’s far better than the futile putsing around with buttons to manually focus a compact digicam.

In short, EVIL cameras do not offer the performance of an SLR, and probably never will. But that’s not completely the point: a 35mm SLR does not offer the performance of an 8×10 view camera (those are the big, old-fashioned-looking cameras that are inevitably used on tripods and which photographers get under a cloth to focus), yet despite that people use SLR’s because they are smaller, lighter, and more convenient than view cameras.

And so it is with EVIL cameras, particularly when compared to the size and weight of modern DSLRs, which tend to be significantly buliker and heavier than old film SLRs. Trading performance for convenience has a long history in photography. I’ve avoided purchasing a DSLR because I did not want to take that extra weight and bulk on the trail with me, and I’m pleased that my new EVIL camera will allow me to ditch even more weight and bulk.

So, Where From Here?

Published at 13:06 on 4 March 2012

First, slide film is not dead. Yet. Fuji still sells it, and fortunately I prefer Fuji’s slide films to Kodak’s anyhow. So I figure there’s at least several more years when I can continue shooting the exact same combination I’ve done for a decade now. As a bonus, the more people who do this, the longer slide film will last.

Next, color negative film is still going strong. I don’t like it as much as slide film, but Kodak has released a new emulsion in recent years (Ektar 100) that I’m actually quite fond of. So after slide film dies it will be decision time: is there a negative film available that is good enough? If so, I can shift to it.

If not, it will be time to begin the unpleasant process of learning how to cope with a new but inferior tool. Or perhaps time to abandon a form of photography which, while presently an enjoyable pursuit, will have become unduly tedious and frustrating thanks to technological processes beyond my control.

And I am loathe to bequeath the name “progress” onto such processes.

Why It Sucks

Published at 12:56 on 4 March 2012

Basically, the end of slide film (not yet, but the writing is clearly on the wall) sucks because it is my preferred choice for nature photography, and it is extremely unlikely that digital cameras will ever be as suitable for my nature photography as older, manual-focus film SLR’s are.

The Problem is Autofocus

Unlike most forms of photographic automation, autofocus is not and cannot ever be purely additive: if one adds autofocus feature, one of necessity must subtract other functionality from a camera.

That puts autofocus into a different and unfortunate category. Consider exposure automation: adding, say, shutter priority or program modes to a camera does not make it any less easy to continue offering a metered-manual mode, one which works every bit as well as it used to on an old, all-mechanical body. (Sure, you lose battery independence, but for macro photography on slide film, where metering is so critical and complex, you never really ever had that in the first place. A metered body offers so many advantages for such photography that one would be a fool not to use one.) Exposure automation also adds no weight penalty to a camera: weight now present in the form of solenoids, circuit boards, and a small battery is countered by weight in the form of clockwork that is now absent.

Autofocus, on the other hand, forces the manufacturer to add focusing motors to either the body or the lens (read: more weight and bulk), to add significantly-sized batteries to power those motors (which use much more electricity than a metering circuit or an electromechanical shutter, yet more weight and bulk), and to rob light from the finder for the autofocus sensor to use. Alternatively, one can delete the optical finder completely and replace it with an electronic one (which will fall short in both resolution and in low-light performance).

EVF’s Will Not Significantly Improve

Anyone who thinks otherwise needs to return back to the case of SLR cameras. Autofocus SLR bodies first started appearing about 20 years ago back when film was still king. The only new manual-focus SLR body introduced since that time which I am aware of is the Pentax ZX-M. (The Nikon FM-10 does not count; it was an existing Chinese camera that Nikon chose to rebrand and sell under their marque with their lens mount. Cameras like the Nikon F3 continued to be made well into the autofocus era, but they were not new introductions.)

More recently, there have never been any manual-focus DSLR bodies, from any manufacturer. The closest thing to such an attempt was Leica’s release of a digital back which turned their R8 and R9 bodies into exceptionally large and awkward (not to mention expensive) DSLR’s. In short, manufacturers have gotten away with deleting manual focus functionality. All evidence indicates that most photographers are willing to trade a superior finder for the autofocus feature.

Not me. I quite naturally evolved a style of very slowly and meticulously adjusting focus manually in my macro shots, often at the sub-millimeter level, and often in gloomy forest-floor conditions. I find that this matters a great deal where the depth of field is almost never as much as a quarter inch. This is about the worst possible conceivable case for either using autofocus, coping with a darker finder, or coping with the diminished resolution and lack of real-time response of an EVF. Consider the combination of needs: I need something that provides the highest resolution and which offers immediate response even in low light.

But that’s just me. Evidently, there are not enough photographers like me to prompt manufacturers to cater to our needs. Given this, what does that say about any incentive to make EVF’s have better resolution and imperceptible low-light lag? Basically, it says that such incentives do not exist. EVFs are being introduced to make autofocus camera bodies smaller and lighter. Period.

This is a recent insight of mine, and a most depressing one. I would actually like to have the choice of using digital for macro photography (and not have to make compromises which inevitably compromise the quality of the resulting images, or which compromise my ability to carry equipment on foot for significant distances). I used to hold out hope that EVFs would eventually get to the stage where they became as good as or better than the optical finders on manual-focus cameras. No longer.


In short, the only lightweight, compact, interchangeable-lens cameras suitable for ultra-precise manual focus at close range are film SLR’s. And this is unlikely to change.

Seriously, This Sucks

Published at 10:32 on 1 March 2012

Film as a whole might not be dead, but it certainly looks as if slide film is dying. Sure, Fuji is still making it, but that’s basically the last manufacturer. When a product is down to one remaining manufacturer, it’s pretty clear that its days are numbered.

And, unlike black-and-white film (which is technologically pretty simple stuff), manufacturing color film is a complex proposition, which means that the odds of any niche manufacturer deciding to make it are pretty slim.

Which sucks for me, because slide film is my medium of choice for photographing wildflowers. I’m still going to stick with it for the next year at least, given that I can still buy it from Fuji (whose slide film I prefer anyhow) and my budget for replacing my film nature-photography outfit is at the present time very limited.

What I’m not looking forward to is the possibility of having to make one or more unpleasant compromises. The main reason I’ve stuck with slide film for so long is that I’ve tried the alternatives and found them wanting. In particular, digital SLR cameras all tend to have at least some of the following disadvantages:

  • Heaver and bulkier bodies. The Pentax KX and K2 were unpopular in their day, because they were considered excessively heavy and bulky. Yet iturns out they are actually smaller and lighter than most current digital SLR bodies currently being made! Weight and bulk matters to me because I take my outfit on the trail.
  • Heavier and bulkier lenses. This is mainly due to modern lenses containing motors and electronics to support autofocus, a feature I seldom use. Why should I want to cope with a “feature” which is seldom of benefit to me, yet always a drawback?
  • Dimmer viewfinders. Again, this is due to the need to support autofocus: DSLR reflex mirrors are only partly-silvered, so that the autofocus system may rob light from the finder for its purposes.
  • Increased complexity. Older, mechanical cameras are simple to understand. If something goes wrong, it is fairly easy to tell what is to blame, and to either repair or replace the offending lens or body. With digital, you’re often chasing software bugs, which is an entirely different realm of complexity. Repair turnarounds for a digital camera issue can thus be significantly longer.

I’m not making any of this up; I battled all of them with the one DSLR I tried using for a few months before dumping it and using the proceeds to buy a good film scanner.

If I absolutely had to abandon film right now, I would probably start by looking at micro four-thirds and other mirrorless systems; they lack the weight and bulk penalty of most digital SLR’s. In fact, I’d probably be looking at them presently if my budget allowed for purchasing a new camera system; I’m not precisely a big fan of the film-scanning process.

Unfortunately, the last time I tried a camera with an electronic viewfinder, I found it to be unacceptable (excessive lag time in low light). If that’s still the case, then I’d probably look at the four-thirds system of SLRs next, followed by investigating to see if Pentax have finally gotten the rid of the last of the serious bugs in their DSLR firmware’s shake reduction code.

What’s unsettling about it all is that I have no assurance that I could find any presently-manufactured camera system that preforms acceptably compared to my current outfit. I’ve received enough patronizing condescension from drinkers of high-technology Kool Aid to realize that the masses are completely willing to excuse inferior performance if it’s sold as new and improved, and its doubters are berated as Luddites.