Monday, February 22, 2016

Fun with numbers...

Fun with numbers...  I just finished writing some software (for the blog server I'm creating) that figures out whether it makes more sense to send a larger image already on the server, or a smaller image made by scaling the larger image down.  Part of doing that required making an estimate of how much computer time it would take to scale down the larger image.  That turns out to be (roughly) proportional to how many pixels on the larger image have to be inspected to create a pixel on the scaled-down image, multiplied by the number of pixels in the scaled-down image.  Got that?  :)

Well, calculating the number of pixels that needed to be inspected turned out to be an interesting problem in geometry and algebra.  It boiled down to this formula:


   t  is the number of pixels in the target (scaled down) image
   x  is the floor of the ratio of source (big image) to target pixel width
   y  is the same ratio minus x

It took me a while to derive that formula, but to my great satisfaction it did a pretty good job at estimating the time.  In my tests it was particularly good with PNG files, and within 20% or so on JPG – way better than I actually need for my purposes.

I don't often get to play with numbers like this for the sorts of software I write.  When I get the chance, it's fun!  Especially when it works :)

No comments:

Post a Comment