main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <sys/stat.h>
  4. #include <assert.h>
  5. #include <u8g2.h>
  6. #define cimg_display 0 // Prevent CImg from including Xlib and other unneeded stuff
  7. #include <CImg.h>
  8. using namespace cimg_library;
  9. const char * OUT_DIR = "output";
  10. u8g2_t u8g2;
  11. // Convert the buffer of an u8g2 bitmap device to a CImg bitmap
  12. template<typename T>
  13. CImg<T> u8x8_bitmap_to_cimg(u8g2_t *u8g2, unsigned int size_c, T *fg_color, T *bg_color) {
  14. uint16_t width = u8g2_GetDisplayWidth(u8g2);
  15. uint16_t height = u8g2_GetDisplayHeight(u8g2);
  16. u8x8_t *u8x8 = u8g2_GetU8x8(u8g2);
  17. CImg<T> img(width, height, /* size_z */ 1, size_c);
  18. for (uint16_t y = 0; y < height; ++y) {
  19. for (uint16_t x = 0; x < width; ++x) {
  20. T *color = u8x8_GetBitmapPixel(u8x8, x, y) ? fg_color : bg_color;
  21. img.draw_point(x, y, color);
  22. }
  23. }
  24. return img;
  25. }
  26. static uint8_t black[] = {0, 0, 0};
  27. static uint8_t white[] = {255, 255, 255};
  28. static uint8_t grey[] = {128, 128, 128};
  29. static uint8_t red[] = {255, 0, 0};
  30. using Font = CImgList<uint8_t>;
  31. const float OPAQUE = 1;
  32. const unsigned SOLID_LINE = ~0U;
  33. const uint8_t SCREENSHOT_BORDER = 2;
  34. const uint8_t SCREENSHOT_SCALE = 3;
  35. const float ARROW_ANGLE = 30;
  36. const float ARROW_LENGTH = 2 * SCREENSHOT_SCALE + 1; // +1 makes for a more (but not necessarily perfect) symmetrical triangle
  37. auto ANNOTATE_FONT = Font::font(8 * SCREENSHOT_SCALE);
  38. // Draw a non-filled rectangle, using the specified line width (in pixels)
  39. template<typename T>
  40. void draw_outline(CImg<T>& img, int x, int y, int w, int h, const T* color, int line_width = 1) {
  41. for (int i = 0; i < line_width; ++i)
  42. img.draw_rectangle(x + i, y + i, x + w - 1 - i, y + h - 1 - i, color, OPAQUE, SOLID_LINE);
  43. }
  44. enum { ALIGN_LEFT = 0, ALIGN_CENTER = 1, ALIGN_RIGHT = 2};
  45. enum { ALIGN_TOP = 0, ALIGN_MIDDLE = 4, ALIGN_BOTTOM = 8};
  46. // Draw aligned text
  47. template<typename T>
  48. void draw_text(CImg<T>& img, int x, int y, const T* color, Font& font, unsigned align, const char *fmt, ...) {
  49. va_list ap;
  50. va_start(ap, fmt);
  51. char buf[100];
  52. int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
  53. assert(ret >= 0 && (size_t)ret < sizeof(buf));
  54. if (align) {
  55. auto text = CImg<uint8_t>().draw_text(0, 0, "%s", color, 0, OPAQUE, font, buf);
  56. if (align & ALIGN_RIGHT)
  57. x -= text.width();
  58. else if (align & ALIGN_CENTER)
  59. x -= text.width() / 2;
  60. if (align & ALIGN_BOTTOM)
  61. y -= text.height();
  62. else if (align & ALIGN_MIDDLE)
  63. y -= text.height() / 2;
  64. }
  65. img.draw_text(x, y, "%s", color, 0, OPAQUE, font, buf);
  66. va_end(ap);
  67. }
  68. enum {TOP, RIGHT, BOTTOM, LEFT};
  69. struct Annotation {
  70. // Will be called to add annotations to the screenshot
  71. virtual void annotate(CImg<uint8_t>& /*img*/) const { }
  72. // Margins are in screenshot pixels, will be scaled later
  73. // Top, right, bottom, left (like CSS)
  74. size_t margin[4] = {0, 0, 0, 0};
  75. };
  76. int translate_x(float x, const Annotation& ann) {
  77. return (x + ann.margin[LEFT]) * SCREENSHOT_SCALE + SCREENSHOT_BORDER;
  78. }
  79. int translate_y(float y, const Annotation& ann) {
  80. return (y + ann.margin[TOP]) * SCREENSHOT_SCALE + SCREENSHOT_BORDER;
  81. }
  82. int translate_wh(float wh) {
  83. return wh * SCREENSHOT_SCALE;
  84. }
  85. void save_screenshot(const char *dir, const char* name, const Annotation& ann = Annotation()) {
  86. char filename[PATH_MAX];
  87. int ret = snprintf(filename, sizeof(filename), "%s/%s.png", dir, name);
  88. assert(ret >= 0 && (size_t)ret < sizeof(filename));
  89. auto screenshot = u8x8_bitmap_to_cimg<uint8_t>(&u8g2, 3, black, white);
  90. screenshot.resize(screenshot.width() * SCREENSHOT_SCALE, screenshot.height() * SCREENSHOT_SCALE);
  91. // Create a bigger output image
  92. size_t width = screenshot.width() + 2*SCREENSHOT_BORDER + (ann.margin[LEFT] + ann.margin[RIGHT]) * SCREENSHOT_SCALE;
  93. size_t height = screenshot.height() + 2*SCREENSHOT_BORDER + (ann.margin[TOP] + ann.margin[BOTTOM]) * SCREENSHOT_SCALE;
  94. auto output = CImg<uint8_t>(width, height, 1, 3, /* value for all channels */ 255);
  95. // Draw the screenshot in the center
  96. output.draw_image(translate_x(0, ann), translate_y(0, ann), 0, 0, screenshot);
  97. // Draw a 1px border around the screenshot, just inside the
  98. // SCREENSHOT_BORDER area (leaving a bit of white space between
  99. // the border and the screenshot).
  100. draw_outline(output, ann.margin[LEFT] * SCREENSHOT_SCALE, ann.margin[TOP] * SCREENSHOT_SCALE, screenshot.width() + 2 * SCREENSHOT_BORDER, screenshot.height() + 2 * SCREENSHOT_BORDER, grey);
  101. ann.annotate(output);
  102. output.save_png(filename);
  103. printf("Generated %s\n", filename);
  104. }
  105. struct DescribeFigures : public Annotation {
  106. DescribeFigures() {
  107. this->margin[RIGHT] = 30;
  108. this->margin[LEFT] = 30;
  109. this->margin[TOP] = 10;
  110. this->margin[BOTTOM] = 10;
  111. }
  112. virtual void annotate(CImg<uint8_t>& img) const {
  113. // Add annotations. Coordinates are converted by translate_x/y from
  114. // original screenshot pixels to the output screenshot pixels
  115. draw_text(img, translate_x(132, *this), translate_y(15, *this), red, ANNOTATE_FONT, ALIGN_LEFT|ALIGN_MIDDLE, "Box");
  116. img.draw_arrow(translate_x(130, *this), translate_y(15, *this), translate_x(116, *this), translate_y(15, *this), red, OPAQUE, ARROW_ANGLE, ARROW_LENGTH);
  117. draw_text(img, translate_x(-4, *this), translate_y(10, *this), red, ANNOTATE_FONT, ALIGN_RIGHT|ALIGN_MIDDLE, "Circle");
  118. img.draw_arrow(translate_x(-3, *this), translate_y(10, *this), translate_x(8, *this), translate_y(10, *this), red, OPAQUE, ARROW_ANGLE, ARROW_LENGTH);
  119. }
  120. };
  121. int main() {
  122. mkdir(OUT_DIR, 0755);
  123. u8g2_SetupBitmap(&u8g2, &u8g2_cb_r0, 128, 64);
  124. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  125. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  126. u8g2_SetFont(&u8g2, u8g2_font_helvB08_tr);
  127. u8g2_ClearBuffer(&u8g2);
  128. u8g2_DrawCircle(&u8g2, 15, 10, 5, U8G2_DRAW_ALL);
  129. u8g2_DrawBox(&u8g2, 100, 10, 10, 10);
  130. u8g2_SendBuffer(&u8g2);
  131. save_screenshot(OUT_DIR, "plain_screenshot");
  132. save_screenshot(OUT_DIR, "annotated_screenshot", DescribeFigures());
  133. return 0;
  134. }