Adam Hooper's Blog

Tags

Tag: Code Show all blog posts

Nov, 2014 back to Jan, 2011: (nothing)

Dec, 2010

How cameras work: interpolation

Posted December 18 in Code

Did you know: digital cameras, from $100 to $10,000, only capture one colour per pixel?

Today's cameras produce vibrant pictures from monochrome sensors. Here's how.

It starts when light hits the back of the camera. This is where the "megapixels" of the camera come in: each pixel, or tiny square of colour, corresponds to one of the camera's millions of sensors. Each sensor says how much light hit it.

Read full story

686 comments

Dec, 2010 back to Feb, 2010: (nothing)

Jan, 2010

Beginning of the End for Flash?

Posted January 27 in Code

The iPad Apple announced today is, from what I understand, an oversized iPhone—which is great, in my opinion, especially considering it's cheaper than analysts expected. But web developers have already noticed that this mega-iPhone is missing one tool: Adobe Flash.

What's Flash? Only the most ubiquitous proprietary format on the Web. It plays videos on YouTube, handles file attachments in GMail, tells interactive stories on the New York Times, energizes punch-the-monkey online ads, and….

And, it's proprietary. Adobe owns the file formats and the only tools that produce and play Flash files properly. That means YouTube, GMail, New York Times, and just about every web browser on the planet depend on Adobe to function, because Adobe has the right to dictate what happens with Flash files.

Flash isn't the only proprietary software out there. Mac OS is proprietary, as is Microsoft Windows. Internet Explorer is proprietary. Microsoft Word is proprietary. But here's the thing: they're commodities. You can replace Microsoft Word with a Google Doc; you can replace Mac OS with Linux. You can't replace Adobe Flash with anything.

Read full story

16 comments

Jan, 2010 back to Dec, 2008: (nothing)

Nov, 2008

Engineering Blog-y Thing

Posted November 11 in Code

There seem to be two aspects to my world these days: real life and engineering. Both vary from stressful to challenging to, at times, rewarding.

Most people who read this blog, I suspect, read it to find out about real life, not about engineering. But I feel I have a fair amount to contribute in the latter category, so I hereby announce the grand opening of The Engineering Section of my website, catering to a new potential group of readers with wildly different interests.

My engineering section is blog-like, but is completely separate from this, my actual blog. I encourage interested software engineers to subscribe to the feed in my Engineering section, as I encourage my current readers to stay tuned here while I write my next proper blog post involving a guy wearing a cat as a hat.

Read full story

20 comments

Nov, 2008 back to Aug, 2008: (nothing)

Jul, 2008

My Code

Posted July 14 in Code

Though I have not spent much effort recreating my old website, one small accomplishment is my "My Code" section. This lets you view some source code I have written for school projects and pet projects. It is ideal for university students and people interested in learning to program. Most of the code was written in C, Java, and Python; and at the time I write this, all of the code was written at least two years ago.

Check it out at http://adamhooper.com/code.

Read full story

18 comments

Jul, 2008 back to Jun, 2008: (nothing)

May, 2008

Website 2.0

Posted May 26 in Code

I am busy rebuilding my website. The new version is better. It is built using Ruby on Rails, which deserves a plug. The hacker in me couldn’t resist writing a blog engine from scratch.

Don’t mind the mess. Not all links behave as they ought to, and I will be putting more content in soon. I figure a website like this is better than one of those animated “Under Construction” websites from the 1990’s.

Read full story

16 comments

May, 2008 back to Apr, 2006: (nothing)

Mar, 2006

WebCT auto-login

Posted March 28 in Code

Even more stuff about McGill's network that I hate! WebCT (the worst classroom management web system thingy) at McGill has a really stupid sign-in page. I used to be able to bookmark the login page and enter by pressing Enter; but when they upgraded to WebCT Vista they removed that feature.

I wrote a Greasemonkey script which automatically clicks the Submit button on the sign-in page. It assumes your password is auto-completed.

It eliminates one of those little things that makes life just a teeny bit worse.

I've only tested it with the Epiphany Greasemonkey extension but I expect it works with Firefox as well.

Read full story

14 comments

McGill Wireless - if-up script

Posted March 22 in Code

Last blog entry I displayed an Epiphany plugin to solve one of my McGill wireless woes. Today I spent about 45 minutes playing with wget and bash, and I came up with a much better solution. It's quite McGill-specific, and unfortunately for many of my friends it won't work on Windows, but here it is:

