Parsing Exif Data

Emacs is moving away from ImageMagick support, and is instead handling all the major image formats (PNG, JPEG, etc) natively. The reason for this is that the ImageMagick libraries have a pretty bad track record: Over the years, a large number of Emacs crashes have turned out to stem from ImageMagick crashing. While things have been getting better, this is still a problem, especially as Emacs is being used as a web browser and a buffer overflow can lead to code execution.

But ImageMagick provides features that the native libraries do not, like image scaling and rotation, and without image scaling, everything’s sad. Have you tried looking at a web page in Emacs with all images being ENORMOUSxGINORMOUS pixels large?

Sad.

Fortunately, Emacs 27, while deprecating ImageMagick, has gained native image transforms at the same time, so we’re all set, I think. The only thing that’s missing is a way for image-mode and the like to suss out what the rotation of images should be, and that data is stored in the Exif portion of JPEG images.

So this weekend I finally got started with writing an Emacs Lisp package to parse Exif data (in JPEG images)… and I finished, too, because the format is way simple. Which shouldn’t come as a surprise, because it’s meant to be implemented by camera manufacturers, and they er well you know.

But it’s an interesting format. It has the smell of being cobbled together from whatever formats people had lying around, and it’s not… er… smart. I don’t think I’m being controversial when I’m saying that.

I’m not an Exif expert: All I know what was I googled while writing exif.el yesterday. If I say anything horribly wrong here, I’m er horribly wrong:

Basically, the Exif format is a TIFF file plonked into the APP1 field of a JPEG. The TIFF format is the weirdness: It’s based on four-byte (32 bit) offsets instead of something sensible like length specifications. And the offsets are always from the start of the TIFF file, so they’re absolute. (Well. Relative to the start. Relatively absolute.)

This means that there’s no way to say “just extend this string”: You have to recompute (or regenerate) the entire TIFF file, because everything that points to something after the string will have their offsets changed. One person writing one of the many web pages that try to explain the format laconically commented that no Exif editors do so correctly, or without corrupting at least some part of the data.

The main part of the TIFF is the IDF: The Image File Directory. It looks like this:

LL
TT|FF|LLLL|VVVV
TT|FF|LLLL|VVVV
TT|FF|LLLL|VVVV
...
NNNN

LL (two bytes; a 16 bit number) say how many entries there are, and then each entry is 12 bytes long. In each entry TT is the tag, FF says what “format” the data is, LLLL says how long the data is (a 32 bit number), and VVVV is the value of the data.

Simple, eh? Hah!

The format is stuff like “ascii”, “short” and “long”. A “short” is 2 bytes and s “long” is 4 bytes. To find the actual length of the data, you have to multiply LLLL with this number of bytes, so if you introduce a new format (with a different byte length), there’s no way for older parsers to know how long the data is! No wonder it’s common for Exif editors to mangle the data.

Besides not making sense, it’s not even any kind of optimisation, because no part in a TIFF file can be longer than what can be described by a 32-bit number, so having LLLL just specify the length directly would have been just as fine.

*sigh*

And then the real fun: If LLLL (times the format length) is shorter than 4, then VVVV is the value. If it’s longer than 4, then obviously it can’t fit into VVVV, so… VVVV (a 32 bit number) holds an offset value that points to a place in the TIFF file where the data really is.

But there’s nice things about the TIFF format. I mean, it has fractions. (They’re represented by eight bytes, the numerator is the four first and the denominator is the last four.) Ideal for parsing with Common Lisp, but unfortunately Emacs Lisp doesn’t have rational numbers.

Oh, I didn’t mention what NNNN is: It’s a pointer to the next directory section, which is er useful if er you have more than 65536 directory entries, I guess.

I think the funniest part of the Exif format is that the numbers embedded in it can be either little-endian and big-endian. Fortunately this is called out explicitly with a bit that says either “II” (Intel) or “MM” (Motorola), so it’s no biggie, but it’s just weird that they couldn’t decide on one or the other.

But isn’t it nice that all this archaeological technology lives inside our modern devices and programs? Just think of that the next time you see an image that’s correctly rotated for sure.

NFLX2019 September 20th: Between Two Ferns: The Movie

Between Two Ferns: The Movie. Scott Aukerman. 2019. ☆☆☆☆★★

*gasp*

This is like the first Netflix Original in a while that isn’t “well, if you like that kind of stuff, here’s a movie that’s kinda like what you’d like to see, only not actually that movie you’d like to see”.

Instead if’s a mockumentary about Between Two Ferns, the Interwebs TV show where Zach Galifianakis insults celebrities.

I’ve only seen a handful of episodes. They can be very amusing, but the concept is so limited that it doesn’t really… feel vital.

But this movie kinda works! They do the obvious thing: Pack the two ferns into a car and make it into a road movie with some interviews between the ferns interspersed. They’ve got some funny performances in here: Chrissy Teigen is great, for instance, and the Peter Dinklage thing is funny.

But there’s also scenes where they… inexplicably… try to go for real emotions and stuff, and those kind of drag.

And also: As with the Interwebs series: The jokes aren’t really that good. It’s also disappointing that they don’t have enough confidence in their jokes and then resort to explaining them (“La Croix”), which is just embarrassing.

This post is part of the NFLX2019 blog series.

NFLX2019 September 13th: Tall Girl

