2017-05-30 197 views
0

我嘗試使用libnoise改編的世界發生器生成地圖圖塊。使用libnoise保存方法的工作很好,但嘗試擴展和使用CIMG裁剪圖像時,我得到以下錯誤:嘗試使用CImg保存圖像時發生錯誤

[CImg] *** CImgIOException *** cimg::fopen(): Failed to open file '~/tiles/0/0/0.bmp' with mode 'wb'. 

相關方法:

void makeTile(CImg<unsigned char> image, int zoom, int x, int y, int depth, int argc, char* argv[]) { 
    std::string directoryBase = "mkdir -p ~/tiles/" + boost::lexical_cast<std::string>(zoom); 
    std::string directory = directoryBase + "/" + boost::lexical_cast<std::string>(x); 
    std::string filename = "~/tiles/" + boost::lexical_cast<std::string>(zoom) + "/" + boost::lexical_cast<std::string>(x) + "/" + boost::lexical_cast<std::string>(y) + ".bmp"; 

    const char* file_o = cimg_option("-o", filename.c_str(), "Output file"); 
    system(directoryBase.c_str()); 
    system(directory.c_str()); 
    std::cout << "Made required dirs\n"; 
    CImg<unsigned char> imageClone = image.get_resize(256, 256, -100, -100, 1); 

    std::cout << "scaled image\n"; 
    imageClone.save(file_o); 

    if (depth > 0) { 
     int smallX = x * 2; 
     int smallY = y * 2; 
     makeTile(image.get_crop(0, 0, image._width/2, image._height/2), zoom + 1, smallX, smallY, depth - 1, argc, argv); 
     makeTile(image.get_crop(image._width/2, 0, image._width, image._height/2), zoom + 1, smallX + 1, smallY, depth - 1, argc, argv); 
     makeTile(image.get_crop(0, image._height/2, image._width/2, image._height), zoom + 1, smallX, smallY + 1, depth - 1, argc, argv); 
     makeTile(image.get_crop(image._width/2, image._height/2, image._width, image._height), zoom + 1, smallX + 1, smallY + 1, depth - 1, argc, argv); 
    } 
} 

發送到該方法上的圖像第一個循環是從libnoise生成的映射,並使用CImg加載。

回答

1

~/tiles/0/0/0.bmp絕對不是有效的文件名。我懷疑你認爲~將被你的$HOME的值所取代,但事實並非如此(只有在編寫shell命令時才能完成此更換,例如bash)。

相關問題