qoiconv.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Command line tool to convert between png <> qoi format
  3. Requires "stb_image.h" and "stb_image_write.h"
  4. Compile with:
  5. gcc qoiconv.c -std=c99 -O3 -o qoiconv
  6. Dominic Szablewski - https://phoboslab.org
  7. -- LICENSE: The MIT License(MIT)
  8. Copyright(c) 2021 Dominic Szablewski
  9. Permission is hereby granted, free of charge, to any person obtaining a copy of
  10. this software and associated documentation files(the "Software"), to deal in
  11. the Software without restriction, including without limitation the rights to
  12. use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies
  13. of the Software, and to permit persons to whom the Software is furnished to do
  14. so, subject to the following conditions :
  15. The above copyright notice and this permission notice shall be included in all
  16. copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24. */
  25. #define STB_IMAGE_IMPLEMENTATION
  26. #define STBI_ONLY_PNG
  27. #define STBI_NO_LINEAR
  28. #include "stb_image.h"
  29. #define STB_IMAGE_WRITE_IMPLEMENTATION
  30. #include "stb_image_write.h"
  31. #define QOI_IMPLEMENTATION
  32. #include "qoi.h"
  33. #define STR_ENDS_WITH(S, E) (strcmp(S + strlen(S) - (sizeof(E)-1), E) == 0)
  34. int main(int argc, char **argv) {
  35. if (argc < 3) {
  36. puts("Usage: qoiconv <infile> <outfile>");
  37. puts("Examples:");
  38. puts(" qoiconv input.png output.qoi");
  39. puts(" qoiconv input.qoi output.png");
  40. exit(1);
  41. }
  42. void *pixels = NULL;
  43. int w, h, channels;
  44. if (STR_ENDS_WITH(argv[1], ".png")) {
  45. if(!stbi_info(argv[1], &w, &h, &channels)) {
  46. printf("Couldn't read header %s\n", argv[1]);
  47. exit(1);
  48. }
  49. // Force all odd encodings to be RGBA
  50. if(channels != 3) {
  51. channels = 4;
  52. }
  53. pixels = (void *)stbi_load(argv[1], &w, &h, NULL, channels);
  54. }
  55. else if (STR_ENDS_WITH(argv[1], ".qoi")) {
  56. qoi_desc desc;
  57. pixels = qoi_read(argv[1], &desc, 0);
  58. channels = desc.channels;
  59. w = desc.width;
  60. h = desc.height;
  61. }
  62. if (pixels == NULL) {
  63. printf("Couldn't load/decode %s\n", argv[1]);
  64. exit(1);
  65. }
  66. int encoded = 0;
  67. if (STR_ENDS_WITH(argv[2], ".png")) {
  68. encoded = stbi_write_png(argv[2], w, h, channels, pixels, 0);
  69. }
  70. else if (STR_ENDS_WITH(argv[2], ".qoi")) {
  71. encoded = qoi_write(argv[2], pixels, &(qoi_desc){
  72. .width = w,
  73. .height = h,
  74. .channels = channels,
  75. .colorspace = QOI_SRGB
  76. });
  77. }
  78. if (!encoded) {
  79. printf("Couldn't write/encode %s\n", argv[2]);
  80. exit(1);
  81. }
  82. free(pixels);
  83. return 0;
  84. }