#include <iostream>
#include "time.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <string>
#include <experimental/filesystem>
#include <fstream>
#define STB_IMAGE_IMPLEMENTATION
#include "C:\Users\tpog\Documents\liberylis\stb_image.h"
__global__ void zrenie(int* itog_voz,unsigned char* data_gpu)
{
itog_voz[blockIdx.x] += data_gpu[blockIdx.x]/10;
}
namespace fs = std::experimental::filesystem;
std::string wstring_to_utf8(const std::wstring& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
std::string string = myconv.to_bytes(str);
return string;
}
int main(void)
{
cudaSetDevice(4);
std::vector<std::wstring> fileList;
for (const fs::path& it : fs::directory_iterator("C:\\Users\\tpog\\Documents\\images")) {
if (fs::is_regular_file(it) && (it.extension() == ".png")) {
fileList.emplace_back(it.c_str());
}
}
std::cout << wstring_to_utf8(fileList[4]);
const char* string = wstring_to_utf8(fileList[4]).c_str();
int w = 1200, h = 800;
unsigned char* data = stbi_load("C:\\Users\\tpog\\Documents\\images\\picture_(1).png", &w, &h, nullptr, 3); // если написать 4 - загрузит с прозрачностью
unsigned char* data_gpu;
cudaMalloc((void**)&data_gpu, 3 * h * w * sizeof(char));
printf("%i", data[3 * (1) + 0]);
printf("%i ", data[3 * (1) + 1]);
printf("%i ", data[3 * (1) + 2]);
std::cout << clock();
cudaMemcpy(data_gpu, data, 3 * h * w * sizeof(char), cudaMemcpyHostToDevice);
//zrenie <<<w * h * 3, 1 >>>(data_gpu);
stbi_image_free(data);
cudaFree(data_gpu);
}
该函数
stbi_load("C:\\Users\\tpog\\Documents\\images\\picture_(1).png", &w, &h, nullptr, 3);
接收图像地址,然后对其进行处理。问题是,当我显式编写链接(而不是字符串变量)时,即 C:\Users\tpog\Documents\images\picture_(1).png,那么一切正常,但是一旦我保存链接到一个变量并通过它,然后突然一切都停止工作,并且没有错误发生,代码成功完成,但是第 49, 50, 51 行,应该打印像素颜色,不打印任何东西
显式指定链接时的输出
const char* string = wstring_to_utf8(fileList[4]).c_str();
不起作用。字符串对象在字符串末尾被销毁,并且变量包含对谁知道什么的引用。