logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

qgf.h (6743B)


  1. // Copyright 2021 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. // Quantum Graphics File "QGF" File Format.
  5. // See https://docs.qmk.fm/#/quantum_painter_qgf for more information.
  6. #include <stdint.h>
  7. #include <stdbool.h>
  8. #include "compiler_support.h"
  9. #include "qp_stream.h"
  10. #include "qp_internal.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // QGF structures
  13. /////////////////////////////////////////
  14. // Common block header
  15. typedef struct QP_PACKED qgf_block_header_v1_t {
  16. uint8_t type_id; // See each respective block type below.
  17. uint8_t neg_type_id; // Negated type ID, used for detecting parsing errors.
  18. uint32_t length : 24; // 24-bit blob length, allowing for block sizes of a maximum of 16MB.
  19. } qgf_block_header_v1_t;
  20. STATIC_ASSERT(sizeof(qgf_block_header_v1_t) == 5, "qgf_block_header_v1_t must be 5 bytes in v1 of QGF");
  21. /////////////////////////////////////////
  22. // Graphics descriptor
  23. #define QGF_GRAPHICS_DESCRIPTOR_TYPEID 0x00
  24. typedef struct QP_PACKED qgf_graphics_descriptor_v1_t {
  25. qgf_block_header_v1_t header; // = { .type_id = 0x00, .neg_type_id = (~0x00), .length = 18 }
  26. uint32_t magic : 24; // constant, equal to 0x464751 ("QGF")
  27. uint8_t qgf_version; // constant, equal to 0x01
  28. uint32_t total_file_size; // total size of the entire file, starting at offset zero
  29. uint32_t neg_total_file_size; // negated value of total_file_size
  30. uint16_t image_width; // in pixels
  31. uint16_t image_height; // in pixels
  32. uint16_t frame_count; // minimum of 1
  33. } qgf_graphics_descriptor_v1_t;
  34. STATIC_ASSERT(sizeof(qgf_graphics_descriptor_v1_t) == (sizeof(qgf_block_header_v1_t) + 18), "qgf_graphics_descriptor_v1_t must be 23 bytes in v1 of QGF");
  35. #define QGF_MAGIC 0x464751
  36. /////////////////////////////////////////
  37. // Frame offset descriptor
  38. #define QGF_FRAME_OFFSET_DESCRIPTOR_TYPEID 0x01
  39. typedef struct QP_PACKED qgf_frame_offsets_v1_t {
  40. qgf_block_header_v1_t header; // = { .type_id = 0x01, .neg_type_id = (~0x01), .length = (N * sizeof(uint32_t)) }
  41. uint32_t offset[0]; // '0' signifies that this struct is immediately followed by the frame offsets
  42. } qgf_frame_offsets_v1_t;
  43. STATIC_ASSERT(sizeof(qgf_frame_offsets_v1_t) == sizeof(qgf_block_header_v1_t), "qgf_frame_offsets_v1_t must only contain qgf_block_header_v1_t in v1 of QGF");
  44. /////////////////////////////////////////
  45. // Frame descriptor
  46. #define QGF_FRAME_DESCRIPTOR_TYPEID 0x02
  47. typedef struct QP_PACKED qgf_frame_v1_t {
  48. qgf_block_header_v1_t header; // = { .type_id = 0x02, .neg_type_id = (~0x02), .length = 6 }
  49. qp_image_format_t format : 8; // Frame format, see qp_internal_formats.h.
  50. uint8_t flags; // Frame flags, see below.
  51. painter_compression_t compression_scheme : 8; // Compression scheme, see qp.h.
  52. uint8_t transparency_index; // palette index used for transparent pixels (not yet implemented)
  53. uint16_t delay; // frame delay time for animations (in units of milliseconds)
  54. } qgf_frame_v1_t;
  55. STATIC_ASSERT(sizeof(qgf_frame_v1_t) == (sizeof(qgf_block_header_v1_t) + 6), "qgf_frame_v1_t must be 11 bytes in v1 of QGF");
  56. #define QGF_FRAME_FLAG_DELTA 0x02
  57. #define QGF_FRAME_FLAG_TRANSPARENT 0x01
  58. /////////////////////////////////////////
  59. // Frame palette descriptor
  60. #define QGF_FRAME_PALETTE_DESCRIPTOR_TYPEID 0x03
  61. typedef struct QP_PACKED qgf_palette_entry_v1_t {
  62. uint8_t h; // hue component: `[0,360)` degrees is mapped to `[0,255]` uint8_t.
  63. uint8_t s; // saturation component: `[0,1]` is mapped to `[0,255]` uint8_t.
  64. uint8_t v; // value component: `[0,1]` is mapped to `[0,255]` uint8_t.
  65. } qgf_palette_entry_v1_t;
  66. STATIC_ASSERT(sizeof(qgf_palette_entry_v1_t) == 3, "Palette entry is not 3 bytes in size");
  67. typedef struct QP_PACKED qgf_palette_v1_t {
  68. qgf_block_header_v1_t header; // = { .type_id = 0x03, .neg_type_id = (~0x03), .length = (N * 3 * sizeof(uint8_t)) }
  69. qgf_palette_entry_v1_t hsv[0]; // N * hsv, where N is the number of palette entries depending on the frame format in the descriptor
  70. } qgf_palette_v1_t;
  71. STATIC_ASSERT(sizeof(qgf_palette_v1_t) == sizeof(qgf_block_header_v1_t), "qgf_palette_v1_t must only contain qgf_block_header_v1_t in v1 of QGF");
  72. /////////////////////////////////////////
  73. // Frame delta descriptor
  74. #define QGF_FRAME_DELTA_DESCRIPTOR_TYPEID 0x04
  75. typedef struct QP_PACKED qgf_delta_v1_t {
  76. qgf_block_header_v1_t header; // = { .type_id = 0x04, .neg_type_id = (~0x04), .length = 8 }
  77. uint16_t left; // The left pixel location to draw the delta image
  78. uint16_t top; // The top pixel location to draw the delta image
  79. uint16_t right; // The right pixel location to to draw the delta image
  80. uint16_t bottom; // The bottom pixel location to to draw the delta image
  81. } qgf_delta_v1_t;
  82. STATIC_ASSERT(sizeof(qgf_delta_v1_t) == (sizeof(qgf_block_header_v1_t) + 8), "qgf_delta_v1_t must be 13 bytes in v1 of QGF");
  83. /////////////////////////////////////////
  84. // Frame data descriptor
  85. #define QGF_FRAME_DATA_DESCRIPTOR_TYPEID 0x05
  86. typedef struct QP_PACKED qgf_data_v1_t {
  87. qgf_block_header_v1_t header; // = { .type_id = 0x05, .neg_type_id = (~0x05), .length = N }
  88. uint8_t data[0]; // 0 signifies that this struct is immediately followed by the length of data specified in the header
  89. } qgf_data_v1_t;
  90. STATIC_ASSERT(sizeof(qgf_data_v1_t) == sizeof(qgf_block_header_v1_t), "qgf_data_v1_t must only contain qgf_block_header_v1_t in v1 of QGF");
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. // QGF API
  93. uint32_t qgf_get_total_size(qp_stream_t *stream);
  94. bool qgf_validate_stream(qp_stream_t *stream);
  95. bool qgf_validate_block_header(qgf_block_header_v1_t *desc, uint8_t expected_typeid, int32_t expected_length);
  96. bool qgf_read_graphics_descriptor(qp_stream_t *stream, uint16_t *image_width, uint16_t *image_height, uint16_t *frame_count, uint32_t *total_bytes);
  97. bool qgf_parse_format(qp_image_format_t format, uint8_t *bpp, bool *has_palette, bool *is_panel_native);
  98. void qgf_seek_to_frame_descriptor(qp_stream_t *stream, uint16_t frame_number);
  99. bool qgf_parse_frame_descriptor(qgf_frame_v1_t *frame_descriptor, uint8_t *bpp, bool *has_palette, bool *is_panel_native, bool *is_delta, painter_compression_t *compression_scheme, uint16_t *delay);