Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用 我用C++编写了一个程序,计算曼德尔布罗特设置< /C>。现在我想将其可视化(保存在图片中)。但是当我试图保存一张64k图片时,出现了一些问题。那么,保存像素图片的最佳方法是什么,或者至少是将其可视化_C++_Image_Visual C++_Sfml_Mandelbrot - Fatal编程技术网

使用 我用C++编写了一个程序,计算曼德尔布罗特设置< /C>。现在我想将其可视化(保存在图片中)。但是当我试图保存一张64k图片时,出现了一些问题。那么,保存像素图片的最佳方法是什么,或者至少是将其可视化

使用 我用C++编写了一个程序,计算曼德尔布罗特设置< /C>。现在我想将其可视化(保存在图片中)。但是当我试图保存一张64k图片时,出现了一些问题。那么,保存像素图片的最佳方法是什么,或者至少是将其可视化,c++,image,visual-c++,sfml,mandelbrot,C++,Image,Visual C++,Sfml,Mandelbrot,编辑: 当我想要创建一个例如64K(61440*34560)的图像时,将出现错误“在0x0位置写入时访问冲突…”(最初是德语,翻译后的),程序停止。此错误以非常高的分辨率出现。在较低的分辨率下,程序按预期工作 #include <SFML\Graphics.hpp> #include <stdlib.h> #include <complex> #include <cmath> #include <thread> //4K : 38

编辑:

当我想要创建一个例如
64K
(61440*34560)的图像时,将出现错误“在
0x0
位置写入时访问冲突…”(最初是德语,翻译后的),程序停止。此错误以非常高的分辨率出现。在较低的分辨率下,程序按预期工作

#include <SFML\Graphics.hpp>
#include <stdlib.h>
#include <complex>
#include <cmath>
#include <thread>

//4K :   3840 *  2160
//8K :   7680 *  4320
//16K:  15360 *  8640
//32K:  30720 * 17280
//64K:  61440 * 34560
//128K:122880 * 69120


const unsigned long width  = 61440; //should be dividable by ratioX & numberOfThreads!
const unsigned long height = 34560; //should be dividable by ratioY & numberOfThreads!

const unsigned int maxIterations = 500;

const unsigned int numberOfThreads = 6;

const int maxWidth = width / 3;
const int maxHeight = height / 2;
const int minWidth = -maxWidth * 2;
const int minHeight = -maxHeight;

const double ratioX = 3.0 / width;
const double ratioY = 2.0 / height;

sf::Image img = sf::Image();


int getsGreaterThan2(std::complex<double> z, int noIterations) {
    double result;
    std::complex<double> zTmp = z;
    std::complex<double> c = z;
    for (int i = 1; i != noIterations; i++) {
        zTmp = std::pow(z, 2) + c;
        if (zTmp == z) {
            return 0;
        }
        z = std::pow(z, 2) + c;
        result = std::sqrt(std::pow(z.real(), 2) + std::pow(z.imag(), 2));
        if (result > 2) {
            return i;
        }
    }
    return 0;
}


void fillPixelArrayThreadFunc(int noThreads, int threadNr) { //threadNr ... starts from 0

    double imgNumber;
    double realNumber;

    double tmp;

    long startWidth = ((double)width) / noThreads * threadNr + minWidth;
    long endWidth = startWidth + width / noThreads;

    for (long x = startWidth; x < endWidth; x++) {
        imgNumber = x * ratioX;
        for (long y = minHeight; y < maxHeight; y++) {
            realNumber = y * ratioY;
            long xArray = x - minWidth;
            long yArray = y - minHeight;
            tmp = getsGreaterThan2(std::complex<double>(imgNumber, realNumber), maxIterations);
            if (tmp == 0) {
                img.setPixel(xArray, yArray, sf::Color(0, 0, 0, 255));
            }
            else {
                img.setPixel(xArray, yArray, sf::Color(tmp / maxIterations * 128, tmp / maxIterations * 128, tmp / maxIterations * 255, 255));
            }
        }
    }
}

