-1

更新: コードは問題ありません。質問の最後にある編集を参照してください。

指定された画像を拡大縮小して結果を画面に表示する単純なアプリケーションを作成しています。画像の読み込み、表示などは 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):

Edit2: 2 番目の問題は、SDL_Surface ピクセル構造内の 4 バイトの配置でした。24ビット画像でのみ動作することを意図していますが、魅力的なように動作します(ここのコードは更新されています) - 最良の回答へのコメントを参照してください。

4

1 に答える 1

0

私はこの行だと思います:-

index1=(int)(x);

する必要があります:-

index1=(int)(x)*3;

また、ストライドが と同じであると仮定しないでくださいwidth.sizeof(pixel)。行の最初のピクセルが word/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_stride、 とoutput_strideは、連続する行の先頭間のバイト数であり、 とは異なる場合がありますwidth * 3

'3' 定数を変数にして、さまざまな画像形式を処理できるようにすることも検討してください。

于 2014-01-21T11:02:15.900 に答える