pixel_driver.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <rtgui/rtgui_system.h>
  2. #include <rtgui/driver.h>
  3. static void _pixeldevice_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  4. {
  5. switch (rtgui_graphic_get_device()->pixel_format)
  6. {
  7. case PIXEL_FORMAT_RGB565:
  8. {
  9. rt_uint16_t pixel;
  10. pixel = rtgui_color_to_565(*c);
  11. rt_device_write(rtgui_graphic_get_device()->device, PIXEL_POSITION(x, y), &pixel,
  12. sizeof(pixel));
  13. }
  14. break;
  15. case PIXEL_FORMAT_RGB888:
  16. {
  17. rt_uint32_t pixel;
  18. pixel = rtgui_color_to_888(*c);
  19. rt_device_write(rtgui_graphic_get_device()->device, PIXEL_POSITION(x, y), &pixel,
  20. 3);
  21. }
  22. break;
  23. }
  24. }
  25. static void _pixeldevice_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  26. {
  27. switch (rtgui_graphic_get_device()->pixel_format)
  28. {
  29. case PIXEL_FORMAT_RGB565:
  30. {
  31. rt_uint16_t pixel;
  32. rt_device_read(rtgui_graphic_get_device()->device, PIXEL_POSITION(x, y), &pixel,
  33. rtgui_graphic_get_device()->byte_per_pixel);
  34. /* get pixel from color */
  35. *c = rtgui_color_from_565(pixel);
  36. }
  37. break;
  38. case PIXEL_FORMAT_RGB888:
  39. {
  40. rt_uint32_t pixel;
  41. rt_device_read(rtgui_graphic_get_device()->device, PIXEL_POSITION(x, y), &pixel,
  42. 3);
  43. /* get pixel from color */
  44. *c = rtgui_color_from_888(pixel);
  45. }
  46. break;
  47. }
  48. }
  49. static void _pixeldevice_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  50. {
  51. rt_ubase_t index;
  52. for (index = x1; index < x2; index ++)
  53. _pixeldevice_set_pixel(c, index, y);
  54. }
  55. static void _pixeldevice_vline(rtgui_color_t *c, rt_base_t x , rt_base_t y1, rt_base_t y2)
  56. {
  57. rt_ubase_t index;
  58. for (index = y1; index < y2; index ++)
  59. _pixeldevice_set_pixel(c, x, index);
  60. }
  61. /* draw raw hline */
  62. static void _pixeldevice_draw_raw_hline(rt_uint8_t *pixels, rt_base_t x1, rt_base_t x2, rt_base_t y)
  63. {
  64. rt_device_write(rtgui_graphic_get_device()->device, PIXEL_POSITION(x1, y), pixels,
  65. (x2 - x1) * rtgui_graphic_get_device()->byte_per_pixel);
  66. }
  67. /* pixel device */
  68. const struct rtgui_graphic_driver_ops _pixeldevice_ops =
  69. {
  70. _pixeldevice_set_pixel,
  71. _pixeldevice_get_pixel,
  72. _pixeldevice_draw_hline,
  73. _pixeldevice_vline,
  74. _pixeldevice_draw_raw_hline,
  75. };
  76. const struct rtgui_graphic_driver_ops *rtgui_pixel_device_get_ops(int pixel_format)
  77. {
  78. return &_pixeldevice_ops;
  79. }