Lossless cropping of lossy encoded images

Dumb question, but is there a way to crop an jpeg without re-encoding it?

Also, unrelated question, is there a way to get acorn to warn you when you’re about to make a destructive save? I sometimes accidentally hit “save” instead of “save as” when cropping a jpeg and lose the original encoding. Undoing the crop and re-saving the file still re-encodes it.

There’s no way to crop a JPEG image without recompressing it, at least that I’m aware of.

And there’s no way to warn about the destructive save- though it’s something I’ve been considering adding in a near term release. Basically, any images that aren’t native to Acorn would open up as untitled windows, and you’d have to do explicit save over the source image to write it back out again to the same location. Is that what you’re after?

Actually, looks like I’ve already been exploring this code a bit, for Acorn 6.1, but it’s hidden behind some hidden prefs. You can enable it though, by opening up Terminal.app and pasting in the following two lines and pressing enter:

defaults write com.flyingmeat.Acorn6 autosavesInPlace 1
defaults write com.flyingmeat.Acorn6 nullNonAcornDocumentURLs 1

If you try it out, let me know how it works out for you.

-gus

I think you can do lossless cropping, but you have to restrict the cropping selection I believe due to how jpegs are encoded. I don’t know all the details. Chain of thought was “I’m just creating an image with a subset of existing data. Why do I need to re-encode?” -> “Because it’s lossy encoded, dummy. It was decoded so you could edit it. Now you have to re-encode it.” -> “Oh… apparently there are tricks that allow you to do it anyway due to the way the compression works.”

On the destructive save issue, setting both of those defaults to 1 works basically, so that’s cool :slight_smile:

As you indicated, the window opens untitled and so when you save, you have to select a format, folder, and file name when you save.

Some suggestions:
Instead of piggy backing on the existing untitled window use case:

  • check for nonAcornDocumentURL on save
  • pop up window warning of destructive save (can have this configurable via a default) with Cancel, Save, Save As
  • If choosing Save As, default to the image’s original format, folder and file name. (A user would need to edit this, but would have the original filename populated if they just append edit strings to the original file name.)

Thanks for the info :slight_smile:

I believe JPEG compresses blocks in the image. So in theory, if the edges of the blocks match up exactly with where you want to compress, then there could be a lossless cropping. I’m not about to write a JPEG decoder just for cropping though :slight_smile:

Thanks for the ideas on the destructive saving. I’ll see if I can get those added in where it makes sense.

-gus

macOS inbuilt command jpegtran supports lossless cropping and more.

jpegtran - lossless transformation of JPEG files

  • crop
  • rotate
  • flip
  • transpose
  • transverse
  • trim
  • wipe/clear

example shell usage:

Neat, I hadn’t come across jpegtran before. It’s not built into the OS by default though. I’ll have to check out the license to see if I can use it in RB.

ah, apologies! one should never assume, and that’s what I did.

I probably got it via brew install libjpeg

licence seems to be permissive.

We welcome the use of this software as a component of commercial products.
No royalty is required, but we do ask for an acknowledgement in product
documentation, as described under LEGAL ISSUES.

and

We specifically permit and encourage the use of this software as the basis of
commercial products, provided that all warranty or liability claims are
assumed by the product vendor.

there’s a fork with “significant performance improvements” that looks interesting: GitHub - cloudflare/jpegtran: jpegtran fork with significant performance improvements

Would be good to have this in both Acorn and RB.

Sweet- I’ll have to look into that sooner rather than later.