ucg_scale.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. ucg_scale.c
  3. Universal uC Color Graphics Library
  4. Copyright (c) 2014, 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. void ucg_UndoScale(ucg_t *ucg)
  29. {
  30. if ( ucg->scale_chain_device_cb != NULL )
  31. {
  32. ucg->device_cb = ucg->scale_chain_device_cb;
  33. ucg->scale_chain_device_cb = NULL;
  34. }
  35. ucg_GetDimension(ucg);
  36. ucg_SetMaxClipRange(ucg);
  37. }
  38. const ucg_fntpgm_uint8_t ucg_scale_2x2[16] UCG_FONT_SECTION("ucg_scale_2x2") =
  39. { 0x00, 0x03, 0x0c, 0x0f, 0x30, 0x33, 0x3c, 0x3f, 0xc0, 0xc3, 0xcc, 0xcf, 0xf0, 0xf3, 0xfc, 0xff };
  40. static void ucg_scale_2x2_send_next_half_byte(ucg_t *ucg, ucg_xy_t *xy, ucg_int_t msg, ucg_int_t len, ucg_int_t dir, uint8_t b)
  41. {
  42. b &= 15;
  43. len *=2;
  44. ucg->arg.pixel.pos = *xy;
  45. switch(dir)
  46. {
  47. case 0: break;
  48. case 1: break;
  49. case 2: ucg->arg.pixel.pos.x++; break;
  50. default: case 3: ucg->arg.pixel.pos.y++; break;
  51. }
  52. ucg->arg.bitmap = ucg_scale_2x2+b;
  53. ucg->arg.len = len;
  54. ucg->arg.dir = dir;
  55. ucg->scale_chain_device_cb(ucg, msg, &(ucg->arg));
  56. ucg->arg.pixel.pos = *xy;
  57. switch(dir)
  58. {
  59. case 0: ucg->arg.pixel.pos.y++; break;
  60. case 1: ucg->arg.pixel.pos.x++; break;
  61. case 2: ucg->arg.pixel.pos.y++; ucg->arg.pixel.pos.x++; break;
  62. default: case 3: ucg->arg.pixel.pos.x++; ucg->arg.pixel.pos.y++; break;
  63. }
  64. ucg->arg.bitmap = ucg_scale_2x2+b;
  65. ucg->arg.len = len;
  66. ucg->arg.dir = dir;
  67. ucg->scale_chain_device_cb(ucg, msg, &(ucg->arg));
  68. switch(dir)
  69. {
  70. case 0: xy->x+=len; break;
  71. case 1: xy->y+=len; break;
  72. case 2: xy->x-=len; break;
  73. default: case 3: xy->y-=len; break;
  74. }
  75. }
  76. ucg_int_t ucg_dev_scale2x2(ucg_t *ucg, ucg_int_t msg, void *data)
  77. {
  78. ucg_xy_t xy;
  79. ucg_int_t len;
  80. ucg_int_t dir;
  81. switch(msg)
  82. {
  83. case UCG_MSG_GET_DIMENSION:
  84. ucg->scale_chain_device_cb(ucg, msg, data);
  85. ((ucg_wh_t *)data)->h /= 2;
  86. ((ucg_wh_t *)data)->w /= 2;
  87. //printf("rw=%d rh=%d\n", ucg->rotate_dimension.w, ucg->rotate_dimension.h);
  88. //printf("aw=%d ah=%d\n", ((ucg_wh_t volatile * volatile )data)->w, ((ucg_wh_t volatile * volatile )data)->h);
  89. //printf("dw=%d dh=%d\n", ucg->dimension.w, ucg->dimension.h);
  90. return 1;
  91. case UCG_MSG_SET_CLIP_BOX:
  92. ((ucg_box_t * )data)->ul.y *= 2;
  93. ((ucg_box_t * )data)->ul.x *= 2;
  94. ((ucg_box_t * )data)->size.h *= 2;
  95. ((ucg_box_t * )data)->size.w *= 2;
  96. //printf("post clipbox x=%d y=%d\n", ((ucg_box_t * )data)->ul.x, ((ucg_box_t * )data)->ul.y);
  97. break;
  98. case UCG_MSG_DRAW_PIXEL:
  99. xy = ucg->arg.pixel.pos;
  100. ucg->arg.pixel.pos.x *= 2;
  101. ucg->arg.pixel.pos.y *= 2;
  102. ucg->scale_chain_device_cb(ucg, msg, data);
  103. ucg->arg.pixel.pos.x++;
  104. ucg->scale_chain_device_cb(ucg, msg, data);
  105. ucg->arg.pixel.pos.y++;
  106. ucg->scale_chain_device_cb(ucg, msg, data);
  107. ucg->arg.pixel.pos.x--;
  108. ucg->scale_chain_device_cb(ucg, msg, data);
  109. ucg->arg.pixel.pos = xy;
  110. return 1;
  111. case UCG_MSG_DRAW_L90SE:
  112. case UCG_MSG_DRAW_L90FX:
  113. xy = ucg->arg.pixel.pos;
  114. len = ucg->arg.len;
  115. dir = ucg->arg.dir;
  116. ucg->arg.pixel.pos.x *= 2;
  117. ucg->arg.pixel.pos.y *= 2;
  118. switch(dir)
  119. {
  120. case 0: break;
  121. case 1: break;
  122. case 2: ucg->arg.pixel.pos.x++; break;
  123. default: case 3: ucg->arg.pixel.pos.y++; break;
  124. }
  125. ucg->arg.len *= 2;
  126. ucg->scale_chain_device_cb(ucg, msg, data);
  127. ucg->arg.pixel.pos = xy;
  128. ucg->arg.pixel.pos.x *= 2;
  129. ucg->arg.pixel.pos.y *= 2;
  130. ucg->arg.len = len*2;
  131. ucg->arg.dir = dir;
  132. switch(dir)
  133. {
  134. case 0: ucg->arg.pixel.pos.y++; break;
  135. case 1: ucg->arg.pixel.pos.x++; break;
  136. case 2: ucg->arg.pixel.pos.y++; ucg->arg.pixel.pos.x++; break;
  137. default: case 3: ucg->arg.pixel.pos.x++; ucg->arg.pixel.pos.y++; break;
  138. }
  139. ucg->scale_chain_device_cb(ucg, msg, data);
  140. ucg->arg.pixel.pos = xy;
  141. ucg->arg.len = len;
  142. ucg->arg.dir = dir;
  143. return 1;
  144. #ifdef UCG_MSG_DRAW_L90TC
  145. case UCG_MSG_DRAW_L90TC:
  146. #endif /* UCG_MSG_DRAW_L90TC */
  147. #ifdef UCG_MSG_DRAW_L90BF
  148. case UCG_MSG_DRAW_L90BF:
  149. #endif /* UCG_MSG_DRAW_L90BF */
  150. #if defined(UCG_MSG_DRAW_L90TC) || defined(UCG_MSG_DRAW_L90BF)
  151. xy = ucg->arg.pixel.pos;
  152. len = ucg->arg.len;
  153. dir = ucg->arg.dir;
  154. ucg->arg.pixel.pos.x *= 2;
  155. ucg->arg.pixel.pos.y *= 2;
  156. {
  157. const unsigned char *b = ucg->arg.bitmap;
  158. ucg_xy_t my_xy = ucg->arg.pixel.pos;
  159. ucg_int_t i;
  160. for( i = 8; i < len; i+=8 )
  161. {
  162. ucg_scale_2x2_send_next_half_byte(ucg, &my_xy, msg, 4, dir, ucg_pgm_read(b)>>4);
  163. ucg_scale_2x2_send_next_half_byte(ucg, &my_xy, msg, 4, dir, ucg_pgm_read(b));
  164. b+=1;
  165. }
  166. i = len+8-i;
  167. if ( i > 4 )
  168. {
  169. ucg_scale_2x2_send_next_half_byte(ucg, &my_xy, msg, 4, dir, ucg_pgm_read(b)>>4);
  170. ucg_scale_2x2_send_next_half_byte(ucg, &my_xy, msg, i-4, dir, ucg_pgm_read(b));
  171. }
  172. else
  173. {
  174. ucg_scale_2x2_send_next_half_byte(ucg, &my_xy, msg, i, dir, ucg_pgm_read(b)>>4);
  175. }
  176. }
  177. ucg->arg.pixel.pos = xy;
  178. ucg->arg.len = len;
  179. ucg->arg.dir = dir;
  180. return 1;
  181. #endif
  182. }
  183. return ucg->scale_chain_device_cb(ucg, msg, data);
  184. }
  185. /* Side-Effects: Update dimension and reset clip range to max */
  186. void ucg_SetScale2x2(ucg_t *ucg)
  187. {
  188. ucg_UndoScale(ucg);
  189. ucg->scale_chain_device_cb = ucg->device_cb;
  190. ucg->device_cb = ucg_dev_scale2x2;
  191. ucg_GetDimension(ucg);
  192. ucg_SetMaxClipRange(ucg);
  193. }