Archive for the 'programming' Category

New iPhone version of Digg

Wednesday, July 11th, 2007

Like many other interesting projects the Digg iPhone project started with a conversation over a few beers and a challenge: code it in 48 hours and Kevin would give me a free iPhone. Being the unabashed Apple fanboy I am and, also, being a self-respecting coder I set out to create Digg for the iPhone.  After spending a short time white boarding the application, Daniel mocked up the design and I set off to code it.

Technically speaking, it’s no revolutionary application and I didn’t spend the entire 48 hours working on the application. I did, of course, find time to go see Transformers (awesome) over the weekend. The JavaScript was borrowed from Joe Hewitt and adapted a bit using jQuery. The application iteself is based on our API using the Services_Digg PEAR package I maintain. I’ve been talking with the jQuery team about some limitation in the animations and plan on packaging up a more robust iPhone JavaScript library based on jQuery sometime in the near future (hopefully).

Yesterday Kevin announced the iPhone application and Daniel Burka, our designer at Digg, has covered the details about designing for the iPhone. So far the response to the application has been positive. It was fun and I’m glad everyone is enjoying it. I know I did while riding the bus to work today.

And to answer everyone’s questions. I got an 8GB version last night, the keyboard is interesting, it’s breathtakingly gorgeous and I’ll write YAiPR (Yet Another iPhone Review) soon.

Intelligent image thumbnails

Tuesday, May 29th, 2007

A recent project I’ve been working on required 4:3 thumbnails of all images no matter the original image’s orientation. This required me to do a little math and cropping before actually making the thumbnail. I use the package Image_Transform to figure out which orientation the image is and then crop it appropriately.


$o = Image_Transform::factory('IM');
if (PEAR::isError($o)) {
    return $o;
}


$result = $o->load($ds1);
if (PEAR::isError($result)) {
    return $result;
}


$w = $o->getImageWidth();
$h = $o->getImageHeight();

if ($h > $w) {
    $newWidth = $w;
    $newHeight = ($newWidth * .75);
    $newY = ($h * .15);
    $newX = 0;
} else {
    $newWidth = ($w / 2);
    $newHeight = ($newWidth * .75);
    $newY = ($h / 2) - ($newHeight / 2);
    $newX = ($x / 2) + ($newWidth / 2);
}

$o->crop($newWidth, $newHeight, $newX, $newY);
$o->save($ds2);

The first check checks to see if the image is in landscape or portrait (in portrait the height will be greater than the width). With portrait’s I use the width, multiple that by 0.75 to get my 4:3 ratio and finish by going 15% down from the top of the portrait (assuming that you’re focusing your portrait in the upper portion of the image). With landscape images I simply go outwards from the center of the image (again, assuming you’re focusing your landscape in the center of the image).

A Quick Bitmask HOWTO for Programmers

Monday, June 14th, 2004

Warning: Highly Geeky material follows.

I’m currently working on a large database for a customer. I’m actually redoing a crappy version of the database into a normalized screaming machine. I ran into a problem recently in that some of the values are stored as bitmasks. I knew what a bitmask was, but generally regarded them as voodoo magic left to crazy C hackers. Until now. I contacted my voodoo crazy C hacker mentor, Jeremy Brand, and asked him how they worked.

Here’s a quick tutorial:

instead of counting 0, 1, 2, 3, 4, 5… you could with powers of 2
instead 2^0, 2^1, 2^2, 2^3, 2^4, 2^5… (1, 2, 4, 8, 16, 32…).

When you lable something with one of these powers of two you can
add them to other powers of 2 and then later on divide out what
you started with again.

For example:

apple = 4
orange = 2
banana = 1
———–
sum = 7

The sum of your identifiers is 7 (lets call this $sum).

So, later on you can check 7 to see what is in your basket:

does $sum mod 4 equal 0? (then there is no apple in the cart)
does $sum mod 2 equal 0? (then there is no orange in the cart)
does $sum mod 1 email 0? (then there is no banana in the cart)

Using bitmasks isn’t neccesarily easier to use, but it is fast.
It’s fast because computers already thing in bits. This example
is using 3 bit memory. Typically you’ll have 16 options (like I
have 3 here) because of the size of an integer. If you’re lucky
you’ll be using C or even mysql that can access all 32 bits of an
integer then you’ll have 32 options.

