Get image dimensions (PNG/JPG) without loading the file in Common Lisp

Hey Common Lispers! Why are we so few, when this language, and tooling, is so amazing and fabulous? :)

Today I added PhotoSwipe on a website I'm working on for a friend. The backend is written in Common Lisp, because life's too short. PhotoSwipe is a wonderful library, It Just Works™, but it has this minor-yet-inconvenient requirement — you have to declare the image size (width and height) for each thumbnail, in some data attributes. I've searched for some Common Lisp code that could fetch an image dimensions from the file, and I couldn't find any. There are image manipulation libraries, alright, but they parse and decode the whole file (and that's slow), when I only need the dimensions. Those could be fetched from the headers, reading just a few bytes and consing nothing at all (or almost nothing).

Following research, since I couldn't find any code that does that, I wrote my own. I hope it will be useful for someone else too. Pasting it below, or get it from this GitHub gist. It should work okay for PNG, JPEG and GIF. I'm not so sure about WEBP. Any comments or improvements are welcome!

Update: I added a proper parser to figure out orientation from the Exif block (which is really a TIFF header).

[ read more... ]


Always choose simple

Today I was tasked with something rather trivial at work, and I wrote the simplest, dumbest code possible to solve the problem. A colleague of mine was surprised looking at my patch, she said it's so simple and beautiful, as she expected some more involved cleanup or refactoring. This brought to mind a little story I'd like to share.

[ read more... ]


No hover please

Ever watch videos online? Ha ha, of course you do. Now here's what bothers me. I set the video — for example — in full-screen, on a desktop, but I don't really watch continuously, I let it play and come back to it every now and then. Whenever I switch back to that desktop, a gross overlay with the video controls shadows the media for like 5 seconds. Even the slightest mouse movement brings on that despicable thing.

If this is pissing you off too, and if you are using Linux + Xorg/X11, then I have a solution. It's called nohoverplease but feel free to name it whatever you like, like fuckyouroverlay or something. It's an empty and fully transparent window that does nothing at all, it just sits there, maximized, on top of the browser, and won't let it know it's visible or catch mouse movements, so the damn overlay won't show up. I do have bigger problems in life, but it feels good to solve at least the little ones.

[ read more... ]


JavaScript Sudoku solver

My wife has developed a passion for Sudoku. I do not share it, I've always thought that this is a problem for computers, not humans, so I've never tried to solve one by hand. However, even if it's a high school problem, I thought it'd be a cute little program to work on. This is the obligatory blog post about it.

Update: see bestest version.

[ read more... ]


New server

“If you don't schedule time for maintenance, your equipment will schedule it for you”

So true! After almost ten years of continuous functioning, one of the hard drives failed and I was left with an unbootable server. Last time I checked it had more than five years uptime. I managed to backup, last minute, things that I should have backed up periodically, and I got a new server, also from Hetzner (whom I warmly recommend, without having any affiliation to them, other than the fact that they are my favorite host for the past 10 years or so).

Sysadmin work sucks. I only do it when the hard drive fails :) so that's about once every 10 years. Still, I was able to put back together my DNS server pretty quickly; also this website, because it's written in Common Lisp. 10 years old software, unchanged, compiled by bleeding edge SBCL running on bleeding edge Arch Linux; now that's stability.

Until the dust settles, comments will be disabled (missing a SMTP server for now). Permanent victims are my old Perl-based websites, including my old blog; I won't bother to get them back up, but there's the Internet Archive if you miss anything.

There'll be a new blog post soon, about Sudoku. I'm not dead yet. :)


Corona US Open and other thoughts

Nice to finally see some live tennis after so long! The US Open has started. Some top players are missing — no Nadal, no Halep (they rather concentrate on Rolland Garros; understandably, since clay is their preferred surface); no Federer (some knee injury).

Some of the bad boys who had the coronavirus are playing, like Djokovic and Dimitrov. The latter just went out. But it was a good match, take a look at the highlights if you like tennis. How strange it feels to watch these intense points without hearing the crowds' murmur of amazement! There are no crowds, because... pandemic! These huge arenas are empty.

Of course, players don't wear a mask during play; on average, a match takes 2-3 hours — it would be impossible to put that effort with a mask. But after the match, the winner gives an interview and that's... with a mask on! Take a look at Sofia Kenin. She's alone, in the middle of the empty court, with the mask on. The interviewer is nowhere to be seen, she's probably not even there.

