qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

qp_draw.h (5562B)


      1 // Copyright 2021 Nick Brassel (@tzarc)
      2 // SPDX-License-Identifier: GPL-2.0-or-later
      3 
      4 #pragma once
      5 
      6 #include "qp_internal.h"
      7 #include "qp_stream.h"
      8 
      9 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     10 // Quantum Painter utility functions
     11 
     12 // Global variable used for native pixel data streaming.
     13 extern uint8_t qp_internal_global_pixdata_buffer[QUANTUM_PAINTER_PIXDATA_BUFFER_SIZE];
     14 
     15 // Check if the supplied bpp is capable of being rendered
     16 bool qp_internal_bpp_capable(uint8_t bits_per_pixel);
     17 
     18 // Returns the number of pixels that can fit in the pixdata buffer
     19 uint32_t qp_internal_num_pixels_in_buffer(painter_device_t device);
     20 
     21 // Fills the supplied buffer with equivalent native pixels matching the supplied HSV
     22 void qp_internal_fill_pixdata(painter_device_t device, uint32_t num_pixels, uint8_t hue, uint8_t sat, uint8_t val);
     23 
     24 // qp_setpixel internal implementation, but uses the global pixdata buffer with pre-converted native pixel. Only the first pixel is used.
     25 bool qp_internal_setpixel_impl(painter_device_t device, uint16_t x, uint16_t y);
     26 
     27 // qp_rect internal implementation, but uses the global pixdata buffer with pre-converted native pixels.
     28 bool qp_internal_fillrect_helper_impl(painter_device_t device, uint16_t l, uint16_t t, uint16_t r, uint16_t b);
     29 
     30 // Convert from input pixel data + palette to equivalent pixels
     31 typedef int16_t (*qp_internal_byte_input_callback)(void* cb_arg);
     32 typedef bool (*qp_internal_pixel_output_callback)(qp_pixel_t* palette, uint8_t index, void* cb_arg);
     33 typedef bool (*qp_internal_byte_output_callback)(uint8_t byte, void* cb_arg);
     34 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);
     35 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);
     36 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);
     37 bool qp_internal_send_bytes(painter_device_t device, uint32_t byte_count, qp_internal_byte_input_callback input_callback, void* input_arg, qp_internal_byte_output_callback output_callback, void* output_arg);
     38 
     39 // Global variable used for interpolated pixel lookup table.
     40 #if QUANTUM_PAINTER_SUPPORTS_256_PALETTE
     41 extern qp_pixel_t qp_internal_global_pixel_lookup_table[256];
     42 #else
     43 extern qp_pixel_t qp_internal_global_pixel_lookup_table[16];
     44 #endif
     45 
     46 // Generates a color-interpolated lookup table based off the number of items, from foreground to background, for use with monochrome image rendering.
     47 // Returns true if a palette was created, false if the palette is reused.
     48 // 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.
     49 bool qp_internal_interpolate_palette(qp_pixel_t fg_hsv888, qp_pixel_t bg_hsv888, int16_t steps);
     50 
     51 // 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.
     52 void qp_internal_invalidate_palette(void);
     53 
     54 // 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.
     55 bool qp_internal_load_qgf_palette(qp_stream_t* stream, uint8_t bpp);
     56 
     57 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     58 // Quantum Painter codec functions
     59 
     60 enum qp_internal_rle_mode_t {
     61     MARKER_BYTE,
     62     REPEATING_RUN,
     63     NON_REPEATING_RUN,
     64 };
     65 
     66 typedef struct qp_internal_byte_input_state_t {
     67     painter_device_t device;
     68     qp_stream_t*     src_stream;
     69     int16_t          curr;
     70     union {
     71         // RLE-specific
     72         struct {
     73             enum qp_internal_rle_mode_t mode;
     74             uint8_t                     remain; // number of bytes remaining in the current mode
     75         } rle;
     76     };
     77 } qp_internal_byte_input_state_t;
     78 
     79 typedef struct qp_internal_pixel_output_state_t {
     80     painter_device_t device;
     81     uint32_t         pixel_write_pos;
     82     uint32_t         max_pixels;
     83 } qp_internal_pixel_output_state_t;
     84 
     85 bool qp_internal_pixel_appender(qp_pixel_t* palette, uint8_t index, void* cb_arg);
     86 
     87 typedef struct qp_internal_byte_output_state_t {
     88     painter_device_t device;
     89     uint32_t         byte_write_pos;
     90     uint32_t         max_bytes;
     91 } qp_internal_byte_output_state_t;
     92 
     93 bool qp_internal_byte_appender(uint8_t byteval, void* cb_arg);
     94 
     95 // Helper shared between image and font rendering, sends pixels to the display using:
     96 //     - qp_internal_decode_palette + qp_internal_pixel_appender (bpp <= 8)
     97 //     - qp_internal_send_bytes                                  (bpp > 8)
     98 bool qp_internal_appender(painter_device_t device, uint8_t bpp, uint32_t pixel_count, qp_internal_byte_input_callback input_callback, void* input_state);
     99 
    100 qp_internal_byte_input_callback qp_internal_prepare_input_state(qp_internal_byte_input_state_t* input_state, painter_compression_t compression);