Stb 图像未正确加载数据
Stb Image not loading in data correctly
我正在使用 stb_image.h
库,我的目标是获取 jpg 的每个像素的 rgb 数据。但是当我调用加载函数时,它只给我一堆奇怪的符号而不是实际数字。这可能是图片原因还是我的代码有误?
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int width, height;
int comp;
const char* filename = "C:/Users/graha/source/repos/AiFirsy/Debug/rainbow.jpg";
unsigned char* data = stbi_load(
filename, &width, &height, &comp, 0);
if (data == nullptr) {
std::cerr << "ERROR: Could not load texture image file '" << filename << "'.\n";
width = height = 0;
}
cout << data[0];
}
这是图片
输出流(在本例中为std::cout
)将解释'char' types as characters。如果要查看数值,可以将这些值转换为适当的类型。例如:
unsigned char const value = 'c';
std::cout << value << std::endl;
std::cout << static_cast<unsigned int>(value) << std::endl;
输出:
c
99
此外,顺便说一句,您正在访问 data
,即使它为 null,我猜这不是您想要的。
我正在使用 stb_image.h
库,我的目标是获取 jpg 的每个像素的 rgb 数据。但是当我调用加载函数时,它只给我一堆奇怪的符号而不是实际数字。这可能是图片原因还是我的代码有误?
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int width, height;
int comp;
const char* filename = "C:/Users/graha/source/repos/AiFirsy/Debug/rainbow.jpg";
unsigned char* data = stbi_load(
filename, &width, &height, &comp, 0);
if (data == nullptr) {
std::cerr << "ERROR: Could not load texture image file '" << filename << "'.\n";
width = height = 0;
}
cout << data[0];
}
这是图片
输出流(在本例中为std::cout
)将解释'char' types as characters。如果要查看数值,可以将这些值转换为适当的类型。例如:
unsigned char const value = 'c';
std::cout << value << std::endl;
std::cout << static_cast<unsigned int>(value) << std::endl;
输出:
c
99
此外,顺便说一句,您正在访问 data
,即使它为 null,我猜这不是您想要的。