Is it player's choice to wear the mask here? I don't think so, she's already been all over the place for two hours without a mask. Most probably, these are the regulations, but what could be the purpose? Clearly there is no risk to get infected, nor to infect anyone, because there's no one around! The virus doesn't just fall from the sky; if it did there would be no tennis and we'd stay indoors and homo sapiens would end, which probably wouldn't be too bad.. but I digress. The only purpose that can be is to maintain fear.

The most heartbreaking change is that they are no longer allowed to shake hands. Most players known each other for years, they are friends and they remain friends even after a hard fight; they used to shake hands and hug at the end, which is really what friends do, but now they just tick rackets and that's it. Sad. Meanwhile in footbal:

You can see these images after every goal. Now, I get it, manifestation of joy is part of the game, is something human and it shouldn't be forbidden, but then why not allowing tennis players to shake hands at the end? It just doesn't make sense. Also, while restaurants reopened just days ago with very strict rules, here's our beloved football players:

No distancing, no masks, indoors. This is practically illegal. What I'm trying to say, I guess, is that nothing seems to make sense! I feel like something more sinister than just the virus is going on. I want and try hard not to believe in conspiracy theories, but I feel like some unseen hand is manipulating all of the press and all the governments, worldwide, silencing voices that try to sing a different kind of music.

I find it weird that the Bill and Melinda Gates Foundation is pouring money both into the WHO, which declared the pandemic and dictates the regulations worldwide, and in ID2020, which recognizes “the opportunity for immunization to serve as a platform for digital identity“, which pretty much means they'd like to use vaccines to insert RFID tags into us; and that the press is talking about how the pandemic will not end until a vaccine is available, which kind of implies that vaccination will be mandatory, and all of a sudden conspiracy theories seem to become reality, in your face! They don't even hide!

I'm not exactly “anti-vaxxer”, but I try to get informed and think, and it's not hard to connect these dots. I wouldn't like to get an ID chip inside my body, so I will refuse vaccination... if I can! I think that's the end goal of this “pandemic” — mandatory vaccination of the whole planet, and that could mean digitally stamping everyone. My cat has a chip, but I'm not a cat and I'd rather not have one.



I haven't blogged in a while. I guess I didn't have anything good to say. There's nothing good here either, but I just have to vent my frustration.

1. “Stay the fuck home”

I'm fed up with this shit! When I hear “coronavirus”, “social distancing” or “self-isolation” I almost feel physically ill. That's all we hear these days.

I hate the lockdown. Perhaps it made sense, initially, but it seems to be getting way too far. It's easy to shout “stay the fuck home” when you can work from home, or when you're extremely rich, but what about the others?

My wife closed her after-school. It was a flourishing business. Children loved them. Their parents and their dog loved them. It was an organic, small but profitable business. It was a dream come true! And now it's closed for like two months, with no reopening hopes in sight. With schools closed until fall, she might as well file for bankruptcy.

Like her, there are hundreds of millions. What will these people do? How many of them will kill themselves? Things are quite depressing already: you can't see friends, you can't see family, you have to carry a paper with you at all times, where you declare where you're going to and why, as if that stupid paper would help preventing the spread! Add to this the loss of job/business/income, and no immediate hopes for a better future, and to many people suicide will start looking like a sweet outcome.

Wouldn't these people rather go on with their lifes, even if that means to risk getting infected?

I wish we at least stop parroting the news, stop shouting “stay the fuck home”, that's disrespect for those who did or will lose everything because everybody had to stay home.

2. The media

Why are these guys being censored? I'm just wondering, I mean, they're not just some random dude on Facebook; they are doctors, they have some pertinent opinions, that deserve to be mentioned. They're not saying it's a hoax. The disease is real, but perhaps it's not so bad as to warrant shutting down the planet. The discussion on the mainstream news is completely imbalanced, nobody who dares to question the benefits of the lockdown is allowed. Isn't this at least suspicious?

I'd like to see some balanced discussion, I'd like to see someone objectively analyzing the usefulness of the lockdown, and the long-term effects. Like these two doctors seem to do. But nope, the mass media is universally one-sided: lockdown! Anybody who suggests otherwise is “fake news”. Allow me to be skeptical.

3. The authorities

