Introduction to 2D Linux Game Programming/Algorithms/Simple Software Transformations/Non Optimized Integer Scaling

Introduction
This algorithm demonstrates scaling an image by one positive integer value in both the x and y axis. A common use for this algorithm would be taking a game made for a GameBoy's low resolution screen and doubling or tripling it before displaying it on a PC's high resolution monitor.

This algorithm keeps the aspect ratio of the original image. Think of watching old TV shows on a widescreen monitor. You can either stretch the image to fit the screen, distorting the original image and making all the actors appear fatter, or you can watch in the original aspect ratio, but then you have black bars on either side of the screen. With this algorithm you'd have the black bars. Princess Peach won't have to worry about why she's gained so much weight!

To understand how this algorithm works, it's important to know how most libraries store image data in memory. Where an image is a two dimensional object, most libraries store the individual pixels of the image in a single dimensional array - a line. The game has to keep track of the height of the image and its width to manipulate the image. Some libraries, like SDL, hide this from you until you need to do something like rotate or scale the image.

Setting up the Problem
To show how this works, we'll scale one 3x3 image. In the following images, each square will represent a single pixel.

Stored in an array, that image looks like:

If you're trying to come up with an algorithm, it's handy to step through the problem a piece at a time. The first thing we're going to do is copy the first pixel three times:

We need to continue copying 3 pixels for every pixel in the source image until we hit the end of the row to properly scale the width of the image:

Then we need to repeat this 3 times to properly scale the height of the image:

Which drawn on the screen looks like: