0

私はさまざまなディザリング方法を使用してグレースケール グラデーションの実装に取り​​組んできましたが、このタスクでは、グラデーションを左側の黒から水平にする必要があります。画像を水平に回転させようとして、次のことを試しました。

std::reverse(result.begin(), result.end())

また、ベクトルを 2D 配列のように処理しようとしました。

temp = result[i][j];
result[i][j] = result[i][width - 1 - j];
result[i][width - 1 - j] = temp;

これまでのところ、これらの方法はどれも機能していません。ここに私が取り組んでいるコードがあります:

//***headers n stuff***
vector<vector<int>> gradient(int height, int width)
{
    assert(height > 0 && width > 0);

    int cf = height / 255;
    int color = 0;
    vector<vector<int>> result(width, vector<int>(height));
    for (int i = 0; i < height; i += cf)
    {
        for (int j = 0; j < cf; j++)
        {
            fill(result[i + j].begin(), result[i + j].end(), color % 255);
        }
        color--;
    }
    stable_sort(result.begin(), result.end());
    return result;
}

vector<vector<int>> Ordered(int height, int width, vector<vector<int>> result)
{
    int ditherSize = 3;
    int diterLookup[] = { 8, 3, 4, 6, 1, 2, 7, 5, 9 };

    vector<vector<int>> temp(height, vector<int>(width));
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int xlocal = i%ditherSize;
            int ylocal = j%ditherSize;
            int requiredShade = diterLookup[xlocal + ylocal * 3]*255/9;
            if (requiredShade >= result[i][j])
            {
                result[i][j] = 0;
            }
            else {
                result[i][j] = 255;
            }
        }
    }
    return temp;
}


vector<vector<int>> Random(int height, int width, vector<vector<int>> result)
{
    int ditherSize = 3;
    int diterLookup[] = { 8, 3, 4, 6, 1, 2, 7, 5, 9 };

    //vector<vector<int>> result(height, vector<int>(width));
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {

            int requiredShade = rand() % 255;
            if (requiredShade >= result[i][j]) {
                result[i][j] = 0;
            }
            else {
                result[i][j] = 255;
            }
        }
    }
    return result;
}

vector<vector<int>> Floyd_Steinberg(int height, int width, vector<vector<int>> result)
{
    int ditherSize = 3;
    int diterLookup[] = { 8, 3, 4, 6, 1, 2, 7, 5, 9 };

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int oldpixel = result[i][j];
            int newpixel;
            if (oldpixel<=127) {
                newpixel = 0;
            }
            else {
                newpixel = 255;
            }
            result[i][j] = newpixel;
            int quanterror = oldpixel - newpixel;
            if (j < width - 1) {
                result[i][j+1] += quanterror * 7 / 16;
            }
            if (i < height - 1) {
                if (j > 0){
                    result[i + 1][j - 1] += quanterror * 3 / 16;
                }
                result[i+1][j] += quanterror * 5 / 16;
                if (j < width - 1) {
                    result[i + 1][j + 1] += quanterror * 1 / 16;
                }
            }

        }
    }
    return result;
}


vector<vector<int>> JJN(int height, int width, vector<vector<int>> result)
{
    int ditherSize = 3;
    int diterLookup[] = { 8, 3, 4, 6, 1, 2, 7, 5, 9 };

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int oldpixel = result[i][j];
            int newpixel;
            if (oldpixel <= 127) {
                newpixel = 0;
            }
            else {
                newpixel = 255;
            }
            result[i][j] = newpixel;
            int quanterror = oldpixel - newpixel;
            if (j < width - 1) {
                result[i][j + 1] += quanterror * 7 / 48;
                if(j<width-2)
                    result[i][j + 2] += quanterror * 5 / 48;
            }

            if (i < height - 1) {
                if (j > 0) {
                    if (j > 1)
                        result[i + 1][j - 2] += quanterror * 3 / 48;
                    result[i + 1][j - 1] += quanterror * 5 / 48;
                }

                result[i + 1][j] += quanterror * 7 / 48;
                if (j < width - 1) {
                    result[i + 1][j + 1] += quanterror * 5 / 48;
                    if (j < width - 2)
                        result[i + 1][j + 2] += quanterror * 3 / 48;
                }
            }

            if (i < height - 2) {
                if (j > 0) {
                    if(j>1)
                        result[i + 2][j - 2] += quanterror * 1 / 48;
                    result[i + 2][j - 1] += quanterror * 3 / 48;
                }
                result[i + 2][j] += quanterror * 5 / 48;
                if (j < width - 1) {
                    result[i + 2][j + 1] += quanterror * 3 / 48;
                    if (j < width - 2)
                        result[i + 2][j + 2] += quanterror * 1 / 48;
                }
            }

        }
    }
    return result;
}

int main(int argc, char *argv[])
{
    if (argc < 5) {
        cout << "usage:" << endl << "prog.exe <filename> <width> <height> <dithering>"<<endl;
        return 0;
    }
    stringstream w(argv[2]);
    stringstream h(argv[3]);
    stringstream d(argv[4]);
    int numcols, numrows, dithering;

   //***handling error cases ***

    srand(time(0));
    ofstream file;

    file.open(argv[1]);

    if (!file)
    {
        cout << "can't open file" << endl;
        return 0;
    }

    file << "P5" << "\n";

    file << numrows << " " << numcols << "\n";

    file << 255 << "\n";

    vector<vector<int>> pixmap{ gradient(numrows, numcols) };
    switch (dithering) {
    case 1:
        pixmap = Ordered(numrows, numcols, pixmap);
        break;
    case 2:
        pixmap = Random(numrows, numcols, pixmap);
        break;
    case 3:
        pixmap = Floyd_Steinberg(numrows, numcols, pixmap);
        break;
    case 4:
        pixmap = JJN(numrows, numcols, pixmap);
        break;
    default:
        break;
    }
    for_each(pixmap.begin(), pixmap.end(), [&](const auto& v) {
        copy(v.begin(), v.end(), ostream_iterator<char>{file, ""});
    });

    file.close();
}

そして、これがOrdered Dither を使用した結果です

4

1 に答える 1