It is currently Thu Nov 23, 2017 8:59 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Interpolation
PostPosted: Sun Sep 16, 2007 5:51 pm 
Offline
User avatar

Joined: Mon Sep 27, 2004 2:13 pm
Posts: 1667
Location: .ma.us
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?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 3:11 am 
Offline

Joined: Wed Sep 13, 2006 12:45 pm
Posts: 55
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;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group