[英]stb image write issue
I am trying to write a simple image file to disk using stb_image_write. 我正在尝试使用stb_image_write将简单的图像文件写入磁盘。 I use the simplest test case : a 128 by 128 pixels RGB image.
我使用最简单的测试用例:一个128 x 128像素的RGB图像。
I found this example online, and the guy seems to say that it works fine, plus it looks exactly like what I have been writing for 2 hours : 我在网上找到了这个例子,那个家伙似乎说它可以正常工作,而且看起来就像我已经写了2个小时了:
void save_image(int w, int h, int channels_num)
{
int data[w * h * channels_num];
int index = 0;
for (int j = h - 1; j >= 0; --j)
{
for (int i = 0; i < w; ++i)
{
float r = (float)i / (float)w;
float g = (float)j / (float)h;
float b = 0.2f;
int ir = int(255.0 * r);
int ig = int(255.0 * g);
int ib = int(255.0 * b);
data[index++] = ir;
data[index++] = ig;
data[index++] = ib;
}
}
stbi_write_jpg("jpg_test_.jpg", w, h, channels_num, data, 100);
}
save_image(128, 128, 3);
The result should be a nice color gradient, but all I can get is a valid file with some vertical red, green, blue and black lines. 结果应该是一个不错的颜色渐变,但是我所能得到的只是一个有效的文件,其中包含一些垂直的红色,绿色,蓝色和黑色线条。 Dimensions of the image are ok though.
图像的尺寸虽然可以。 I really don't find the solution here.
我真的在这里找不到解决方案。 I am on linux Jessie.
我在Linux Jessie上。 Could there be 'endianess' issue or something like that?
可能会出现“偏执”问题或类似问题吗?
The question was answered in the comments. 评论中回答了这个问题。 I had to replace : int data[] by : unsigned char data[];
我必须将:int data []替换为:unsigned char data [];
=============== This part was not accurate. ===============这部分不准确。 please see my solution part below ====
请在下面查看我的解决方案部分====
int data[w * h * channels_num]; int data [w * h * channels_num]; is ok, that's not the reason you failed.
没关系,这不是您失败的原因。
Change this line 更改此行
stbi_write_jpg("jpg_test_.jpg", w, h, channels_num, data, 100);
to 至
stbi_write_jpg("jpg_test_.jpg", w, h, channels_num, data, w * sizeof(int));
You will get what you want. 您将得到想要的东西。 The last parameters, I believe it is stride.
最后一个参数,我相信这是大步向前。 If so, stride means the offset between two lines.
如果是这样,则步幅表示两行之间的偏移量。 Based on your question, I assume you have 128 X 128 X 32bit color.
根据您的问题,我假设您有128 X 128 X 32位颜色。 Then stride will be w * sizeof(int).
然后跨步将是w * sizeof(int)。
If NOT, let me know. 如果没有,请告诉我。
Sorry I was in a hurry to rush to a meeting. 抱歉,我急着去开会。
============ This part is the solution part ==== ============这部分是解决方案部分====
Yes, 是,
int data[w * h * channels_num];
should be 应该
unsigned char data[w * h * channels_num];
Plus, 加,
stbi_write_jpg("jpg_test_.jpg", w, h, channels_num, data, 100);
should be changed to 应该更改为
stbi_write_jpg("jpg_test_.jpg", w, h, channels_num, data, w * channels_num);
Full solution 完整解决方案
void save_image(int w, int h, int channels_num)
{
unsigned char data[w * h * channels_num];
int index = 0;
for (int j = h - 1; j >= 0; --j)
{
for (int i = 0; i < w; ++i)
{
data[index++] = (unsigned char)(255.0 * i / w);
data[index++] = (unsigned char)(255.0 * j / h);
data[index++] = (unsigned char)(255.0 * 0.2);
}
}
stbi_write_jpg("jpg_test_.jpg", w, h, channels_num, data, w * channels_num);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.