The reason why computers use base-2 to to begin with are because
with storage hardware there is really only two states: on and off.
The more single units that you can store ons and offs the more
storage the device can have.

FYI, you can use google for your calculator:
eg. http://www.google.com/search?q=7+mod+4.

Finally, that makes sense. I’m still completely lost on the actual math that goes into making this work (which irks me), but I did manage to get a proof-of-concept program working, which may help other bitmask deficient programmers out there:

<?php

  // Copyright 2004 Joe Stump <joe@joestump.net>
  // Public Domain
  // Usage: php -q bitmask.php 1 4 32
  //        (any numeric argument will be evaluated - change to whatever)
  echo "Valid bitmask values (up to 16): n";
  for ($i = 1 ; $i < 16 ; ++$i) {
      echo "2 ^ $i = ".pow(2,$i)."n";
  }
  echo "nn";

  $bitmask = 0;
  $values = array();
  for ($i = 1 ; $i < count($argv) ; ++$i) {
      if (is_numeric($argv[$i])) {
          $bitmask += $argv[$i];
          $values[] = $argv[$i];
      }
  }

  echo "Bitmask Contains: ".implode(', ',$values)."n";
  echo "Bitmask Total: ".$bitmask."n";

  echo "nResults:n";
  $arr = array(1,2,4,8,16,32);
  for ($i = 0 ; $i < count($arr) ; ++$i) {
      echo $arr[$i].': '.((($bitmask & $arr[$i]) == 0) ? 'FALSE' : 'TRUE')."n";
  }

?>

The trick is the &. If the result is 0 (zero) then the single bitmask value is not present in the sum of the bitmask. I’m sure this has something with the fact that you cannot add any separate list of single bitmask values and get the same sum twice (ie. 1 + 2 + 4 = 7, 2 + 4 + 8 = 14). Again, I just know “it works[tm]”. I hope this helps someone else out there.

I’m mainly caching this here so the next time my retarded mind can’t wrap itself around bitmasks I can check back to my own site. If you have questions concerning this don’t email me because I still think little green men make this mathematic “trick” work. Damn, I wish I had gotten a Computer Science degree instead of a Computer Info. Sys. degree.

Pyschology of Programming

Monday, April 7th, 2003

A Slashdot thread about the Psychology of Programming really hit home today. After having my almost zendlike coding state shattered I left work in a huff.

Much like authors and artists, programmers suffer from “writers block” and when they are in the mental state to program it’s a fragile state that should be respected. I know wonder how much productivity has been lost because of such interruptions to my “flow” through the years …

Fuckus!

Monday, April 7th, 2003

Psychic: Fuckus!
Brodie: That’s what I’m talking about!
TS: She said “focus”
Brodie: Whatever.

On Saturday I did something I had only heard about in fair tales; I conducted a focus group for our new website. To put it lightly it was mind blowing. It was interesting to see how their clicking habits compared to how you had designed pageflow. Not to mention the things that were totally overlooked.

I had them do a few tasks that I needed users to be able to do with little problem (adding stuff to their cart, signing up for an account, checking out, searching, etc.). It was interesting to see where their eyes focused on the page, what confused them, and what path they followed to find products.

A friend of mine turned me onto what appears to be a wealth of information concerning web user interface. I found a few particularily interesting, such as the Top Ten Web-Design Mistakes of 2002 and Writing for the Web.

WAP, WML

Monday, January 6th, 2003

I’ve been doing some research on WAP and WML. While searching on how to create WBMP image files I ran across a story, written in 2000, that predicted more than half of all web content would be viewed from wireless devices. Here we are in the beginning of 2003 and I’ve still yet to use WAP for anything more than looking up movie times.

Since I recently purchased a chatboard for my T68i I have become interested in programming my own WAP applications. Mainly so I can read my IMAP email and post to this blog from the road at anytime. While figuring out how to do this I found this superb primer on building interactive WAP applications. When I get things working I’ll be sure to let everyone know. I’d have to believe the blog community would be very interested in being able to blog from their cell phones.

Look Ma I modified my source!

Saturday, January 4th, 2003