Politicians announced some gradual relaxing, after May 15. Announcements were made in cautious words and nothing concrete has been said, but some business will be allowed to reopen, like barber shops. The coffee shop where I used to go daily with my colleagues? — mmm nope, that stays closed. They're probably doing the final paperwork for their soon-to-be-history business right now. Or writing their wills.

I find some measures contradictory, if not downright stupid. Why open barber shops and no coffee shops?

Why are we not allowed to go out in groups bigger than three, when there's a hundred people in a grocery store?

It is my understanding that office companies will be allowed to reopen; of course, many of them will chose to continue to work from home, but the point is that they can get people to the office if they like to and that means dozens of people in an open-plan room. Why can't we go out in groups bigger than three, then?

Schools remain closed, because if children go to school they might get infected, and then infect their parents and hell breaks loose. But if their parents go to work, can't the exact same thing happen?

Some people go to church regularly. I'm not one of them, but I understand them. Why can't they attend church for a couple of hours, yet they can stay in an open-plan office for eight hours?

Why can't I drive to another town without having a solid reason — will I get infected in my car? I'd like to visit my parents, is that reason solid enough? Oh, and I can't take my children because then we'd be more than three.

Our PM said the economy will restart when the epidemic is over. Excellent news, thank you very much.

4. Me.

I'm depressed. I guess I'm in the high-risk group, being a smoker for too many years. Still, I'd rather take the risk than live a miserable life for the foreseeable future. If I do get the virus, I ain't going to no fucking hospital; I shut myself indoors and hope for the best, and if I die then so be it!


Emacs and JavaScript in 2017

About an year ago, when it became clear that sooner or later I'll have to write ES6 code, I decided to give js2-mode another look (wow, how time flies!)

Steve Yegge no longer seems to maintain this majestic creation, but as it often happens in the open-source world, quality code finds new home and maintainers. The official fork today is located at, and it's what you get if you install it from Melpa. However, I live on my own fork where I fix bugs as I find them. Eventually, my fixes get merged into the official repo.

On top of js2-mode, Magnar Sveen of fame wrote js2-refactor, a collection of tools for working on JavaScript code. It brings new powers at your fingertips — for example, you can select a piece of code and turn it into a function; it figures out what are the variables needed by that piece of code, and turns them into arguments of the new function; and replaces that piece of code with a call to the new function, which is inserted just outside the current function. All that happens in a split second — an operation that would take many seconds to do manually. If you edit JavaScript, and use Emacs, make sure to check js2-refactor.

Before switching to js2-mode + js2-refactor, I had some utilities for editing JS that worked with an external parser (based on UglifyJS). I have ported these tools on top of js2-mode and js2-refactor, so we don't need to call an external parser, and we get support for ES6. I wanted to briefly cover it in this post, and perhaps if more people are interested I will submit a pull request to js2-refactor.

[ read more... ]


Gazing at the numbers: the Collatz sequence

I've been obsessing on this problem for a couple weeks. While thinking on it I made some observations on my own, so it's worth a post on my blog. Note that I'm not a mathematician, so there's more English than Math here (which, for people like me, could in fact make it easier to read). I don't have proofs for the most interesting observations I made, and they might be known already for the trained mind.

If you studied the Collatz conjecture, then you might want to jump to the TL;DR.

The problem

The Collatz conjecture defines a sequence of positive integers that starts with any number $n > 1$, and at each step, in order to get the next number, you apply this simple logic, depending on the parity of the current number $n$:

  • When $n$ is even, the next number is $n / 2$
  • When $n$ is odd, the next number is $3n + 1$

The conjecture states that any such sequence eventually reaches $1$ (of course, we could continue after $1$ but it will loop). For an example, let's start with $n = 6$: 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1.

[ read more... ]


Don't sudo gnome-disks after beers

So yesterday I was repartitioning an USB stick and I was, say, less vigilant than I usually am when I play with these tools: the wrong disk was selected. I clicked through the confirmation dialogs without thinking too much, and gnome-disks happily removed my /boot and swap partitions. About the third partition, however, it complained with a “can't unmount it” message! (that was the root partition). As I was re-reading the message in horror, I realized I was messing with the wrong disk. Holy shmoly, so it did try to unmount it! "Thanks" goes to whoever made it impossible to remove a mounted partition, and "Thanks NOT!" goes to gnome-disks for not displaying a big fat warning when I try to remove a mounted partition that it can unmount.

[ read more... ]

« Before 30 Jul 2017