Jeff Kaufman  ::  Blog Posts  ::  RSS Feed  ::  Contact

Compiling SSIM on Mac

If you want to examine the distortion that comes from a lossy compression algorithm, it's helpful to have a tool that's a good proxy for asking a person "how similar are these two images"? The standard algorithm for this is SSIM, but there are several implementations. I was initially using Pornel's dssim, but I wanted to compare this to Mehdi's SSIM. Mehdi's doesn't document how to compile it on a mac, so here's what worked for me:

  1. Install OpenCV
    1. Download OpenCV for linux/mac. I downloaded 2.4.9 from opencv.org/downloads.
    2. unzip ~/Downloads/opencv-2.4.9.zip
    3. cd ~/Downloads/opencv-2.4.9/
    4. mkdir build && cd build
    5. cmake -G "Unix Makefiles" ..
    6. make -j8
    7. sudo make install
  2. Download SSIM.ccp: wget http://mehdi.rabah.free.fr/SSIM/SSIM.cpp
  3. The SSIM.cpp file needs a small change: replace "#include <iostream.h>" with "#include <iostream>".
  4. Compile with:
    g++ -I/usr/local/include/opencv \
        -L/usr/local/lib/ \
        -lopencv_core \
        -lopencv_highgui \
        -lopencv_imgproc \
        SSIM.cpp \
        -o ssim
  5. Enjoy using SSIM: ./ssim a-original.png a-modified.png

full post...

Why Parse the User-Agent?

People really hate it when web servers decide what to do based on User-Agent:

"UA strings need to die a horrible horrible death."
  — UnoriginalGuy

"The UA string is flawed by itself. it shouldn't even be used anymore. The fact that browser manufacturers have to include all sorts of stuff is proof that this system doesn't work."
  — guardian5x

"Well written sites use feature detection, not user-agent detection."
  — Strom

"We'd all be better off if they just stopped sending the UA string altogether"
  — nly

The modern advice is to use feature detection. Instead of the server intepreting the User-Agent header to guess at what features the browser supports, just run some JavaScript in the browser to see if the specific feature you need is supported. When this fits your situation this is great, but it's almost always slower. Many times it's not enough slower to matter, just a few more lines of JavaScript, but let's look at a case where the performance issues are substantial.

Let's say I want to show you a picture of a kitten:

How big is that? [1] It depends how we encode it:
Unoptimized JPEG 290.0 kB
Optimized JPEG 38.6 kB
Optimized WebP 20.2 kB
Optimizing the JPEG makes it 7.5x smaller, and WebP gets it another 1.9x smaller on top of that. (Update 2014-11-23: These sizes are off because of mismatched quality settings. More details.) So why doesn't everyone use WebP? The problem is not all browsers support it. For now, the best we can do is send WebP to the browsers that support it, and optimized JPEG to ones that don't. With feature detection this would look like, roughly:

    <img id=img>
    <script>
       var img = document.getElementById("img");
       if (SupportsWebP()) {
          img.src = "image.webp";
       } else {
          img.src = "image.jpg";
       }
    </script>
First the browser downloads the HTML, then it runs the javascript, and then depending on the value of SupportsWebP() [2] it either loads image.webp or image.jpeg. What's inefficient about this? How is this worse than just handling <img src="image.webp">?

The problem is this breaks the preload scanner. Technically, the browser is supposed to make its way through the web page piece by piece, handling each bit as it comes to it. For example, if it gets to some external JavaScript, it's supposed to fetch and run that script before continuing on with anything else. To load your page faster, however, your browser cheats. While it's waiting for that script to load, it looks ahead through the rest of the page for resources it thinks it's going to need and fetches them. And, critically, that scanner doesn't run javascript.

Here are two example pages, containing a single external script and an image: one loads the image with JavaScript and one uses an ordinary img tag. Running these both through WebPageTest (1, 2), here are charts showing how the browser loaded the page:


loaded with JavaScript


loaded with img tag

You can see that in the JavaScript case the browser loaded everything in order while when using an img tag the two files could be loaded in parallel. [3]

To emit html that references either a JPEG or a WebP depending on the browser, you need some way that the server can tell whether the browser supports WebP. Because this feature is so valuable, there is a standard way of indicating support for it: include image/webp in the Accept header. Unfortunately this doesn't quite work in practice. For example, Chrome v36 on iOS broke support for WebP images outside of data:// urls but was still sending Accept: image/webp. Similarly, Opera added image/webp to their Accept header before they supported WebP lossless. And no one indicates in their Accept whether they support animated WebP.

