u8g2_d_tga.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "u8g2.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. //#define FACTOR 3
  6. //#define XOFFSET (FACTOR*32)
  7. //#define YOFFSET (FACTOR*32)
  8. #define DEFAULT_WIDTH (512+512)
  9. #define DEFAULT_HEIGHT (1024+512+256)
  10. uint16_t tga_max_x;
  11. uint16_t tga_max_y;
  12. size_t tga_max_offset = 0;
  13. static uint16_t tga_width;
  14. static uint16_t tga_height;
  15. static uint8_t *tga_data = NULL;
  16. uint8_t tga_r = 0;
  17. uint8_t tga_g = 0;
  18. uint8_t tga_b = 0;
  19. uint8_t tga_desc_r = 0;
  20. uint8_t tga_desc_g = 0;
  21. uint8_t tga_desc_b = 0;
  22. int tga_init(uint16_t w, uint16_t h)
  23. {
  24. tga_max_x = 0;
  25. tga_max_y = 0;
  26. tga_width = 0;
  27. tga_height = 0;
  28. tga_max_offset = 0;
  29. if ( tga_data != NULL )
  30. {
  31. tga_data = (uint8_t *)realloc(tga_data, w*h*3);
  32. }
  33. else
  34. {
  35. tga_data = (uint8_t *)malloc(w*h*3);
  36. }
  37. if ( tga_data == NULL )
  38. return 0;
  39. memset(tga_data, 255, (long)w*(long)h*3L);
  40. tga_width = w;
  41. tga_height = h;
  42. return 1;
  43. }
  44. void tga_set_pixel(uint16_t x, uint16_t y, uint16_t f)
  45. {
  46. uint8_t *p;
  47. uint16_t xx,yy;
  48. size_t offset;
  49. for( yy = y; yy < y+f; yy++ )
  50. {
  51. for( xx = x; xx < x+f; xx++ )
  52. {
  53. if ( yy < tga_height && xx < tga_width )
  54. {
  55. //printf ("(%d %d) ", xx, yy);
  56. offset = (tga_height-yy-1)*tga_width*3 + xx*3;
  57. p = tga_data + offset;
  58. if ( tga_max_offset < offset )
  59. tga_max_offset = offset;
  60. *p++ = tga_b;
  61. *p++ = tga_g;
  62. *p++ = tga_r;
  63. }
  64. }
  65. }
  66. }
  67. void tga_clr_pixel(uint16_t x, uint16_t y, uint16_t f)
  68. {
  69. uint8_t *p;
  70. size_t offset;
  71. uint16_t xx,yy;
  72. for( yy = y; yy < y+f; yy++ )
  73. {
  74. for( xx = x; xx < x+f; xx++ )
  75. {
  76. offset = (tga_height-yy-1)*tga_width*3 + xx*3;
  77. p = tga_data + offset;
  78. *p++ = 255;
  79. *p++ = 255;
  80. *p++ = 255;
  81. }
  82. }
  83. }
  84. void tga_set_8pixel(int x, int y, uint8_t pixel, uint16_t f)
  85. {
  86. int cnt = 8;
  87. while( cnt > 0 )
  88. {
  89. if ( (pixel & 1) != 0 )
  90. {
  91. tga_set_pixel(x,y, f);
  92. }
  93. else
  94. {
  95. tga_clr_pixel(x,y, f);
  96. }
  97. pixel >>= 1;
  98. y+=f;
  99. cnt--;
  100. }
  101. }
  102. void tga_set_multiple_8pixel(int x, int y, int cnt, uint8_t *pixel, uint16_t f)
  103. {
  104. uint8_t b;
  105. while( cnt > 0 )
  106. {
  107. b = *pixel;
  108. tga_set_8pixel(x, y, b, f);
  109. x+=f;
  110. pixel++;
  111. cnt--;
  112. }
  113. }
  114. void tga_write_byte(FILE *fp, uint8_t byte)
  115. {
  116. fputc(byte, fp);
  117. }
  118. void tga_write_word(FILE *fp, uint16_t word)
  119. {
  120. tga_write_byte(fp, word&255);
  121. tga_write_byte(fp, word>>8);
  122. }
  123. void tga_save(const char *name)
  124. {
  125. FILE *fp;
  126. if ( tga_data == NULL )
  127. return;
  128. //printf("tga_save: File %s with %dx%d pixel\n", name, tga_width, tga_height);
  129. fp = fopen(name, "wb");
  130. if ( fp != NULL )
  131. {
  132. tga_write_byte(fp, 0); /* no ID */
  133. tga_write_byte(fp, 0); /* no color map */
  134. tga_write_byte(fp, 2); /* uncompressed true color */
  135. tga_write_word(fp, 0);
  136. tga_write_word(fp, 0);
  137. tga_write_byte(fp, 0);
  138. tga_write_word(fp, 0); /* x origin */
  139. tga_write_word(fp, 0); /* y origin */
  140. tga_write_word(fp, tga_width); /* width */
  141. tga_write_word(fp, tga_height); /* height */
  142. tga_write_byte(fp, 24); /* color depth */
  143. tga_write_byte(fp, 0);
  144. fwrite(tga_data, tga_width*tga_height*3, 1, fp);
  145. tga_write_word(fp, 0);
  146. tga_write_word(fp, 0);
  147. tga_write_word(fp, 0);
  148. tga_write_word(fp, 0);
  149. fwrite("TRUEVISION-XFILE.", 18, 1, fp);
  150. fclose(fp);
  151. }
  152. }
  153. /*==========================================*/
  154. /* tga procedures */
  155. u8x8_display_info_t u8x8_tga_info =
  156. {
  157. /* chip_enable_level = */ 0,
  158. /* chip_disable_level = */ 1,
  159. /* post_chip_enable_wait_ns = */ 0,
  160. /* pre_chip_disable_wait_ns = */ 0,
  161. /* reset_pulse_width_ms = */ 0,
  162. /* post_reset_wait_ms = */ 0,
  163. /* sda_setup_time_ns = */ 0,
  164. /* sck_pulse_width_ns = */ 0,
  165. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  166. /* spi_mode = */ 1,
  167. /* i2c_bus_clock_100kHz = */ 0,
  168. /* data_setup_time_ns = */ 0,
  169. /* write_pulse_width_ns = */ 0,
  170. /* tile_width = */ (DEFAULT_WIDTH)/8,
  171. /* tile_hight = */ (DEFAULT_HEIGHT)/8,
  172. /* default_x_offset = */ 0,
  173. /* flipmode_x_offset = */ 0,
  174. DEFAULT_WIDTH,
  175. DEFAULT_HEIGHT
  176. };
  177. uint8_t u8x8_d_tga(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  178. {
  179. u8g2_uint_t x, y, c;
  180. uint8_t *ptr;
  181. switch(msg)
  182. {
  183. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  184. u8x8_d_helper_display_setup_memory(u8g2, &u8x8_tga_info);
  185. break;
  186. case U8X8_MSG_DISPLAY_INIT:
  187. u8x8_d_helper_display_init(u8g2);
  188. //if ( tga_data == NULL )
  189. tga_init(u8x8_tga_info.pixel_width, u8x8_tga_info.pixel_height);
  190. break;
  191. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  192. break;
  193. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  194. break;
  195. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  196. break;
  197. case U8X8_MSG_DISPLAY_DRAW_TILE:
  198. tga_r = tga_desc_r;
  199. tga_g = tga_desc_g;
  200. tga_b = tga_desc_b;
  201. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  202. //printf("U8X8_MSG_DISPLAY_DRAW_TILE x=%d, ", x);
  203. x *= 8;
  204. x += u8g2->x_offset;
  205. y = ((u8x8_tile_t *)arg_ptr)->y_pos;
  206. //printf("y=%d, c=%d\n", y, ((u8x8_tile_t *)arg_ptr)->cnt);
  207. y *= 8;
  208. do
  209. {
  210. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  211. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  212. tga_set_multiple_8pixel(x, y, c*8, ptr, 1);
  213. arg_int--;
  214. } while( arg_int > 0 );
  215. break;
  216. default:
  217. return 0;
  218. }
  219. return 1;
  220. }
  221. void u8x8_Setup_TGA(u8x8_t *u8x8)
  222. {
  223. /* setup defaults */
  224. u8x8_SetupDefaults(u8x8);
  225. /* setup specific callbacks */
  226. u8x8->display_cb = u8x8_d_tga;
  227. /* setup display info */
  228. u8x8_SetupMemory(u8x8);
  229. }
  230. void u8g2_SetupBuffer_TGA(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb)
  231. {
  232. static uint8_t buf[(DEFAULT_WIDTH)*8*8];
  233. u8x8_Setup_TGA(u8g2_GetU8x8(u8g2));
  234. u8g2_SetupBuffer(u8g2, buf, 8, u8g2_ll_hvline_vertical_top_lsb, u8g2_cb);
  235. }