Add-in development tools for fx-9860G and fx-CG 50, to use with GCC and gint.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.0KB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <fxg1a.h>
  4. #include <png.h>
  5. /* icon_print(): Show a monochrome 30*17 icon on stdout */
  6. void icon_print(uint8_t const *icon)
  7. {
  8. for(int y = 0; y < 17; y++)
  9. {
  10. for(int x = 0; x < 30; x++)
  11. {
  12. int v = icon[(y << 2) + (x >> 3)] & (0x80 >> (x & 7));
  13. putchar(v ? '#' : ' ');
  14. putchar(v ? '#' : ' ');
  15. }
  16. putchar('\n');
  17. }
  18. }
  19. /* icon_load(): Load a monochrome PNG image into an array */
  20. uint8_t *icon_load(const char *filename, size_t *width, size_t *height)
  21. {
  22. png_image img;
  23. memset(&img, 0, sizeof img);
  24. img.opaque = NULL;
  25. img.version = PNG_IMAGE_VERSION;
  26. png_image_begin_read_from_file(&img, filename);
  27. if(img.warning_or_error)
  28. {
  29. fprintf(stderr, "libpng %s: %s\n", img.warning_or_error == 1
  30. ? "warning": "error", img.message);
  31. if(img.warning_or_error > 1)
  32. {
  33. png_image_free(&img);
  34. return NULL;
  35. }
  36. }
  37. img.format = PNG_FORMAT_GRAY;
  38. void *buffer = calloc(img.width * img.height, 1);
  39. if(!buffer)
  40. {
  41. fprintf(stderr, "error: cannot read %s: %m\n", filename);
  42. png_image_free(&img);
  43. return NULL;
  44. }
  45. png_image_finish_read(&img, NULL, buffer, img.width, NULL);
  46. if(width) *width = img.width;
  47. if(height) *height = img.height;
  48. png_image_free(&img);
  49. return buffer;
  50. }
  51. /* icon_save(): Save an 8-bit array to a PNG image */
  52. int icon_save(const char *filename, uint8_t *input, size_t width,
  53. size_t height)
  54. {
  55. png_image img;
  56. memset(&img, 0, sizeof img);
  57. img.version = PNG_IMAGE_VERSION;
  58. img.width = width;
  59. img.height = height;
  60. img.format = PNG_FORMAT_GRAY;
  61. png_image_write_to_file(&img, filename, 0, input, 0, NULL);
  62. png_image_free(&img);
  63. if(img.warning_or_error)
  64. {
  65. fprintf(stderr, "libpng %s: %s\n", img.warning_or_error == 1
  66. ? "warning": "error", img.message);
  67. if(img.warning_or_error > 1) return 1;
  68. }
  69. return 0;
  70. }
  71. /* icon_conv_8to1(): Convert an 8-bit icon to 1-bit */
  72. uint8_t *icon_conv_8to1(uint8_t const *input, size_t width, size_t height)
  73. {
  74. if(!input) return NULL;
  75. uint8_t *mono = calloc(68, 1);
  76. if(!mono) return NULL;
  77. size_t stride = width;
  78. /* If the image is wider than 30 pixels, ignore columns at the right */
  79. if(width > 30) width = 30;
  80. /* Skip the first line if there is enough rows, because in standard
  81. 30*19 icons, the first and last lines are skipped */
  82. if(height > 17) input += stride, height--;
  83. /* If height is still larger than 17, ignore rows at the bottom */
  84. if(height > 17) height = 17;
  85. /* Then copy individual pixels on the currently-blank image */
  86. for(size_t y = 0; y < height; y++)
  87. for(size_t x = 0; x < width; x++)
  88. {
  89. int offset = (y << 2) + (x >> 3);
  90. int color = input[y * stride + x] < 128;
  91. uint8_t mask = color << (~x & 7);
  92. mono[offset] |= mask;
  93. }
  94. return mono;
  95. }
  96. /* icon_conv_1to8(): Convert an 1-bit icon to 8-bit */
  97. uint8_t *icon_conv_1to8(uint8_t const *mono)
  98. {
  99. uint8_t *data = calloc(30 * 17, 1);
  100. if(!data) return NULL;
  101. for(int y = 0; y < 17; y++)
  102. for(int x = 0; x < 30; x++)
  103. {
  104. int offset = (y << 2) + (x >> 3);
  105. int bit = mono[offset] & (0x80 >> (x & 7));
  106. data[y * 30 + x] = (bit ? 0x00 : 0xff);
  107. }
  108. return data;
  109. }