This leaves us having to look at the User-Agent header to figure out what the browser is, and then look up what features that browser supports. The header is ugly, I hate having to do this, but if we want to make pages fast we need to use the UA.

(The full gory details: kernel/http/user_agent_matcher.cc.)


[1] I uploaded this picture to my server as a poorly optimized jpeg, but I'm running PageSpeed. You should be seeing WebP if your browser supports it, or an optimized JPEG if it doesn't.

[2] Which would be a bit of an awkward function.

[3] This only is a problem because of the external script reference. If there were nothing to block the regular parser then both versions would be just as good. (1, 2) Most pages do reference external scripts, however, so in practice the preload scanner helps a lot and you don't want to disable it.

full post...

Gendered Behavior

One of the best comments on yesterday's post post was by a trans woman who wrote, paraphrasing:

There are lots of ways people treat men and women differently. Not just pronouns: body language, formality, assumptions. As a woman I want people to group me with women in all of these ways, but pronouns are really the only one most people seem to be willing to put in the effort for. Which means that if we stop using gendered pronouns I'll lose one of the few situations where people respect my gender at all.

This makes sense. If we were to keep our current social environment and just drop gendered pronouns it would be an awkward tradeoff. Some people would be very happy that they could mostly sidestep the question of gender; others would feel like they were losing the only way they currently get any acknowledgement of their gender, any counterbalance to the many subtle ways people continuously misgender them. I think it would be an improvement on balance, but I'm not sure.

It seems like the real problem here, however, is using apparent gender in deciding how to treat people. Thinking through examples, I haven't found any that are beneficial or even benign. Keeping explicit recognition of gender via pronouns seems like the wrong way to handle this: fight excessive gendering with more gendering? Gender-neutral pronouns aren't enough on their own, but for many of us they're one of the only ways we intentionally treat men and women differently. It seems like it would be harder to get rid of all the small ways we indicate to others how we have categorized them while still keeping around these big explicit ones. more...

Pushing They

In the comments on yesterdays post a few people brought up ideas like, paraphrasing:

When a cisgender person says they don't care what pronouns people use for them they trivialize the experiences of people living with the dysphoria that comes from constant misgendering. To a cis person this isn't real, it isn't raw, it doesn't matter. For a cis person to express a pronoun "preference" ignores that for lots of people it's not a preference, it's a core part of their identity.
This is a major reason why we should get gender out of language. Right now, every time you use a pronoun there's the potential to hurt someone; we're hanging core parts of people's identities out for constant affirmation or disaffirmation. As long as pronouns have gender implications this is going to be a problem. We should push English toward having a single pronoun for everyone: no one will feel dysphoric when hearing it used for them because it will have no gender implications a all.

Some ways cis people can use their privilege to help get gender out of language:

more...
"They" is OK By Me

As part of the process of switching to ungendered pronouns at some point people need to start being ok with using "they" with named referents. I'm really optimistic about how "they" is catching on among genderqueer people, but I'd like to help it along. So: if you want to use singular they to refer to me, feel free! I'm still happy to be refered to with standard male pronouns, but if you'd like to use "they", that's fine too.

Example usage:

  • Jeff lost their hat.
  • When you see Jeff, can you give them this note?
  • Jeff's borrowing a car, so they can drive themself there.

These are currently confusing to most people, so you probably want to think about context and not use them when it's not worth it. Still, the more we hear "they" the better it will sound. For example, two years ago uses of "they" like "whenever I meet a cute boy contra dancing I friend them on facebook" sounded wrong to me, but now those sound fine. more...

Effective Altruism at Your Work

Talking about effective altruism with coworkers can be really awkward. There's potential to come off as "I'm more moral than you", potential for "I think the charities you do support are useless", for "I'm so rich I can afford to give away lots of money", "I'm trying to convert you", "I think you're a bad person because you don't do this", and for just seeming weird. Most of the year I just don't bring it up, but as we get into giving season I feel like I ought to try.

I think most of the social difficulty around effective altruism comes from the demandingness aspect, the idea that to be a good person you should be devoting a substantial effort to making the world better. One way around this is to mostly focus on the effectiveness angle, saying "here's an awesome charity that can do huge amounts of good with your donation". The best charities are so much better than the rest that pushing effectiveness seems well worth it, and any sort of discussion of giving helps normalize it and lead people to give more. So here are three ideas for promoting effective giving among your coworkers: more...

More Posts


Jeff Kaufman  ::  Blog Posts  ::  RSS Feed  ::  Contact  ::  G+ Profile