放大的PPM图像(无过滤)

Upscaled PPM image (no filtering)

本文关键字:过滤 图像 PPM 放大      更新时间:2023-10-16

我的问题是我的PPM图像太小,当我想在窗口中显示它时(没有缩放功能,我必须将图像保存在磁盘上),我只使用一个简单的函数来提升像素(或者只是将其多次写入文件)。但在这之后,图像变得非常混乱(如屏幕截图所示),分辨率错误。在过去的4个小时里,我尝试了很多方法,但都无济于事。有什么解决办法吗?还是我只是错过了一些显而易见的事情,或者做错了很多事情?我在C++cli Windows窗体(VS)中使用PictureBox,但在图像查看器(WinExplorer、IrfanView)中显示的内容相同。

这是主代码,大小是尺寸(图像是正方形),颜色只是表示它是否想是彩色的,像素化放大了像素。此外,我在这里提供完整的数字代码

int sizemult = size; 
        if (color)
            sizemult *= 3; //3 values of RGB
        std::fstream file;
        file.open("temp.ppm", std::ios::trunc | std::ios::out);
        if (color)
            file << "P3" << std::endl;
        else
            file << "P2" << std::endl;
        file << size*pixelsize << " " << size*pixelsize << std::endl;
        file << 256 << std::endl;
        for (auto i = 0; i < size; i++) {
            for (auto h = 0; h < pixelsize; h++) {
                for (auto k = 0; k < size*sizemult; k++) {
                    for (auto j = 0; j < pixelsize; j++) {
                        file << table[i*size+k] << " ";
                    }
                }
                file << std::endl;
            }
        }

        file.close();
}

结果

显然我的问题出在for循环中。这是经过更正的代码,增加了颜色兼容性。

int sizemult = size;
        if (color)
            sizemult *= 3;
        std::default_random_engine e1(r());
        std::uniform_real_distribution <double> dist(0, 256);
        table = (double*)calloc(size*sizemult, sizeof(double));
        for (auto j = 0; j < size*sizemult  ; j++) {
            table[j] = dist(e1);
            //table[j] = rand() % 256;
        }
        std::fstream file;
        file.open("temp.ppm", std::ios::trunc | std::ios::out);
        if (color)
            file << "P3" << std::endl;
        else
            file << "P2" << std::endl;
        file << size*pixelsize << " " << size*pixelsize << std::endl;
        file << 256 << std::endl;
        for (auto i = 0; i < size; i++) {
            for (auto h = 0; h < pixelsize; h++) {
                for (auto k = 0; k < sizemult; k++) {
                    for (auto j = 0; j < pixelsize; j++) {
                        if (color) {
                            file << (int)table[i*size + k] << " ";
                            file << (int)table[i*size + k+1] << " ";
                            file << (int)table[i*size + k+2] << " ";
                        }
                        else
                            file << (int)table[i*size+k] << " ";
                    }
                    if (color)
                        k += 2;
                }
                file << std::endl;
            }
        }