ucg_dev_default_cb.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. ucg_dev_default_cb.c
  3. Universal uC Color Graphics Library
  4. Copyright (c) 2013, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "ucg.h"
  28. /*
  29. default device callback
  30. this should be (finally) called by any other device callback to handle
  31. messages, which are not yet handled.
  32. */
  33. ucg_int_t ucg_dev_default_cb(ucg_t *ucg, ucg_int_t msg, void *data)
  34. {
  35. switch(msg)
  36. {
  37. case UCG_MSG_DRAW_L90SE:
  38. return ucg->ext_cb(ucg, msg, data);
  39. case UCG_MSG_SET_CLIP_BOX:
  40. ucg->clip_box = *(ucg_box_t *)data;
  41. break;
  42. }
  43. return 1; /* all ok */
  44. }
  45. /*
  46. will be used as default cb if no extentions callback is provided
  47. */
  48. ucg_int_t ucg_ext_none(ucg_t *ucg, ucg_int_t msg, void *data)
  49. {
  50. return 1; /* all ok */
  51. }
  52. /*
  53. handle UCG_MSG_DRAW_L90FX message and make calls to "dev_cb" with UCG_MSG_DRAW_PIXEL
  54. return 1 if something has been drawn
  55. */
  56. ucg_int_t ucg_handle_l90fx(ucg_t *ucg, ucg_dev_fnptr dev_cb)
  57. {
  58. if ( ucg_clip_l90fx(ucg) != 0 )
  59. {
  60. ucg_int_t dx, dy;
  61. ucg_int_t i;
  62. switch(ucg->arg.dir)
  63. {
  64. case 0: dx = 1; dy = 0; break;
  65. case 1: dx = 0; dy = 1; break;
  66. case 2: dx = -1; dy = 0; break;
  67. case 3: dx = 0; dy = -1; break;
  68. default: dx = 1; dy = 0; break; /* avoid compiler warning */
  69. }
  70. for( i = 0; i < ucg->arg.len; i++ )
  71. {
  72. dev_cb(ucg, UCG_MSG_DRAW_PIXEL, NULL);
  73. ucg->arg.pixel.pos.x+=dx;
  74. ucg->arg.pixel.pos.y+=dy;
  75. }
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. /*
  81. handle UCG_MSG_DRAW_L90TC message and make calls to "dev_cb" with UCG_MSG_DRAW_PIXEL
  82. return 1 if something has been drawn
  83. */
  84. ucg_int_t ucg_handle_l90tc(ucg_t *ucg, ucg_dev_fnptr dev_cb)
  85. {
  86. if ( ucg_clip_l90tc(ucg) != 0 )
  87. {
  88. ucg_int_t dx, dy;
  89. ucg_int_t i;
  90. unsigned char pixmap;
  91. uint8_t bitcnt;
  92. switch(ucg->arg.dir)
  93. {
  94. case 0: dx = 1; dy = 0; break;
  95. case 1: dx = 0; dy = 1; break;
  96. case 2: dx = -1; dy = 0; break;
  97. case 3: dx = 0; dy = -1; break;
  98. default: dx = 1; dy = 0; break; /* avoid compiler warning */
  99. }
  100. //pixmap = *(ucg->arg.bitmap);
  101. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  102. bitcnt = ucg->arg.pixel_skip;
  103. pixmap <<= bitcnt;
  104. for( i = 0; i < ucg->arg.len; i++ )
  105. {
  106. if ( (pixmap & 128) != 0 )
  107. {
  108. dev_cb(ucg, UCG_MSG_DRAW_PIXEL, NULL);
  109. }
  110. pixmap<<=1;
  111. ucg->arg.pixel.pos.x+=dx;
  112. ucg->arg.pixel.pos.y+=dy;
  113. bitcnt++;
  114. if ( bitcnt >= 8 )
  115. {
  116. ucg->arg.bitmap++;
  117. //pixmap = *(ucg->arg.bitmap);
  118. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  119. bitcnt = 0;
  120. }
  121. }
  122. return 1;
  123. }
  124. return 0;
  125. }
  126. /*
  127. handle UCG_MSG_DRAW_L90FB message and make calls to "dev_cb" with UCG_MSG_DRAW_PIXEL
  128. return 1 if something has been drawn
  129. */
  130. ucg_int_t ucg_handle_l90bf(ucg_t *ucg, ucg_dev_fnptr dev_cb)
  131. {
  132. if ( ucg_clip_l90tc(ucg) != 0 )
  133. {
  134. ucg_int_t dx, dy;
  135. ucg_int_t i, y;
  136. unsigned char pixmap;
  137. uint8_t bitcnt;
  138. switch(ucg->arg.dir)
  139. {
  140. case 0: dx = 1; dy = 0; break;
  141. case 1: dx = 0; dy = 1; break;
  142. case 2: dx = -1; dy = 0; break;
  143. case 3: dx = 0; dy = -1; break;
  144. default: dx = 1; dy = 0; break; /* avoid compiler warning */
  145. }
  146. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  147. bitcnt = ucg->arg.pixel_skip;
  148. pixmap <<= bitcnt;
  149. for( i = 0; i < ucg->arg.len; i++ )
  150. {
  151. for( y = 0; y < ucg->arg.scale; y++ )
  152. {
  153. if ( (pixmap & 128) == 0 )
  154. {
  155. ucg->arg.pixel.rgb = ucg->arg.rgb[1];
  156. dev_cb(ucg, UCG_MSG_DRAW_PIXEL, NULL);
  157. }
  158. else
  159. {
  160. ucg->arg.pixel.rgb = ucg->arg.rgb[0];
  161. dev_cb(ucg, UCG_MSG_DRAW_PIXEL, NULL);
  162. }
  163. ucg->arg.pixel.pos.x+=dx;
  164. ucg->arg.pixel.pos.y+=dy;
  165. }
  166. pixmap<<=1;
  167. bitcnt++;
  168. if ( bitcnt >= 8 )
  169. {
  170. ucg->arg.bitmap++;
  171. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  172. bitcnt = 0;
  173. }
  174. }
  175. return 1;
  176. }
  177. return 0;
  178. }
  179. /*
  180. handle UCG_MSG_DRAW_L90SE message and make calls to "dev_cb" with UCG_MSG_DRAW_PIXEL
  181. return 1 if something has been drawn
  182. */
  183. ucg_int_t ucg_handle_l90se(ucg_t *ucg, ucg_dev_fnptr dev_cb)
  184. {
  185. uint8_t i;
  186. /* Setup ccs for l90se. This will be updated by ucg_clip_l90se if required */
  187. for ( i = 0; i < 3; i++ )
  188. {
  189. ucg_ccs_init(ucg->arg.ccs_line+i, ucg->arg.rgb[0].color[i], ucg->arg.rgb[1].color[i], ucg->arg.len);
  190. }
  191. /* check if the line is visible */
  192. if ( ucg_clip_l90se(ucg) != 0 )
  193. {
  194. ucg_int_t dx, dy;
  195. ucg_int_t i, j;
  196. switch(ucg->arg.dir)
  197. {
  198. case 0: dx = 1; dy = 0; break;
  199. case 1: dx = 0; dy = 1; break;
  200. case 2: dx = -1; dy = 0; break;
  201. case 3: dx = 0; dy = -1; break;
  202. default: dx = 1; dy = 0; break; /* avoid compiler warning */
  203. }
  204. for( i = 0; i < ucg->arg.len; i++ )
  205. {
  206. ucg->arg.pixel.rgb.color[0] = ucg->arg.ccs_line[0].current;
  207. ucg->arg.pixel.rgb.color[1] = ucg->arg.ccs_line[1].current;
  208. ucg->arg.pixel.rgb.color[2] = ucg->arg.ccs_line[2].current;
  209. dev_cb(ucg, UCG_MSG_DRAW_PIXEL, NULL);
  210. ucg->arg.pixel.pos.x+=dx;
  211. ucg->arg.pixel.pos.y+=dy;
  212. for ( j = 0; j < 3; j++ )
  213. {
  214. ucg_ccs_step(ucg->arg.ccs_line+j);
  215. }
  216. }
  217. return 1;
  218. }
  219. return 0;
  220. }
  221. /*
  222. l90rl run lenth encoded constant color pattern
  223. yyllllll wwwwbbbb wwwwbbbb wwwwbbbb wwwwbbbb ...
  224. sequence terminates if wwwwbbbb = 0
  225. wwww: number of pixel to skip
  226. bbbb: number of pixel to draw with color
  227. llllll: number of bytes which follow, can be 0
  228. */
  229. #ifdef ON_HOLD
  230. void ucg_handle_l90rl(ucg_t *ucg, ucg_dev_fnptr dev_cb)
  231. {
  232. ucg_int_t dx, dy;
  233. uint8_t i, cnt;
  234. uint8_t rl_code;
  235. ucg_int_t skip;
  236. switch(ucg->arg.dir)
  237. {
  238. case 0: dx = 1; dy = 0; break;
  239. case 1: dx = 0; dy = 1; break;
  240. case 2: dx = -1; dy = 0; break;
  241. case 3: dx = 0; dy = -1; break;
  242. default: dx = 1; dy = 0; break; /* avoid compiler warning */
  243. }
  244. cnt = ucg_pgm_read(ucg->arg.bitmap);
  245. cnt &= 63;
  246. ucg->arg.bitmap++;
  247. for( i = 0; i < cnt; i++ )
  248. {
  249. rl_code = ucg_pgm_read(ucg->arg.bitmap);
  250. if ( rl_code == 0 )
  251. break;
  252. skip = (ucg_int_t)(rl_code >> 4);
  253. switch(ucg->arg.dir)
  254. {
  255. case 0: ucg->arg.pixel.pos.x+=skip; break;
  256. case 1: ucg->arg.pixel.pos.y+=skip; break;
  257. case 2: ucg->arg.pixel.pos.x-=skip; break;
  258. default:
  259. case 3: ucg->arg.pixel.pos.y-=skip; break;
  260. }
  261. rl_code &= 15;
  262. while( rl_code )
  263. {
  264. dev_cb(ucg, UCG_MSG_DRAW_PIXEL, NULL);
  265. ucg->arg.pixel.pos.x+=dx;
  266. ucg->arg.pixel.pos.y+=dy;
  267. rl_code--;
  268. }
  269. ucg->arg.bitmap++;
  270. }
  271. }
  272. #endif