# Implementing Bicubic Scaling in TGSI

26 Sep 2016This post has been pending for some time now. I have been working on VDPAU state tracker for the past 2 months and have implemented luma keying and bicubic scaling and currently working on lanczos scaling. I will be writing about image scaling algorithms and my implementation of bicubic shader in this post.

# Image Scaling

We are given a image at a particular resolution say 300x300 in the form of a 2D matrix which contains the RGB values. Let’s say we need to scale the image to 3x. So the final output will be a 900x900 matrix. Out of the 900x900 = 810000 values we know 90000(300x300) values but we need to find the remaining values by extrapolating the given values.

The fuction that you use to extrapolate the image determines the type of filter. All the functions us the values of the pixels present in the original image to expolate the pixels. The most basic scaling is the Nearest-neighbor interpolation, as the name suggests the value of the pixel is same as the value of the nearest pixel (which was part of the original image). The other important and widely used method is bilinear interpolation which uses linear interpolation to calculate the pixel values.

# Bicubic Interpolation

As is obivous we are going to use a bicubic fuction for interpolation in the bicubic filter. The specific function that we are going to focus on is cubic Hermite spline (given in the figure below with a = -2).

In a bicubic interpolater we use 16 neighbouring pixels to calculate the pixel value. The value of the pixel not only depends on its neighbouring pixels but also depends on the distance of the neighbouring pixels from the pixel. Bicubic interpolation involves cubic interpolation along x axis applying it on 4 pixel at a time and then applying a cubic interpolation to the resultant of the previous 4 cubic interpolation along the y axis. Confusing right? It will get clear once once we look at the formulas.

### 1-D Cubic Interpolation

For one dimensional cubic interpolation we require 4 points. So given value of pixel at x=-1,0,1 and 2, I can calculate the value of pixel at any position between 0 and 1 by following function p(t):

Where t is the x-coordinate of the point.

### 2-D Bicubic interpolation

For the bicubic interpolation we need to apply cubic interpolation 5 times, 4 along x-axis and one along y-axis.

Enough with the theory let’s look at the code. First we will look at the code for the cubic interpolator i.e. code to calculate p(t). We will divide calculation of p(t) in two parts first we will multiply the pixel values of the four points with the 4x4 matrix and store the result in 4 temp registers.

Easy enough right.

Now let’s look at the code to multiply temp registes with t, which is provided to us as a argument, we will see how to calculate ‘t’ it in the bicubic code. This code is mostly trivial.

The one intriguing thing about the code is the number of temp registers I use to calculate the value this could be easily avoided, that’s what I thought but when I tried reducing there were errors and artifacts in the output. Even now I don’t know the exact reason for the artifacts but using many temp registers cleared the errors.

Now we need to write the code for the bicubic interpolation which calls the above function 5 times. The toughest part of the entire code is to calculate t while calling the cubic interpolator

First we subtract the offset of 0.5 pixel from the coordinate because the pixel center is at 0.5. Now the t_array[21] holds the coordinates divided by the dst width/height. We multiply it with src (width, height) and taking fraction of this value we get the value of t which needed to be in the frame of src coordinates, that explains the reason for multiplying it with src width/height. By applying the FLR on the value in t_array[22] gives us the point which had a pixel value from the original src and not the interpolated one. Now we need 15 more points from the original image for the interpolation. This could be achieved by adding offsets to the original points that we have got so we convert the point to dst coordinates. Finally t_array[22] conatins the refernce point from which the other 15 points will be obatined, we have also added the 0.5 offset to get the value in dst coordinates.

The next part involves adding offsets to the points and storing the texture of this points in the temp array. Now we call our cubic interpolater function 5 times with the respective values.

This completes the post then. I have tried to explain the bicubic interpolation and its implementation in detail but whom am I kidding, I am not that good in explaining, if there is something wrong or something that you haven’t understood please feel free to comment.