int main() {
    img.create(width, height, sf::Color::Black);

    std::thread *threads = new std::thread[numberOfThreads];
    for (int i = 0; i < numberOfThreads; i++) {
        threads[i] = std::thread(std::bind(fillPixelArrayThreadFunc, numberOfThreads, i));
    }
    for (int i = 0; i < numberOfThreads; i++) {
        threads[i].join();
    }

    img.saveToFile("filename.png");
    return 1;
}
#包括
#包括
#包括
#包括
#包括
//4K:3840*2160
//8K:7680*4320
//16K:15360*8640
//32K:30720*17280
//64K:61440*34560
//128K:122880*69120
常量无符号长宽=61440//应按ratioX和numberOfThreads进行划分!
常数无符号长高度=34560//应该可以按比率和线程数进行划分!
常量无符号整数最大迭代次数=500;
const unsigned int numberOfThreads=6;
const int maxWidth=宽度/3;
const int maxHeight=高度/2;
const int minWidth=-maxWidth*2;
const int minHeight=-maxHeight;
常数双比值=3.0/宽度;
常数双比值=2.0/高度;
sf::Image img=sf::Image();
int getsgreaterhan2(std::complex z,int noIterations){
双重结果;
std::复数zTmp=z;
std::复合物c=z;
for(int i=1;i!=noIterations;i++){
zTmp=std::pow(z,2)+c;
如果(zTmp==z){
返回0;
}
z=std::pow(z,2)+c;
结果=std::sqrt(std::pow(z.real(),2)+std::pow(z.imag(),2));
如果(结果>2){
返回i;
}
}
返回0;
}
void fillPixelArrayThreadFunc(int-noThreads,int-threadNr){//threadNr…从0开始
双imgNumber;
双重实数;
双tmp;
长起始宽度=((双)宽度)/noThreads*threadNr+minWidth;
长端宽度=起始宽度+宽度/宽度;
用于(长x=startWidth;x
您的程序在调用img.create时失败(宽度、高度、sf::颜色::黑色)

当您进入
sf::Image::create
函数时,您将在这里创建
新像素
向量,当
宽度*高度
太大时,这将失败,如您的情况:

////////////////////////////////////////////////////////////
void Image::create(unsigned int width, unsigned int height, const Color& color)
{
    if (width && height)
    {
        // Create a new pixel buffer first for exception safety's sake
        std::vector<Uint8> newPixels(width * height * 4);
                                     ^61440* ^34560 = 8'493'465'600 bytes !!
////////////////////////////////////////////////////////////
void Image::create(无符号整数宽度、无符号整数高度、常量颜色和颜色)
{
if(宽度和高度)
{
//为了异常安全起见,首先创建一个新的像素缓冲区
std::矢量新像素(宽度*高度*4);
^61440*^34560=8'493'465'600字节!!

结论:SFML无法处理巨大的图像。

欢迎访问stackoverflow.com。请花些时间阅读,尤其是名为和的部分。还有请和。最后,请学习如何创建一个。当我试图保存一张64k的图片时,出现了一些问题……这些问题到底是什么?你不能指望任何人给出有效的答案用这样一个模糊的描述。你保存的图片格式是什么:JPEG?TIFF?PNG?GIF?BMP?WMF?你使用的格式有大小限制吗?很抱歉解释得不好。这只是一个如何正确保存的一般问题(哪种文件格式/方法)。但我很快就会上传我的代码。你是用调试器运行的吗?谢谢!我想我必须找到一种不同的方式来可视化它。创建几个较小的图像并将它们缝合在一起可能是一种方法。但我仍然必须使用其他方法将它们缝合在一起,因为即使我从较小的图像开始,末端的大小-图像不会改变是的,你需要其他东西。无论如何,这不是小事。也许会有帮助。