You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
4.5 KiB

  1. // Copyright 2021 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include "qp_internal.h"
  5. #include "qp_stream.h"
  6. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7. // Quantum Painter utility functions
  8. // Global variable used for native pixel data streaming.
  9. extern uint8_t qp_internal_global_pixdata_buffer[QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE];
  10. // Check if the supplied bpp is capable of being rendered
  11. bool qp_internal_bpp_capable(uint8_t bits_per_pixel);
  12. // Returns the number of pixels that can fit in the pixdata buffer
  13. uint32_t qp_internal_num_pixels_in_buffer(painter_device_t device);
  14. // Fills the supplied buffer with equivalent native pixels matching the supplied HSV
  15. void qp_internal_fill_pixdata(painter_device_t device, uint32_t num_pixels, uint8_t hue, uint8_t sat, uint8_t val);
  16. // qp_setpixel internal implementation, but uses the global pixdata buffer with pre-converted native pixel. Only the first pixel is used.
  17. bool qp_internal_setpixel_impl(painter_device_t device, uint16_t x, uint16_t y);
  18. // qp_rect internal implementation, but uses the global pixdata buffer with pre-converted native pixels.
  19. bool qp_internal_fillrect_helper_impl(painter_device_t device, uint16_t l, uint16_t t, uint16_t r, uint16_t b);
  20. // Convert from input pixel data + palette to equivalent pixels
  21. typedef int16_t (*qp_internal_byte_input_callback)(void* cb_arg);
  22. typedef bool (*qp_internal_pixel_output_callback)(qp_pixel_t* palette, uint8_t index, void* cb_arg);
  23. bool qp_internal_decode_palette(painter_device_t device, uint32_t pixel_count, uint8_t bits_per_pixel, qp_internal_byte_input_callback input_callback, void* input_arg, qp_pixel_t* palette, qp_internal_pixel_output_callback output_callback, void* output_arg);
  24. bool qp_internal_decode_grayscale(painter_device_t device, uint32_t pixel_count, uint8_t bits_per_pixel, qp_internal_byte_input_callback input_callback, void* input_arg, qp_internal_pixel_output_callback output_callback, void* output_arg);
  25. bool qp_internal_decode_recolor(painter_device_t device, uint32_t pixel_count, uint8_t bits_per_pixel, qp_internal_byte_input_callback input_callback, void* input_arg, qp_pixel_t fg_hsv888, qp_pixel_t bg_hsv888, qp_internal_pixel_output_callback output_callback, void* output_arg);
  26. // Global variable used for interpolated pixel lookup table.
  27. #if QUANTUM_PAINTER_SUPPORTS_256_PALETTE
  28. extern qp_pixel_t qp_internal_global_pixel_lookup_table[256];
  29. #else
  30. extern qp_pixel_t qp_internal_global_pixel_lookup_table[16];
  31. #endif
  32. // Generates a color-interpolated lookup table based off the number of items, from foreground to background, for use with monochrome image rendering.
  33. // Returns true if a palette was created, false if the palette is reused.
  34. // As this uses a global, this may present a problem if using the same parameters but a different screen converts pixels -- use qp_internal_invalidate_palette() below to reset.
  35. bool qp_internal_interpolate_palette(qp_pixel_t fg_hsv888, qp_pixel_t bg_hsv888, int16_t steps);
  36. // Resets the global palette so that it can be regenerated. Only needed if the colors are identical, but a different display is used with a different internal pixel format.
  37. void qp_internal_invalidate_palette(void);
  38. // Helper shared between image and font rendering -- sets up the global palette to match the palette block specified in the asset. Expects the stream to be positioned at the start of the block header.
  39. bool qp_internal_load_qgf_palette(qp_stream_t* stream, uint8_t bpp);
  40. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. // Quantum Painter codec functions
  42. enum qp_internal_rle_mode_t {
  43. MARKER_BYTE,
  44. REPEATING_RUN,
  45. NON_REPEATING_RUN,
  46. };
  47. struct qp_internal_byte_input_state {
  48. painter_device_t device;
  49. qp_stream_t* src_stream;
  50. int16_t curr;
  51. union {
  52. // RLE-specific
  53. struct {
  54. enum qp_internal_rle_mode_t mode;
  55. uint8_t remain; // number of bytes remaining in the current mode
  56. } rle;
  57. };
  58. };
  59. struct qp_internal_pixel_output_state {
  60. painter_device_t device;
  61. uint32_t pixel_write_pos;
  62. uint32_t max_pixels;
  63. };
  64. bool qp_internal_pixel_appender(qp_pixel_t* palette, uint8_t index, void* cb_arg);
  65. qp_internal_byte_input_callback qp_internal_prepare_input_state(struct qp_internal_byte_input_state* input_state, painter_compression_t compression);