In true Open Source tradition I found a program that did *almost* what I wanted and modified the source to do what I wanted. Once I had it working I submitted the patch to the maintainer.

The program is called BlackHole and does AV and SPAM checking. I use SpamAssassin for SPAM protection so my main focus was AV. BlackHole was made to work with Qmail, but because of my setup I needed it to output to STDOUT. It currently only wrote to Maildir and mbox. With a little modification I soon had it working. Hopefully, someone actually finds it useful.

Google bucks the trend

Thursday, December 19th, 2002

Everyone knows the coolest way to interface with web services is SOAP. Evidently no one told Google this. They use SOAP for their Google Search API, which is very cool, but decided to use flat tab deliminated files and FTP as it’s interface for Froogle. If you work for a retail shop I highly recommend posting your products there. I can see this becoming a major source of traffic for web stores around the world.

Too much to learn, too little time

Wednesday, December 11th, 2002

GTK+, XML, XUL, HTML, MFC, VB, SQL, UNIX, Windows, VIM, PHP, CSS, C, C++, Ruby, Perl, Assembly, the list goes on and on and on. I’ve dabbled in various technologies throughout my career as a programmer. I actually learned programming logic in my TI-85. From there I started in on PHP with MySQL and Apache as the backend. Why, you’re asking now, do you care?

I just finished reading an excellent article at Joel on Software about how becoming completely proficient in a coding environment is taking longer and longer. I can agree with this. I’ve been programming complex PHP/MySQL applications for over 3 years now and I’m only recently beginning to realize how truly scalable and complex applications are built within this environment.

Joel points out that more and more of the memorization is in how to utilize the various API’s programmed in the language, not the actual language. Since I’ve built an extensive library of code in PHP and utilize other libraries written in PHP, most notably PEAR, I can agree with this.

Joel also makes a great point that learning a language is compounded by the fact that you must also learn that languages environment. PHP’s main environment is UNIX, thus you need to know that Apache barfs on missing directories or that certain shell commands require environment variables to be set properly before they will execute. You need to know how tr, sort, uniq, STDIN, and STDOUT work. If you don’t understand bitmasks and permissions you’re going to spend days figuring out why you can’t write and read files and when you DO figure out how to manipulate permissions you’ll most likely just chmod 777 because it works without any regards to security.

The outline above has largely deterred me from expanding my horizons into new languages. Sure I can hack Perl and I know how to use Visual Basic, but am I proficient? Hardly. Anyone who has looked at any of the open source PHP projects lately will agree with me on this one.

User Interface Design

Tuesday, December 3rd, 2002

I’m the first to admit that I don’t think about User Interface design. In fact, I’m of the mantra that everyone should be as technically savvy as I am. This changed recently when I worked with a newly hired UI person at Care2. A lot of the stuff that seemed like annoying “features” made a lot of sense in the terms she used.

This experience has led me to search around the web for articles on UI techniques. I found an excellent article at Joel on Software called User Interface Design for Programmers. This is the best hands on article I’ve seen so far. Here is a brief overview for those too busy to read the entire article:



  1. A user interface is well-designed when the program behaves exactly how the user thought it would.

    If you think about it this makes a whole lot of sense. If you can learn one thing from this article this is it. Users get frustrated when they encounter programs that don’t perform the way the expect them too. This is why Windows users hate Macs and vice versus, because the UI doesn’t perform the way they are used to UI’s performing.


  2. Every time you provide an option, you’re asking the user to make a decision.

    Programmers love to make extremely flexible programs. The author makes a good point about providing options: Most users do not change the default settings and when they do there is no propagation of those changes in settings. This means even if user does change their settings they have to make those same changes on every computer they use.


  3. Good computer UI uses affordances

    Simply put make your controls appear to be controls. Doors have large bars that afford to be pushed. You should make the buttons and text on your site afford to be pushed.


  4. Users don’t have the manual, and if they did, they wouldn’t read it.

    This is very important for everyone to remember. How many user manuals have you read? I can honestly say I’ve read none. I’ve read tech books, etc., but I have not read a user manual per se. Your program should work as expected and with very little effort on the user’s part.

The articles is much more in depth with perfect real world examples. There are lots of big pictures as well. The article is overall thoughtful and well written. If you program applications for a living this is a must read.


Auto Insurance Stories