image_write_test.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifdef __clang__
  2. #define STBIWDEF static inline
  3. #endif
  4. #define STB_IMAGE_WRITE_STATIC
  5. #define STB_IMAGE_WRITE_IMPLEMENTATION
  6. #include "stb_image_write.h"
  7. // using an 'F' since it has no rotational symmetries, and 6x5
  8. // because it's a small, atypical size likely to trigger edge cases.
  9. //
  10. // conveniently, it's also small enough to fully fit inside a typical
  11. // directory listing thumbnail, which simplifies checking at a glance.
  12. static const char img6x5_template[] =
  13. ".****."
  14. ".*...."
  15. ".***.."
  16. ".*...."
  17. ".*....";
  18. void image_write_test(void)
  19. {
  20. // make a RGB version of the template image
  21. // use red on blue to detect R<->B swaps
  22. unsigned char img6x5_rgb[6*5*3];
  23. float img6x5_rgbf[6*5*3];
  24. int i;
  25. for (i = 0; i < 6*5; i++) {
  26. int on = img6x5_template[i] == '*';
  27. img6x5_rgb[i*3 + 0] = on ? 255 : 0;
  28. img6x5_rgb[i*3 + 1] = 0;
  29. img6x5_rgb[i*3 + 2] = on ? 0 : 255;
  30. img6x5_rgbf[i*3 + 0] = on ? 1.0f : 0.0f;
  31. img6x5_rgbf[i*3 + 1] = 0.0f;
  32. img6x5_rgbf[i*3 + 2] = on ? 0.0f : 1.0f;
  33. }
  34. stbi_write_png("output/wr6x5_regular.png", 6, 5, 3, img6x5_rgb, 6*3);
  35. stbi_write_bmp("output/wr6x5_regular.bmp", 6, 5, 3, img6x5_rgb);
  36. stbi_write_tga("output/wr6x5_regular.tga", 6, 5, 3, img6x5_rgb);
  37. stbi_write_jpg("output/wr6x5_regular.jpg", 6, 5, 3, img6x5_rgb, 95);
  38. stbi_write_hdr("output/wr6x5_regular.hdr", 6, 5, 3, img6x5_rgbf);
  39. stbi_flip_vertically_on_write(1);
  40. stbi_write_png("output/wr6x5_flip.png", 6, 5, 3, img6x5_rgb, 6*3);
  41. stbi_write_bmp("output/wr6x5_flip.bmp", 6, 5, 3, img6x5_rgb);
  42. stbi_write_tga("output/wr6x5_flip.tga", 6, 5, 3, img6x5_rgb);
  43. stbi_write_jpg("output/wr6x5_flip.jpg", 6, 5, 3, img6x5_rgb, 95);
  44. stbi_write_hdr("output/wr6x5_flip.hdr", 6, 5, 3, img6x5_rgbf);
  45. }
  46. #ifdef IWT_TEST
  47. int main(int argc, char **argv)
  48. {
  49. image_write_test();
  50. return 0;
  51. }
  52. #endif