Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        7个月前关闭。
                                                                                            
                
        
更新:代码现在可以了,请参阅问题末尾的编辑

我正在编写一个简单的应用程序,该应用程序应该缩放给定的图像并将结果显示在屏幕上。通过SDL可以实现图像加载,显示等功能,但是缩放功能仍然存在问题-它会产生乱码。

我必须对24位图像进行操作,因此必须进行uint8_t转换和逐字节计算。

#include <stdint.h>
void blin(uint8_t* pixelsIn, uint8_t* pixelsOut, int w, int h, float scale)
{
    int index1, index2;
    int w2, h2;
    int i, j, k;
    float x, y;
    float t;
    int p1, p2;

    w2 = (int)(scale*w + 0.5);
    h2 = (int)(scale*h + 0.5);
    p1 = w*3;
    if(p1%4) p1 += (4-p1%4);
    p2 = w2*3;
    if(p2%4) p2 += (4-p2%4);
    for(i=0;i<h2;i++) //line
    {
        index2=i*p2;
        for(j=0;j<w2;j++) //column
        {
            x=((float)(j))/scale;
            index1=(int)(x) * 3;
            x-=(int)(x);
            y=((float)(i))/scale;
            index1+=(int)(y) * p1;
            y-=(int)(y);
            for(k=0;k<3;k++) //for color in R, G, B
            {
                t = (float)(pixelsIn[index1]) * (1.0-x)*(1.0-y);
                t += (float)(pixelsIn[index1+3]) * (x)*(1.0-y);
                t += (float)(pixelsIn[index1+p1]) * (1.0-x)*(y);
                t += (float)(pixelsIn[index1+p1+3]) * (x)*(y);
                pixelsOut[index2] = (uint8_t)(t);
                index1++;
                index2++;
            }
        }
    }
}


编辑:明显的错误,index2没有清零并且x的计算没有乘以3(每像素字节)。但是图片仍然没有正确缩放,这是在scale = 1.0之前和之后的(jpg只是为了更快地上传):


之前:http://i.stack.imgur.com/TZLF4.jpg
之后:http://i.stack.imgur.com/IWKfh.jpg


Edit2:第二个问题是SDL_Surface像素结构内部的4字节对齐。现在,它的工作方式就像一个超级按钮(此处的代码已更新),尽管它仅适用于24位图像-请参见注释以获取最佳答案。

最佳答案

我认为这条线:-

index1=(int)(x);


应该:-

index1=(int)(x)*3;


另外,不要假设跨步与width.sizeof(pixel)相同,一行上的第一个像素可能会与单词/ dword / etc边界对齐,因此我将代码更改为:-

void blin(uint8_t* pixelsIn, uint8_t* pixelsOut, int w, int h, float scale, int input_stride, int output_stride)
{
    int index1, index2;
    int w2, h2;
    int i, j, k;
    float x, y;
    float t;

    w2=(int)(scale*w + 0.5);
    h2=(int)(scale*h + 0.5);
    index2=0;
    for(i=0;i<h2;i++) //line
    {
        int pixelindex2=index2;
        for(j=0;j<w2;j++) //column
        {
            x=((float)(j))/scale;
            index1=(int)(x)*3;
            x-=(int)(x);
            y=((float)(i))/scale;
            index1+=(int)(y) * input_stride;
            y-=(int)(y);
            for(k=0;k<3;k++) //for color in R, G, B
            {
                t = (float)(pixelsIn[index1]) * (1.0-x)*(1.0-y);
                t += (float)(pixelsIn[index1+3]) * (x)*(1.0-y);
                t += (float)(pixelsIn[index1+w*3]) * (1.0-x)*(y);
                t += (float)(pixelsIn[index1+w*3+3]) * (x)*(y);
                pixelsOut[pixelindex2] = (uint8_t)(t);
                index1++;
                pixelindex2++;
            }
        } //column
        index2+=output_stride;
    } //line
}


其中,input_strideoutput_stride是连续行开始之间的字节数,可能与width * 3不同。

您可能还需要考虑使“ 3”常量成为变量,以便处理不同的图像格式。

关于c - C-逐字节进行双线性缩放(插值),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21250099/

10-11 22:11
查看更多