nesdev.com
https://forums.nesdev.com/

Interpolation
https://forums.nesdev.com/viewtopic.php?f=3&t=3590
Page 1 of 1

Author:  kyuusaku [ Sun Sep 16, 2007 5:51 pm ]
Post subject:  Interpolation

I would like to add interpolation to my programs, but I don't have the background in math necessary to understand the Wikipedia entries on the various methods. Has anyone else like me here found a good website for reference?

Most algorithms also interpolate in 2D when I only want to interpolate 1D scanlines to just approximately achieve the correct pixel aspect ratio. What would be the easiest method for me to implement in this respect?

I know I don't want nearest neighbor, but I think I've come up with an algorithm for it. Could someone confirm this to be correct:

Code:
outpixel[i] = inpixel[round(numofpixin/numofpixout*i)]


In the meantime I've come up with a "common sense" algorithm which just weighs pixels against their subpixel "bias" which I split between the previous and next pixels:
Code:
scalefactor = numofpixout / numofpixin

pixelout[i] =
(pixelin[((int)(i / scalefactor) - 1) & numofpixin] * ((i / scalefactor) % 1) / 2)) +
(pixelin[(int)(i / scalefactor)     ] * (1 - ((i / scalefactor) % 1))) +
(pixelin[((int)(i / scalefactor)+ 1) & numofpixin] * ((i / scalefactor) % 1) / 2))


Anything like conventional linear interpolation?

Author:  Mednafen [ Tue Sep 18, 2007 3:11 am ]
Post subject: 

A quick-and-dirty example of linear interpolation that works for:
(output_width / input_width) >= 0.5

(There may be a few bugs, at it's untested, but the general concept is good ;)).

Also, your input pixel buffer should at least have a pitch of (input_width + 1) pixels, and on each line do:

input_line[input_width] = input_line[input_width - 1];

so that the color of the rightmost pixels in the output will be correct.

Code:
uint32_t step = (uint32_t)((uint64_t)65536 * input_width / output_width);
uint32_t pos = 0;

for(unsigned int i = 0; i < output_width; i++)
{
 uint32_t r,g,b;

 r = pixel[pos >> 16].r * (0x10000 - (pos & 0xFFFF));
 r+= pixel[(pos >> 16) + 1].r * (pos & 0xFFFF);

 g = pixel[pos >> 16].g * (0x10000 - (pos & 0xFFFF));
 g+= pixel[(pos >> 16) + 1].g * (pos & 0xFFFF);

 b = pixel[pos >> 16].b * (0x10000 - (pos & 0xFFFF));
 b+= pixel[(pos >> 16) + 1].b * (pos & 0xFFFF);

 out_pixel[i].r = r >> 16;
 out_pixel[i].g = g >> 16;
 out_pixel[i].b = b >> 16;

 pos += step;
}

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/