u8x8_capture.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. u8x8_capture.c
  3. Screen capture funcion
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2016, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "u8x8.h"
  29. /*========================================================*/
  30. /* vertical top lsb memory architecture */
  31. uint8_t u8x8_capture_get_pixel_1(uint16_t x, uint16_t y, uint8_t *dest_ptr, uint8_t tile_width)
  32. {
  33. //uint8_t *dest_ptr = capture->buffer;
  34. //if ( dest_ptr == NULL )
  35. //return 0;
  36. //dest_ptr += (y/8)*capture->tile_width*8;
  37. dest_ptr += (y/8)*tile_width*8;
  38. y &= 7;
  39. dest_ptr += x;
  40. if ( (*dest_ptr & (1<<y)) == 0 )
  41. return 0;
  42. return 1;
  43. }
  44. /* horizontal right lsb memory architecture */
  45. /* SH1122, LD7032, ST7920, ST7986, LC7981, T6963, SED1330, RA8835, MAX7219, LS0 */
  46. uint8_t u8x8_capture_get_pixel_2(uint16_t x, uint16_t y, uint8_t *dest_ptr, uint8_t tile_width)
  47. {
  48. //uint8_t *dest_ptr = capture->buffer;
  49. //if ( dest_ptr == NULL )
  50. // return 0;
  51. //dest_ptr += y*capture->tile_width;
  52. y *= tile_width;
  53. dest_ptr += y;
  54. dest_ptr += x>>3;
  55. if ( (*dest_ptr & (128>>(x&7))) == 0 )
  56. return 0;
  57. return 1;
  58. }
  59. void u8x8_capture_write_pbm_pre(uint8_t tile_width, uint8_t tile_height, void (*out)(const char *s))
  60. {
  61. out("P1\n");
  62. out(u8x8_utoa((uint16_t)tile_width*8));
  63. out("\n");
  64. out(u8x8_utoa((uint16_t)tile_height*8));
  65. out("\n");
  66. }
  67. void u8x8_capture_write_pbm_buffer(uint8_t *buffer, uint8_t tile_width, uint8_t tile_height, uint8_t (*get_pixel)(uint16_t x, uint16_t y, uint8_t *dest_ptr, uint8_t tile_width), void (*out)(const char *s))
  68. {
  69. uint16_t x, y;
  70. uint16_t w, h;
  71. w = tile_width;
  72. w *= 8;
  73. h = tile_height;
  74. h *= 8;
  75. for( y = 0; y < h; y++)
  76. {
  77. for( x = 0; x < w; x++)
  78. {
  79. if ( get_pixel(x, y, buffer, tile_width) )
  80. out("1");
  81. else
  82. out("0");
  83. }
  84. out("\n");
  85. }
  86. }
  87. void u8x8_capture_write_xbm_pre(uint8_t tile_width, uint8_t tile_height, void (*out)(const char *s))
  88. {
  89. out("#define xbm_width ");
  90. out(u8x8_utoa((uint16_t)tile_width*8));
  91. out("\n");
  92. out("#define xbm_height ");
  93. out(u8x8_utoa((uint16_t)tile_height*8));
  94. out("\n");
  95. out("static unsigned char xbm_bits[] = {\n");
  96. }
  97. void u8x8_capture_write_xbm_buffer(uint8_t *buffer, uint8_t tile_width, uint8_t tile_height, uint8_t (*get_pixel)(uint16_t x, uint16_t y, uint8_t *dest_ptr, uint8_t tile_width), void (*out)(const char *s))
  98. {
  99. uint16_t x, y;
  100. uint16_t w, h;
  101. uint8_t v, b;
  102. char s[2];
  103. s[1] = '\0';
  104. w = tile_width;
  105. w *= 8;
  106. h = tile_height;
  107. h *= 8;
  108. y = 0;
  109. for(;;)
  110. {
  111. x = 0;
  112. for(;;)
  113. {
  114. v = 0;
  115. for( b = 0; b < 8; b++ )
  116. {
  117. v <<= 1;
  118. if ( get_pixel(x+7-b, y, buffer, tile_width) )
  119. v |= 1;
  120. }
  121. out("0x");
  122. s[0] = (v>>4);
  123. if ( s[0] <= 9 )
  124. s[0] += '0';
  125. else
  126. s[0] += 'a'-10;
  127. out(s);
  128. s[0] = (v&15);
  129. if ( s[0] <= 9 )
  130. s[0] += '0';
  131. else
  132. s[0] += 'a'-10;
  133. out(s);
  134. x += 8;
  135. if ( x >= w )
  136. break;
  137. out(",");
  138. }
  139. y++;
  140. if ( y >= h )
  141. break;
  142. out(",");
  143. out("\n");
  144. }
  145. out("};\n");
  146. }
  147. /*========================================================*/
  148. #ifdef NOT_YET_IMPLEMENTED_U8X8_SCREEN_CAPTURE
  149. struct _u8x8_capture_struct
  150. {
  151. u8x8_msg_cb old_cb;
  152. uint8_t *buffer; /* tile_width*tile_height*8 bytes */
  153. uint8_t tile_width;
  154. uint8_t tile_height;
  155. };
  156. typedef struct _u8x8_capture_struct u8x8_capture_t;
  157. u8x8_capture_t u8x8_capture;
  158. static void u8x8_capture_memory_copy(uint8_t *dest, uint8_t *src, uint16_t cnt)
  159. {
  160. while( cnt > 0 )
  161. {
  162. *dest++ = *src++;
  163. cnt--;
  164. }
  165. }
  166. static void u8x8_capture_DrawTiles(u8x8_capture_t *capture, uint8_t tx, uint8_t ty, uint8_t tile_cnt, uint8_t *tile_ptr)
  167. {
  168. uint8_t *dest_ptr = capture->buffer;
  169. //printf("tile pos: %d %d, cnt=%d\n", tx, ty, tile_cnt);
  170. if ( dest_ptr == NULL )
  171. return;
  172. dest_ptr += (uint16_t)ty*capture->tile_width*8;
  173. dest_ptr += (uint16_t)tx*8;
  174. u8x8_capture_memory_copy(dest_ptr, tile_ptr, tile_cnt*8);
  175. }
  176. uint8_t u8x8_d_capture(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  177. {
  178. if ( msg == U8X8_MSG_DISPLAY_DRAW_TILE )
  179. {
  180. uint8_t x, y, c;
  181. uint8_t *ptr;
  182. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  183. y = ((u8x8_tile_t *)arg_ptr)->y_pos;
  184. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  185. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  186. do
  187. {
  188. u8x8_capture_DrawTiles(&u8x8_capture, x, y, c, ptr);
  189. x += c;
  190. arg_int--;
  191. } while( arg_int > 0 );
  192. }
  193. return u8x8_capture.old_cb(u8x8, msg, arg_int, arg_ptr);
  194. }
  195. uint8_t u8x8_GetCaptureMemoryPixel(u8x8_t *u8x8, uint16_t x, uint16_t y)
  196. {
  197. return u8x8_capture_GetPixel(&u8x8_capture, x, y);
  198. }
  199. /* memory: tile_width*tile_height*8 bytes */
  200. void u8x8_ConnectCapture(u8x8_t *u8x8, uint8_t tile_width, uint8_t tile_height, uint8_t *memory)
  201. {
  202. if ( u8x8->display_cb == u8x8_d_capture )
  203. return; /* do nothing, capture already installed */
  204. u8x8_capture.buffer = memory; /* tile_width*tile_height*8 bytes */
  205. u8x8_capture.tile_width = tile_width;
  206. u8x8_capture.tile_height = tile_height;
  207. u8x8_capture.old_cb = u8x8->display_cb;
  208. u8x8->display_cb = u8x8_d_capture;
  209. return;
  210. }
  211. #endif