Dump the following into /etc/network/if-up.d/00mcgill-wireless-login:

<![CDATA[#!/bin/sh

# Configuration
WIRELESS_IFACE=eth1
USERNAME=adam.hooper
PASSWORD=`cat /home/adam/.mcgill-wireless-password`

# Only run this script on the wireless interface
[ "$IFACE" == "$WIRELESS_IFACE" ] || exit 0

# Find our IP address
ADDR=`/sbin/ifconfig "$IFACE" | sed -n -e '/^.*inet addr:([.[:digit:]]*).*/{s//1/;p;q;}'`

# Exit if we're not on the McGill wireless network
host $ADDR | grep -i 'wireless.mcgill.ca' > /dev/null
[ $? -eq 0 ] || exit 0

# Bizarre variables for the authentication page
LOGIN_PAGE=https://wireless-login.mcgill.ca/wireless/webpages/noc-authenticate.php
NAS_HOST=www.noc-cn3000.com
NAS_IP=`host $NAS_HOST | sed -n -e '/^.*has address (.*)/{s//1/;p;q;}'`

# Pretend we're a web browser
wget "$LOGIN_PAGE" -o /dev/null -O /dev/null 
        "--post-data=username=$USERNAME&password=$PASSWORD&button=Login&ipaddress=$ADDR&CN3000_INT_IP=$NAS_IP"]]>

Read full story

14 comments

McGill Wireless

Posted March 21 in Code

At McGill, our wireless network requires a web-based log-in. That is, the first web request results in an authentication web page being presented to the user. After the user types in a valid username and password, some voodoo is performed, and an authentication succeeded page is displayed.

There are two enormous annoyances:

  • No Internet is available over any port until the web page has been visited. I'm constantly frustrated: email, VNC, ssh... none work. I haven't looked into how feasible it is to write an if-up.d script to automate the log-in.
  • After typing in a web page and hitting Enter, the authentication web page intercepts; the original web page the user typed in is never presented. This is especially annoying because the URL of the authentication succeeded page actually contains the originally-typed web page as a GET parameter. Why don't they just use a simple HTTP redirect and save thounsands of students the trouble?

One day a flash hit me, and I realized I could solve the second problem (which I'll admit only exists because of the first, which is still annoying) in ten minutes, using an Epiphany extension. It had been a shameful while since I'd last written one, but my sample-python-location.py example was extremely close to what I wanted.

Read full story

30017 comments

Multiple Projects

Posted March 3 in Code

Back when I started at university, I'd be able to immerse myself in projects. I accomplished quite a few things, one project at a time.

Now things are different. I'm taking only four courses this semester, but three of them have semester-long projects. I am currently working on:

  • An Internet version of the game Pit, using the Twisted Python framework for the Internet part and client. The end goal: code an AI player for the game (language undecided: either C++ or Python, probably).
  • A simulation of a distributed computing network. This involves a simulated local network of processes (using an event queue) as well as a real network component (using MPI). Language: C++.
  • A sticky-notes application. I really like this one. Language: Java.
  • (Work, not school:) A medical-imaging application, using Qt, VTK, and ITK. Language: C++ (and each of the libraries has a different idea of how C++ should be written, leading to acute pains in my neck.)

Switching between projects takes a change in mindset. As soon as I get immersed in one, I discover I've neglected another. It's a real pain. Not only that, but I strongly dislike C++.

Read full story

17 comments

Mar, 2006 back to Jan, 2006: (nothing)

Dec, 2005

Blog category list

Posted December 1 in Code

Ironically, the only time I actaully blog is when I'm hacking at my blog.

I wanted a simple HTML category list for my Pyblosxom blog. I hacked something up quickly, without much regard to maintanability or portability.

<![CDATA[__author__ = "Adam Hooper"
__version__ = "1.0"
__url__ = "http://www.adamhooper.com:4242/blog"
__description__ = "Gives an HTML list of blog categories"

import Pyblosxom.tools
import os.path
import re

def cb_prepare(args):
        request = args['request']
        data = request.getData()

        dirs = Pyblosxom.tools.Walk(request, root='/var/www/pyblosxom',
                                    pattern=re.compile('[^.].*'),
                                    return_folders=True)

        html = '  <ul>
'
        for d in dirs:
                rd = os.path.split(d)[-1]
                html += '   <li><a href="/blog/%s">%s</a></li>
' % (rd, rd)
        html += '  </ul>
'

        data['categoryList'] = html]]>

Read full story

17 comments