英文:
How do I write pixel data to PNG using stb_image in plain C?
问题
I am wondering how to write RGB pixel data to a PNG using the stb_image library?
我想知道如何使用stb_image库将RGB像素数据写入PNG文件?
I'm trying to write a uint8_t array that contains all the data.
我尝试写入一个包含所有数据的uint8_t数组。
I start with pixel data for 160x60 and up-scale to 1280x480.
我从160x60的像素数据开始,然后放大到1280x480。
I have made a test code that should make a grey PNG but the image is just random noise.
我已经编写了一个测试代码,应该生成一个灰度PNG图像,但图像只是随机噪音。
英文:
I am wondering how to write RGB pixel data to a PNG using the stb_image library?
I'm trying to write a uint8_t array that contains all the data.
I start with pixel data for 160x60 and up-scale to 1280x480.
I have made a test code that should make a grey PNG but the image is just random noise.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void zFill(char * dest, int num, int amount) // fills string with zeros
{
char str [10];
sprintf(str, "%d", num);
int len = 0;
while (len < amount - strlen(str))
{
strncat(dest, "0", 2);
len++;
}
strncat(dest, str, strlen(str));
}
void save_frame(int frame, uint8_t * data) // Saves image
{
char file[20];
sprintf(file, "output/");
zFill(file, frame, 6);
strncat(file, ".png", 5);
printf("%s\n", file);
uint8_t image[1280 * 480 * 3]; // output image is upscales 8x
int a = 0, b = 0;
for (int y = 0; y < 60; y++)
{
for (int x = 0; x < 160; x++)
{
for (int add_b = 0; add_b < 8; add_b++)
{
for (int add_a = 0; add_a < 8; add_a++)
{
image[(a + add_a) * (b + add_b) * 1] = data[x * y * 1];
image[(a + add_a) * (b + add_b) * 2] = data[x * y * 2];
image[(a + add_a) * (b + add_b) * 3] = data[x * y * 3];
}
}
a = a + 8;
}
a = 0;
b = b + 8;
}
stbi_write_png(file, 1280, 480, 3, image, 1280 * 3);
}
int main()
{
uint8_t image_data[160 * 60 * 3];
for (int y = 0; y < 60; y++)
{
for (int x = 0; x < 160; x++)
{
image_data[x * y *1] = 128;
image_data[x * y *2] = 128;
image_data[x * y *3] = 128;
}
}
save_frame(1, image_data);
return 0;
}
答案1
得分: 0
问题出在数学计算和放大代码上。
修复:image_data[3 * (y * 160 + x) + 2] = 128;
并移除放大代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void save_frame(int frame, uint8_t * data) // 保存图像
{
char file[PATH_MAX];
sprintf (file, "output/%06d.png", frame);
stbi_write_png(file, 160, 60, 3, data, 160 * 3);
}
int main()
{
uint8_t image_data[160 * 60 * 3];
for (int y = 0; y < 60; y++)
{
for (int x = 0; x < 160; x++)
{
image_data[3 * (y * 160 + x)] = 128;
image_data[3 * (y * 160 + x) + 1] = 128;
image_data[3 * (y * 160 + x) + 2] = 128;
}
}
save_frame(1, image_data);
return 0;
}
英文:
The issue was incorrect math and something with the up-scaling code.
Fix: image_data[3 * (y * 160 + x) + 2] = 128;
plus removed up-scaling code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void save_frame(int frame, uint8_t * data) // Saves image
{
char file[PATH_MAX];
sprintf (file, "output/%06d.png", frame);
stbi_write_png(file, 160, 60, 3, data, 160 * 3);
}
int main()
{
uint8_t image_data[160 * 60 * 3];
for (int y = 0; y < 60; y++)
{
for (int x = 0; x < 160; x++)
{
image_data[3 * (y * 160 + x)] = 128;
image_data[3 * (y * 160 + x) + 1] = 128;
image_data[3 * (y * 160 + x) + 2] = 128;
}
}
save_frame(1, image_data);
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论