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

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

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.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #define STB_IMAGE_IMPLEMENTATION
  7. #include "stb_image.h"
  8. #define STB_IMAGE_WRITE_IMPLEMENTATION
  9. #include "stb_image_write.h"
  10. void zFill(char * dest, int num, int amount) // fills string with zeros
  11. {
  12. char str [10];
  13. sprintf(str, "%d", num);
  14. int len = 0;
  15. while (len < amount - strlen(str))
  16. {
  17. strncat(dest, "0", 2);
  18. len++;
  19. }
  20. strncat(dest, str, strlen(str));
  21. }
  22. void save_frame(int frame, uint8_t * data) // Saves image
  23. {
  24. char file[20];
  25. sprintf(file, "output/");
  26. zFill(file, frame, 6);
  27. strncat(file, ".png", 5);
  28. printf("%s\n", file);
  29. uint8_t image[1280 * 480 * 3]; // output image is upscales 8x
  30. int a = 0, b = 0;
  31. for (int y = 0; y < 60; y++)
  32. {
  33. for (int x = 0; x < 160; x++)
  34. {
  35. for (int add_b = 0; add_b < 8; add_b++)
  36. {
  37. for (int add_a = 0; add_a < 8; add_a++)
  38. {
  39. image[(a + add_a) * (b + add_b) * 1] = data[x * y * 1];
  40. image[(a + add_a) * (b + add_b) * 2] = data[x * y * 2];
  41. image[(a + add_a) * (b + add_b) * 3] = data[x * y * 3];
  42. }
  43. }
  44. a = a + 8;
  45. }
  46. a = 0;
  47. b = b + 8;
  48. }
  49. stbi_write_png(file, 1280, 480, 3, image, 1280 * 3);
  50. }
  51. int main()
  52. {
  53. uint8_t image_data[160 * 60 * 3];
  54. for (int y = 0; y < 60; y++)
  55. {
  56. for (int x = 0; x < 160; x++)
  57. {
  58. image_data[x * y *1] = 128;
  59. image_data[x * y *2] = 128;
  60. image_data[x * y *3] = 128;
  61. }
  62. }
  63. save_frame(1, image_data);
  64. return 0;
  65. }

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

答案1

得分: 0

问题出在数学计算和放大代码上。

修复:image_data[3 * (y * 160 + x) + 2] = 128; 并移除放大代码。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #define STB_IMAGE_IMPLEMENTATION
  6. #include "stb_image.h"
  7. #define STB_IMAGE_WRITE_IMPLEMENTATION
  8. #include "stb_image_write.h"
  9. void save_frame(int frame, uint8_t * data) // 保存图像
  10. {
  11. char file[PATH_MAX];
  12. sprintf (file, "output/%06d.png", frame);
  13. stbi_write_png(file, 160, 60, 3, data, 160 * 3);
  14. }
  15. int main()
  16. {
  17. uint8_t image_data[160 * 60 * 3];
  18. for (int y = 0; y < 60; y++)
  19. {
  20. for (int x = 0; x < 160; x++)
  21. {
  22. image_data[3 * (y * 160 + x)] = 128;
  23. image_data[3 * (y * 160 + x) + 1] = 128;
  24. image_data[3 * (y * 160 + x) + 2] = 128;
  25. }
  26. }
  27. save_frame(1, image_data);
  28. return 0;
  29. }
英文:

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.

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdlib.h&gt;
  3. #include &lt;string.h&gt;
  4. #include &lt;stdint.h&gt;
  5. #define STB_IMAGE_IMPLEMENTATION
  6. #include &quot;stb_image.h&quot;
  7. #define STB_IMAGE_WRITE_IMPLEMENTATION
  8. #include &quot;stb_image_write.h&quot;
  9. void save_frame(int frame, uint8_t * data) // Saves image
  10. {
  11. char file[PATH_MAX];
  12. sprintf (file, &quot;output/%06d.png&quot;, frame);
  13. stbi_write_png(file, 160, 60, 3, data, 160 * 3);
  14. }
  15. int main()
  16. {
  17. uint8_t image_data[160 * 60 * 3];
  18. for (int y = 0; y &lt; 60; y++)
  19. {
  20. for (int x = 0; x &lt; 160; x++)
  21. {
  22. image_data[3 * (y * 160 + x)] = 128;
  23. image_data[3 * (y * 160 + x) + 1] = 128;
  24. image_data[3 * (y * 160 + x) + 2] = 128;
  25. }
  26. }
  27. save_frame(1, image_data);
  28. return 0;
  29. }

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:

确定