How to compare two images pixel by pixel

The task might seem trivial, but I can assure you it's not. Smile Every image manipulation program handles file formats a little differently, so if you take an image file (let's assume it uses a lossless format, eg. PNG) and simply save/export it with two different programs, but using the same (lossless) format, the two resulting files will be most probably different in size (and obviously in content too). The question is: how do you know whether the two images are the same or not? The size of the two files can differ for a number of reasons and not just because of the difference of the image data. Eg. modern file formats allow a lot of metadata to be stored with the image. Some file formats allow the use of more than one compression algorithms (eg. TIFF files can use a number of -lossless- compression algorithms).

How to compile ffmpeg (with support for most of the popular codecs) for Mac OS X

OSX Expert's guide on how to compile ffmpeg for the Mac is pretty detailed and a huge help. However since it was written some time ago, quite a few things changed. This post provides an -at the moment- up-to-date version of OSX Expert's instructions. I still use a Snow Leopard (10.6.8) with Xcode 4.2, so small adjustments might be necessary for more recent Mac OS X and/or Xcode versions.

Using sed with curl to grab the filename from the Content-Disposition header

The answer to the Stackoverflow question sounds like this:
Do two requests: a HEAD to get the file name from response header, then a GET:
filename="$(curl -sI  "$url" | grep -o -E 'filename=.*$' | sed -e 's/filename=//')"
curl -o "$filename" -L "$url"

It assumes a pretty simple format for the Content-Disposition header, which is most of the time not true. Meanwhile curl has a new option called --remote-header-name (or just -J), which does exactly what we intend ... ie. takes the filename for the --remote-name option from the Content-Disposition header. Actually even this option of curl was flawed at some point. So to be sure you might take the matter into your own hands and use a proper sed command to fetch the filename from the Content-Disposition line:
filename="$(curl -sIL "$url" | sed -r -e 's/^ *Content-Disposition[ \t]*:[ \t]*[^ \t;]+;[ \t]*filename[ \t]*=[ \t]*("(([^"]|\")*)".*|([^; \t\r"]+)(([^;\r]*[^; \t\r]+)*)[ \t]*(;.*|[\r]?)$)/\2\4\5/' -e 't' -e 'd')"
[ -n "$filename" ] && curl -o "$filename" -L "$url" || curl -OL "$url"

You might have noticed that my regular expression allows for some irregularities in the Content-Disposition header (eg. HTTP headers should not start with a whitespace ... unless it's a folded header which started in a previous line). That's totally intentional.

Port forwarding on Windows with builtin tools (netsh)

"Here is an example on how to use netsh interface portproxy to forward all requests that came to local IP on port 25 to on port 80. Remember to enable IPv6!
  netsh>interface portproxy
  netsh interface portproxy>add v4tov4 listenport=25 connectaddress= connectport=80 protocol=tcp

You can find more information about the commands here.

If it doesn't work you can use a software for this kind of job. For example if you need to use connectaddress= it will not work."

PassPort port forwarding utility Win XP

"PassPort is a simple port forwarding utility. The program runs as an NT Service and can forward various ports from any of local interfaces to whatever remote IP address. It is easily manageable with a simple Windows GUI. Runs on MS Windows XP or newer."

How to estimate the capacity of your Android phone battery

It's a Python script that analyses the log of the currentwidget app. There's a detailed description at the author's site. It comes handy when you buy a new battery. Lets you assess whether the battery is truely new. Smile

How to rotate videos with ffmpeg or VLC

The option to rotate is -vf as in "video filter". Ffmpeg comes with tons of filters, one of them is "transpose". You can rotate a video 90 degrees like this:
ffmpeg -i video.mp4 -acodec copy -sameq -vf transpose=1 out.mp4

The various parameter values for "transpose" are:
  • 0: rotate by 90 degrees counterclockwise and vertically flip
  • 1: rotate by 90 degrees clockwise
  • 2: rotate by 90 degrees counterclockwise
  • 3: rotate by 90 degrees clockwise and vertically flip
To vertically flip the video, use the "vflip" filter. To horizontally flip, use "hflip".
Eg. to rotate the video by 180 degrees you can combine both flips
ffmpeg -i video.mp4 -acodec copy -sameq -vf vflip,hflip out.mp4

You can do similiar rotations in VLC too.

Using the commandline:
vlc --started-from-file video.mp4 --video-filter "rotate{angle=180}"

Using the GUI: Tools / Effects & Filters / Video Effects / Geometry / Transform / Rotate by 180 degrees
(this is based on VLC v2.0.4 for Windows)

Protecting Your Cookies: HttpOnly

A very good and funny writeup on fighting XSS attacks with HttpOnly cookies. I prefer to set session.cookie_httponly = 1 right in the server's php.ini file (for PHP projects). If a project depends on JavaScript access to cookies, then fix the project and not the other way around. Smile

Using Charles Web Debugging Proxy with a custom CA SSL certificate

  1. Generate a new private key and certificate:
    openssl req -x509 -newkey rsa:1024 -keyout charles.key -out charles.crt -days 3650 -nodes
  2. Convert it to PKCS12 format:
    openssl pkcs12 -export -out charles.pfx -inkey charles.key -in charles.crt
  3. Select the *.pfx file in Charles for the custom CA certificate and enter the password (that you specified while converting to the PKCS12 format).
P.S.: note that Charles asks for the certificate's password during every startup, but if you use Charles's builtin certificate, it won't ask you for a password. The builtin certificate is stored in a "keystore" file in charles.jar and the keystore (and key) passphrase are embedded/built into Charles. But if you specify your own certificate and key in a PKCS12 format file, it's passphrase will not be known to Charles (you cannot specify it in Preferences or in the config file itself). And you cannot create a PKCS12 file without a password. And an empty password (/ string) is still a password. Smile Btw. Charles doesn't accept an empty string for the PKCS12 file's password ... thus you've to specify a non-empty password!

Insight on how Google Handwriting works

"Google recently added handwriting recognition capabilities to their web search interface thus giving users an option to scribble search queries without opening the keyboard. Once you turn on the Handwriting mode, the entire Google page turns into a scratch pad – you can write anywhere on the screen and Google will instantly convert your freehand drawing into digital text.

The results are accurate and though the conversion happens on Google’s servers, you won’t notice the delay. Google suggests using block letters but cursive writing works as well."

Syndicate content