如何在纯C中使用stb_image将像素数据写入PNG?

huangapple go评论58阅读模式
英文:

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;
}

This is the result:
如何在纯C中使用stb_image将像素数据写入PNG?

答案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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;stdint.h&gt;
#define STB_IMAGE_IMPLEMENTATION
#include &quot;stb_image.h&quot;
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include &quot;stb_image_write.h&quot;



void save_frame(int frame, uint8_t * data) // Saves image
{
    char file[PATH_MAX];
    sprintf (file, &quot;output/%06d.png&quot;, 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 &lt; 60; y++)
    {
        for (int x = 0; x &lt; 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;
}

huangapple
  • 本文由 发表于 2023年4月17日 02:26:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定