放大的 PPM 图像(无过滤)

Upscaled PPM image (no filtering)

我遇到的问题是我的PPM图片太小了,想在window中显示时(没有缩放,缩放功能,必须把图片存盘) ,我只是使用一个简单的函数来放大像素(或者只是将它多次写入文件)。但在此之后,图像变得非常混乱(如屏幕截图所示)并且分辨率错误。在过去的 4 个小时里,我尝试了很多事情,但没有任何效果。 对此有任何解决方案,还是我只是遗漏了一些明显的东西或做错了很多事情? 我在 C++ cli Windows Forms (VS) 中使用 PictureBox,但在图像查看器(WinExplorer、IrfanView)中显示相同的内容。

这里是主要代码,size是维度(图片是正方形),color只是表示是否要彩色,pixelsize是放大像素。 我还提供了完整的 git 代码 Here

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();

}

Result

显然我的问题出在 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;
            }
        }