Tall Girl. Nzingha Stewart. 2019. ☆☆★★★★

There haven’t been many Netflix Original movies the past few months. I’ve kinda missed watching horrible, horrible movies.

This doesn’t really look that bad. It’s a high school comedy where the main concept is that it’s embarrassing to be a tall girl. I can relate. I’m tall, girl.

So I was just idly wondering “if this is such a big deal, why doesn’t she just move to Norway” and the next second a (tall) exchange student from Sweden walks in.

So close!

And so is this movie, really. It’s got all the required set pieces: The nerds, the Heathers, the movie parodies, the wipe cuts, the album scratches, and the over-the-top characters.

But it just doesn’t have funny enough jokes, and the pacing is way too slow. Instead of a totally super-zany makeover scene (it always works in these movies), you have them dragging down the paces by having the makeoveree (that a word) saying stuff like “thanks mom… but… it’s just not… me…”.

Edit! Edit!

I think there’s a salvageable movie in here.

Sabrina Carpenter steals every scene she’s in: It’s like she’s in the version of this movie that’s how it’s supposed to be, and the rest are in a much duller film.

It’s frustrating watching this. At least when a movie totally sucks, you can enjoy hating it.

This post is part of the NFLX2019 blog series.

My New Innovations in Packaging blog

While unpacking this week’s haul, I was surprised when unpacking a Laraaji album (Sun Transformations) sent to me by…

… All Saints Records.

It’s this intricately, but beautifully cut paper. It feels like just kinda normal paper, but since it’s been cut in this way, it replaces bubble wrap. It’s kinda magical: It’s light but has kept the album safe during shipping.

And this is much, much, much better than previous paper-based packaging material I’ve seen: I’ve had some just kind of pour out of the package and get into everything.

I hope everybody starts using it.

BC&B: Salade Frisée aux Lardons aux Lyonnais w/ Mon Gâteau au Chocolat

I think I’ll do a salad tonight, because… Salads.

As salads go, it’s not very saladey. I mean, the main ingredient is pork sausage. Those aren’t green. Or not supposed to be green. If you’re eating green pork sausage, you should probably reconsider.

I went to a couple of stores to get endive, but couldn’t find anything. So I finally thought “perhaps it’s called something completely different in Norwegian”, and indeed it is: “Sikory.” Which is very similar to “chicory”, which she mentions up there. Like duh. I should read recipes more thoroughly.

But… it mentions “frisée” in the name I see now. We do have frisee salads here. I mean, it’s nothing like an endive. And I got endives. I think. “Sikory.” But small.

Gah!!!

That’s a frisée! So I got the wrong salad!

Gah!

As if this were not baffling enough, what we in the U.S. (and France) refer to as frisée is in turn called endive in the U.K.

OK, I’m soldiering on with the wrong type of greens…

And Stargate: SG1 and beer to drown my sorrows.

The recipe says “fresh pork sausage”, so I got some raw salsiccia. Which is Italian… but it’s fresh and it’s pork, so…

Mm… looks so appetising simmering for half an hour.

The dressing (Dijon, peanut oil, vinegar) is kinda tasty. I don’t really go for super-vinegary salad dressings, but this one’s nice.

So that’s the sausage in the wrong kind of salad. It would have looked visually better with the frisée, I mean endives, I mean the curly green stuff. It’s so… pale…

She said to cut both the bacon and the bread in one-inch cubes. That’s oddly huge for crutons, isn’t it?

That’s a really unappetising-looking salad bowl, isn’t it? All the colours are somewhere between sickly green and sickly yellow.

But the flavour is kinda good. The sausage is excellent, the croutons could have been more baconey (needs more bacon), and the dressing goes well with it.

The salad should definitely have been endives instead of endives, though.

Since I’m just making a salad (and a cake), it’s fitting that the book I’m reading tonight is a very small one. I picked up The Red Tenda of Bologna by John Berger on a whim at a bookstore the other month because I watched The Seasons in Quincy the other er year. I don’t think I’ve read anything of him before.

So let’s read the first three pages of this slim booklet:

Hey, it’s nice. It’s a reminisce told in these short sections.

And there’s even a recipe in here. Nothing could be more apposite.

Goes well with a salad.

So let’s make a cake, too:

That’s the easiest chocolate cake recipe ever. And I’m already slightly sceptical: I like cakes with lots of flour. I mean, I love chocolate, but I really prefer to eat chocolate as chocolate, and not dissolved into butter and named “cake”.

But let’s see. Perhaps this one will be fab.

So the most important bit: Melting the chocolate with some unsalted butter.

I used the pot-in-pot thing. I think it’s less work than the newfangled microwave method.

Mmm… deli… OK, it looks horrible. But then!

Smoooth chocolate.

And then you whisk some egg whites and beat the yolks into the butter and then add the way-too-little flour and then carefully fold the egg white into the batter and:

Presto! Wrinkly-looking cake!

I may have slightly overcooked it. I tested it (with the wooden stick trick) and it was too underdone and then a second later it was overdone.

But not too bad.

To drink with this cake, she suggests a fortified wine called Banyuls, which I have never had before, I think. It’s basically a French Port.

Mmm… They really do go well together. I had like three slices and er six glasses. Nice.

This blog post is part of the Bistro
Cooking & Books
series.