【发布时间】:2014-01-01 16:15:15
【问题描述】:
以下代码是使用双线性插值放大图片。
slow_rescale函数中哪里可以修改,提高效率?
我希望从计算机组织原理的角度对其进行修改。
期待您的回答!
谢谢!
unsigned char *slow_rescale(unsigned char *src, int src_x, int src_y, int dest_x, int dest_y)
{
double step_x,step_y; // Step increase as per instructions above
unsigned char R1,R2,R3,R4; // Colours at the four neighbours
unsigned char G1,G2,G3,G4;
unsigned char B1,B2,B3,B4;
double RT1, GT1, BT1; // Interpolated colours at T1 and T2
double RT2, GT2, BT2;
unsigned char R,G,B; // Final colour at a destination pixel
unsigned char *dst; // Destination image - must be allocated here!
int x,y; // Coordinates on destination image
double fx,fy; // Corresponding coordinates on source image
double dx,dy; // Fractional component of source image coordinates
dst=(unsigned char *)calloc(dest_x*dest_y*3,sizeof(unsigned char)); // Allocate and clear destination image
if (!dst) return(NULL); // Unable to allocate image
step_x=(double)(src_x-1)/(double)(dest_x-1);
step_y=(double)(src_y-1)/(double)(dest_y-1);
for (x=0;x<dest_x;x++) // Loop over destination image
for (y=0;y<dest_y;y++)
{
fx=x*step_x;
fy=y*step_y;
dx=fx-(int)fx;
dy=fy-(int)fy;
getPixel(src,floor(fx),floor(fy),src_x,&R1,&G1,&B1); // get N1 colours
getPixel(src,ceil(fx),floor(fy),src_x,&R2,&G2,&B2); // get N2 colours
getPixel(src,floor(fx),ceil(fy),src_x,&R3,&G3,&B3); // get N3 colours
getPixel(src,ceil(fx),ceil(fy),src_x,&R4,&G4,&B4); // get N4 colours
// Interpolate to get T1 and T2 colours
RT1=(dx*R2)+(1-dx)*R1;
GT1=(dx*G2)+(1-dx)*G1;
BT1=(dx*B2)+(1-dx)*B1;
RT2=(dx*R4)+(1-dx)*R3;
GT2=(dx*G4)+(1-dx)*G3;
BT2=(dx*B4)+(1-dx)*B3;
// Obtain final colour by interpolating between T1 and T2
R=(unsigned char)((dy*RT2)+((1-dy)*RT1));
G=(unsigned char)((dy*GT2)+((1-dy)*GT1));
B=(unsigned char)((dy*BT2)+((1-dy)*BT1));
// Store the final colour
setPixel(dst,x,y,dest_x,R,G,B);
}
return(dst);
}
void getPixel(unsigned char *image, int x, int y, int sx, unsigned char *R, unsigned char *G, unsigned char *B)
{
// Get the colour at pixel x,y in the image and return it using the provided RGB pointers
// Requires the image size along the x direction!
*(R)=*(image+((x+(y*sx))*3)+0);
*(G)=*(image+((x+(y*sx))*3)+1);
*(B)=*(image+((x+(y*sx))*3)+2);
}
void setPixel(unsigned char *image, int x, int y, int sx, unsigned char R, unsigned char G, unsigned char B)
{
// Set the colour of the pixel at x,y in the image to the specified R,G,B
// Requires the image size along the x direction!
*(image+((x+(y*sx))*3)+0)=R;
*(image+((x+(y*sx))*3)+1)=G;
*(image+((x+(y*sx))*3)+2)=B;
}
【问题讨论】:
-
你能显示getPixel()和setPixel()吗?
-
我刚刚编辑了一下,添加了getPixel()和setPixel()的函数。@self.
-
除了完全改变算法之外,您还可以删除一些多余的乘法。在 getPixel:
int x, int y, int sx将((x+(y*sx))*3)+0传递给函数。 -
您可能想在CodeReview 上问这个问题,那里会更合适。
-
-O3?也许分析?然后,尽管由于编译器优化,其中一些可能是浪费/无效的:内联 get/setPixel;更改它们,以便完成一次 32 位从/到 mem 的读/写(注意字节顺序),可能是 4/8 字节对齐(你可以用“R G B 0 R G B 0”而不是“R G B R G B”吗?);一些更多的临时变量(例如对于 floor(fx))......
标签: c interpolation performance