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.

178 lines
9.0 KiB

  1. # QMK Graphics Format :id=qmk-graphics-format
  2. QMK uses a graphics format _("Quantum Graphics Format" - QGF)_ specifically for resource-constrained systems.
  3. This format is capable of encoding 1-, 2-, 4-, and 8-bit-per-pixel greyscale- and palette-based images. It also includes RLE for pixel data for some basic compression.
  4. All integer values are in little-endian format.
  5. The QGF is defined in terms of _blocks_ -- each _block_ contains a _header_ and an optional _blob_ of data. The _header_ contains the block's _typeid_, and the length of the _blob_ that follows. Each block type is denoted by a different _typeid_ has its own block definition below. All blocks are defined as packed structs, containing zero padding between fields.
  6. The general structure of the file is:
  7. * _Graphics descriptor block_
  8. * _Frame offset block_
  9. * Repeating list of frames:
  10. * _Frame descriptor block_
  11. * _Frame palette block_ (optional, depending on frame format)
  12. * _Frame delta block_ (optional, depending on delta flag)
  13. * _Frame data block_
  14. Different frames within the file should be considered "isolated" and may have their own image format and/or palette.
  15. ## Block Header :id=qgf-block-header
  16. This block header is present for all blocks, including the graphics descriptor.
  17. _Block header_ format:
  18. ```c
  19. typedef struct __attribute__((packed)) qgf_block_header_v1_t {
  20. uint8_t type_id; // See each respective block type
  21. uint8_t neg_type_id; // Negated type ID, used for detecting parsing errors
  22. uint24_t length; // 24-bit blob length, allowing for block sizes of a maximum of 16MB
  23. } qgf_block_header_v1_t;
  24. // _Static_assert(sizeof(qgf_block_header_v1_t) == 5, "qgf_block_header_v1_t must be 5 bytes in v1 of QGF");
  25. ```
  26. The _length_ describes the number of octets in the data following the block header -- a block header may specify a _length_ of `0` if no blob is specified.
  27. ## Graphics descriptor block :id=qgf-graphics-descriptor
  28. * _typeid_ = 0x00
  29. * _length_ = 18
  30. This block must be located at the start of the file contents, and can exist a maximum of once in an entire QGF file. It is always followed by the _frame offset block_.
  31. _Block_ format:
  32. ```c
  33. typedef struct __attribute__((packed)) qgf_graphics_descriptor_v1_t {
  34. qgf_block_header_v1_t header; // = { .type_id = 0x00, .neg_type_id = (~0x00), .length = 18 }
  35. uint24_t magic; // constant, equal to 0x464751 ("QGF")
  36. uint8_t qgf_version; // constant, equal to 0x01
  37. uint32_t total_file_size; // total size of the entire file, starting at offset zero
  38. uint32_t neg_total_file_size; // negated value of total_file_size, used for detecting parsing errors
  39. uint16_t image_width; // in pixels
  40. uint16_t image_height; // in pixels
  41. uint16_t frame_count; // minimum of 1
  42. } qgf_graphics_descriptor_v1_t;
  43. // _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");
  44. ```
  45. ## Frame offset block :id=qgf-frame-offset-descriptor
  46. * _typeid_ = 0x01
  47. * _length_ = variable
  48. This block denotes the offsets within the file to each frame's _frame descriptor block_, relative to the start of the file. The _frame offset block_ always immediately follows the _graphics descriptor block_. The contents of this block are an array of U32's, with one entry for each frame.
  49. Duplicate frame offsets in this block are allowed, if a certain frame is to be shown multiple times during animation.
  50. _Block_ format:
  51. ```c
  52. typedef struct __attribute__((packed)) qgf_frame_offsets_v1_t {
  53. qgf_block_header_v1_t header; // = { .type_id = 0x01, .neg_type_id = (~0x01), .length = (N * sizeof(uint32_t)) }
  54. uint32_t offset[N]; // where 'N' is the number of frames in the file
  55. } qgf_frame_offsets_v1_t;
  56. ```
  57. ## Frame descriptor block :id=qgf-frame-descriptor
  58. * _typeid_ = 0x02
  59. * _length_ = 5
  60. This block denotes the start of a frame.
  61. _Block_ format:
  62. ```c
  63. typedef struct __attribute__((packed)) qgf_frame_v1_t {
  64. qgf_block_header_v1_t header; // = { .type_id = 0x02, .neg_type_id = (~0x02), .length = 5 }
  65. uint8_t format; // Frame format, see below.
  66. uint8_t flags; // Frame flags, see below.
  67. uint8_t compression_scheme; // Compression scheme, see below.
  68. uint8_t transparency_index; // palette index used for transparent pixels (not yet implemented)
  69. uint16_t delay; // frame delay time for animations (in units of milliseconds)
  70. } qgf_frame_v1_t;
  71. // _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");
  72. ```
  73. If this frame is grayscale, the _frame descriptor block_ (or _frame delta block_ if flags denote a delta frame) is immediately followed by this frame's corresponding _frame data block_.
  74. If the frame uses an indexed palette, the _frame descriptor block_ (or _frame delta block_ if flags denote a delta frame) is immediately followed by this frame's corresponding _frame palette block_.
  75. Frame format possible values:
  76. * `0x00`: 1bpp grayscale, no palette, `0` = black, `1` = white, LSb first pixel
  77. * `0x01`: 2bpp grayscale, no palette, `0` = black, `3` = white, linear interpolation of brightness, LSb first pixel
  78. * `0x02`: 4bpp grayscale, no palette, `0` = black, `15` = white, linear interpolation of brightness, LSb first pixel
  79. * `0x03`: 8bpp grayscale, no palette, `0` = black, `255` = white, linear interpolation of brightness, LSb first pixel
  80. * `0x04`: 1bpp indexed palette, 2 colors, LSb first pixel
  81. * `0x05`: 2bpp indexed palette, 4 colors, LSb first pixel
  82. * `0x06`: 4bpp indexed palette, 16 colors, LSb first pixel
  83. * `0x07`: 8bpp indexed palette, 256 colors, LSb first pixel
  84. Frame flags is a bitmask with the following format:
  85. | `bit 7` | `bit 6` | `bit 5` | `bit 4` | `bit 3` | `bit 2` | `bit 1` | `bit 0` |
  86. |---------|---------|---------|---------|---------|---------|---------|--------------|
  87. | - | - | - | - | - | - | Delta | Transparency |
  88. * `[1]` -- Delta: Signifies that the current frame is a delta frame, which specifies only a sub-image. The _frame delta block_ follows the _frame palette block_ if the image format specifies a palette, otherwise it directly follows the _frame descriptor block_.
  89. * `[0]` -- Transparency: The transparent palette index in the _blob_ is considered valid and should be used when considering which pixels should be transparent during rendering this frame, if possible.
  90. Compression scheme possible values:
  91. * `0x00`: No compression
  92. * `0x01`: [QMK RLE](quantum_painter_rle.md)
  93. ## Frame palette block :id=qgf-frame-palette-descriptor
  94. * _typeid_ = 0x03
  95. * _length_ = variable
  96. This block describes the palette used for the frame. The _blob_ contains an array of palette entries -- one palette entry is present for each color used -- each palette entry is in QMK HSV888 format:
  97. ```c
  98. typedef struct __attribute__((packed)) qgf_palette_v1_t {
  99. qgf_block_header_v1_t header; // = { .type_id = 0x03, .neg_type_id = (~0x03), .length = (N * 3 * sizeof(uint8_t)) }
  100. struct { // container for a single HSV palette entry
  101. uint8_t h; // hue component: `[0,360)` degrees is mapped to `[0,255]` uint8_t.
  102. uint8_t s; // saturation component: `[0,1]` is mapped to `[0,255]` uint8_t.
  103. uint8_t v; // value component: `[0,1]` is mapped to `[0,255]` uint8_t.
  104. } hsv[N]; // N * hsv, where N is the number of palette entries depending on the frame format in the descriptor
  105. } qgf_palette_v1_t;
  106. ```
  107. ## Frame delta block :id=qgf-frame-delta-descriptor
  108. * _typeid_ = 0x04
  109. * _length_ = 8
  110. This block describes where the delta frame should be drawn, with respect to the top left location of the image.
  111. ```c
  112. typedef struct __attribute__((packed)) qgf_delta_v1_t {
  113. qgf_block_header_v1_t header; // = { .type_id = 0x04, .neg_type_id = (~0x04), .length = 8 }
  114. uint16_t left; // The left pixel location to draw the delta image
  115. uint16_t top; // The top pixel location to draw the delta image
  116. uint16_t right; // The right pixel location to to draw the delta image
  117. uint16_t bottom; // The bottom pixel location to to draw the delta image
  118. } qgf_delta_v1_t;
  119. // _Static_assert(sizeof(qgf_delta_v1_t) == 13, "qgf_delta_v1_t must be 13 bytes in v1 of QGF");
  120. ```
  121. ## Frame data block :id=qgf-frame-data-descriptor
  122. * _typeid_ = 0x05
  123. * _length_ = variable
  124. This block describes the data associated with the frame. The _blob_ contains an array of bytes containing the data corresponding to the frame's image format:
  125. ```c
  126. typedef struct __attribute__((packed)) qgf_data_v1_t {
  127. qgf_block_header_v1_t header; // = { .type_id = 0x05, .neg_type_id = (~0x05), .length = N }
  128. uint8_t data[N]; // N data octets
  129. } qgf_data_v1_t;
  130. ```