2016-05-16 60 views
0

我遇到的问题是我的PPM图像太小,当我想在窗口中显示它时(没有缩放,可用缩放,并且必须将图像保存在磁盘),我只是使用一个简单的函数来升级像素(或者只是将更多次写入文件)。但在此之后,图像变得非常混乱(如截图所示)并且分辨率错误。过去4小时我尝试了很多东西,但没有任何效果。 有没有解决这个问题,或者我只是错过了一些显而易见的事情,或者做了很多错误的事情? 我在C++ cli Windows窗体(VS)中使用PictureBox,但在图像查看器(WinExplorer,IrfanView)中显示了相同的内容。放大的PPM图像(无滤波)

继承人主要代码,尺寸是尺寸(图像是方形),颜色只是表示它是否想要变为颜色,像素尺寸是否会放大像素。 此外,我提供全程的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

回答

0

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