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.

292 lines
11 KiB

  1. // Copyright 2021 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // Quantum Graphics File "QGF" File Format.
  4. // See https://docs.qmk.fm/#/quantum_painter_qgf for more information.
  5. #include "qgf.h"
  6. #include "qp_draw.h"
  7. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. // QGF API
  9. bool qgf_validate_block_header(qgf_block_header_v1_t *desc, uint8_t expected_typeid, int32_t expected_length) {
  10. if (desc->type_id != expected_typeid || desc->neg_type_id != ((~expected_typeid) & 0xFF)) {
  11. qp_dprintf("Failed to validate header, expected typeid 0x%02X, was 0x%02X, expected negated typeid 0x%02X, was 0x%02X\n", (int)expected_typeid, (int)desc->type_id, (int)((~desc->type_id) & 0xFF), (int)desc->neg_type_id);
  12. return false;
  13. }
  14. if (expected_length >= 0 && desc->length != expected_length) {
  15. qp_dprintf("Failed to validate header (typeid 0x%02X), expected length %d, was %d\n", (int)desc->type_id, (int)expected_length, (int)desc->length);
  16. return false;
  17. }
  18. return true;
  19. }
  20. bool qgf_parse_format(qp_image_format_t format, uint8_t *bpp, bool *has_palette) {
  21. // clang-format off
  22. static const struct QP_PACKED {
  23. uint8_t bpp;
  24. bool has_palette;
  25. } formats[] = {
  26. [GRAYSCALE_1BPP] = {.bpp = 1, .has_palette = false},
  27. [GRAYSCALE_2BPP] = {.bpp = 2, .has_palette = false},
  28. [GRAYSCALE_4BPP] = {.bpp = 4, .has_palette = false},
  29. [GRAYSCALE_8BPP] = {.bpp = 8, .has_palette = false},
  30. [PALETTE_1BPP] = {.bpp = 1, .has_palette = true},
  31. [PALETTE_2BPP] = {.bpp = 2, .has_palette = true},
  32. [PALETTE_4BPP] = {.bpp = 4, .has_palette = true},
  33. [PALETTE_8BPP] = {.bpp = 8, .has_palette = true},
  34. };
  35. // clang-format on
  36. // Copy out the required info
  37. if (format > PALETTE_8BPP) {
  38. qp_dprintf("Failed to parse frame_descriptor, invalid format 0x%02X\n", (int)format);
  39. return false;
  40. }
  41. // Copy out the required info
  42. if (bpp) {
  43. *bpp = formats[format].bpp;
  44. }
  45. if (has_palette) {
  46. *has_palette = formats[format].has_palette;
  47. }
  48. return true;
  49. }
  50. bool qgf_parse_frame_descriptor(qgf_frame_v1_t *frame_descriptor, uint8_t *bpp, bool *has_palette, bool *is_delta, painter_compression_t *compression_scheme, uint16_t *delay) {
  51. // Decode the format
  52. qgf_parse_format(frame_descriptor->format, bpp, has_palette);
  53. // Copy out the required info
  54. if (is_delta) {
  55. *is_delta = (frame_descriptor->flags & QGF_FRAME_FLAG_DELTA) == QGF_FRAME_FLAG_DELTA;
  56. }
  57. if (compression_scheme) {
  58. *compression_scheme = frame_descriptor->compression_scheme;
  59. }
  60. if (delay) {
  61. *delay = frame_descriptor->delay;
  62. }
  63. return true;
  64. }
  65. 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) {
  66. // Seek to the start
  67. qp_stream_setpos(stream, 0);
  68. // Read and validate the graphics descriptor
  69. qgf_graphics_descriptor_v1_t graphics_descriptor;
  70. if (qp_stream_read(&graphics_descriptor, sizeof(qgf_graphics_descriptor_v1_t), 1, stream) != 1) {
  71. qp_dprintf("Failed to read graphics_descriptor, expected length was not %d\n", (int)sizeof(qgf_graphics_descriptor_v1_t));
  72. return false;
  73. }
  74. // Make sure this block is valid
  75. if (!qgf_validate_block_header(&graphics_descriptor.header, QGF_GRAPHICS_DESCRIPTOR_TYPEID, (sizeof(qgf_graphics_descriptor_v1_t) - sizeof(qgf_block_header_v1_t)))) {
  76. return false;
  77. }
  78. // Make sure the magic and version are correct
  79. if (graphics_descriptor.magic != QGF_MAGIC || graphics_descriptor.qgf_version != 0x01) {
  80. qp_dprintf("Failed to validate graphics_descriptor, expected magic 0x%06X was 0x%06X, expected version = 0x%02X was 0x%02X\n", (int)QGF_MAGIC, (int)graphics_descriptor.magic, (int)0x01, (int)graphics_descriptor.qgf_version);
  81. return false;
  82. }
  83. // Make sure the file length is valid
  84. if (graphics_descriptor.neg_total_file_size != ~graphics_descriptor.total_file_size) {
  85. qp_dprintf("Failed to validate graphics_descriptor, expected negated length 0x%08X was 0x%08X\n", (int)(~graphics_descriptor.total_file_size), (int)graphics_descriptor.neg_total_file_size);
  86. return false;
  87. }
  88. // Copy out the required info
  89. if (image_width) {
  90. *image_width = graphics_descriptor.image_width;
  91. }
  92. if (image_height) {
  93. *image_height = graphics_descriptor.image_height;
  94. }
  95. if (frame_count) {
  96. *frame_count = graphics_descriptor.frame_count;
  97. }
  98. if (total_bytes) {
  99. *total_bytes = graphics_descriptor.total_file_size;
  100. }
  101. return true;
  102. }
  103. static bool qgf_read_frame_offset(qp_stream_t *stream, uint16_t frame_number, uint32_t *frame_offset) {
  104. uint16_t frame_count;
  105. if (!qgf_read_graphics_descriptor(stream, NULL, NULL, &frame_count, NULL)) {
  106. return false;
  107. }
  108. // Read the frame offsets descriptor
  109. qgf_frame_offsets_v1_t frame_offsets;
  110. if (qp_stream_read(&frame_offsets, sizeof(qgf_frame_offsets_v1_t), 1, stream) != 1) {
  111. qp_dprintf("Failed to read frame_offsets, expected length was not %d\n", (int)sizeof(qgf_frame_offsets_v1_t));
  112. return false;
  113. }
  114. // Make sure this block is valid
  115. if (!qgf_validate_block_header(&frame_offsets.header, QGF_FRAME_OFFSET_DESCRIPTOR_TYPEID, (frame_count * sizeof(uint32_t)))) {
  116. return false;
  117. }
  118. if (frame_number >= frame_count) {
  119. qp_dprintf("Invalid frame number, was %d but only %d frames in image\n", (int)frame_number, (int)frame_count);
  120. return false;
  121. }
  122. // Skip the necessary amount of data to get to the requested frame offset
  123. qp_stream_seek(stream, frame_number * sizeof(uint32_t), SEEK_CUR);
  124. // Read the frame offset
  125. uint32_t offset = 0;
  126. if (qp_stream_read(&offset, sizeof(uint32_t), 1, stream) != 1) {
  127. qp_dprintf("Failed to read frame offset, expected length was not %d\n", (int)sizeof(uint32_t));
  128. return false;
  129. }
  130. // Copy out the required info
  131. if (frame_offset) {
  132. *frame_offset = offset;
  133. }
  134. return true;
  135. }
  136. void qgf_seek_to_frame_descriptor(qp_stream_t *stream, uint16_t frame_number) {
  137. // Read the offset
  138. uint32_t offset = 0;
  139. qgf_read_frame_offset(stream, frame_number, &offset);
  140. // Move to the offset
  141. qp_stream_setpos(stream, offset);
  142. }
  143. bool qgf_validate_frame_descriptor(qp_stream_t *stream, uint16_t frame_number, uint8_t *bpp, bool *has_palette, bool *is_delta) {
  144. // Seek to the correct location
  145. qgf_seek_to_frame_descriptor(stream, frame_number);
  146. // Read the raw descriptor
  147. qgf_frame_v1_t frame_descriptor;
  148. if (qp_stream_read(&frame_descriptor, sizeof(qgf_frame_v1_t), 1, stream) != 1) {
  149. qp_dprintf("Failed to read frame_descriptor, expected length was not %d\n", (int)sizeof(qgf_frame_v1_t));
  150. return false;
  151. }
  152. // Make sure this block is valid
  153. if (!qgf_validate_block_header(&frame_descriptor.header, QGF_FRAME_DESCRIPTOR_TYPEID, (sizeof(qgf_frame_v1_t) - sizeof(qgf_block_header_v1_t)))) {
  154. return false;
  155. }
  156. return qgf_parse_frame_descriptor(&frame_descriptor, bpp, has_palette, is_delta, NULL, NULL);
  157. }
  158. bool qgf_validate_palette_descriptor(qp_stream_t *stream, uint16_t frame_number, uint8_t bpp) {
  159. // Read the palette descriptor
  160. qgf_palette_v1_t palette_descriptor;
  161. if (qp_stream_read(&palette_descriptor, sizeof(qgf_palette_v1_t), 1, stream) != 1) {
  162. qp_dprintf("Failed to read palette_descriptor, expected length was not %d\n", (int)sizeof(qgf_palette_v1_t));
  163. return false;
  164. }
  165. // Make sure this block is valid
  166. uint32_t expected_length = (1 << bpp) * 3 * sizeof(uint8_t);
  167. if (!qgf_validate_block_header(&palette_descriptor.header, QGF_FRAME_PALETTE_DESCRIPTOR_TYPEID, expected_length)) {
  168. return false;
  169. }
  170. // Move forward in the stream to the next block
  171. qp_stream_seek(stream, expected_length, SEEK_CUR);
  172. return true;
  173. }
  174. bool qgf_validate_delta_descriptor(qp_stream_t *stream, uint16_t frame_number) {
  175. // Read the delta descriptor
  176. qgf_delta_v1_t delta_descriptor;
  177. if (qp_stream_read(&delta_descriptor, sizeof(qgf_delta_v1_t), 1, stream) != 1) {
  178. qp_dprintf("Failed to read delta_descriptor, expected length was not %d\n", (int)sizeof(qgf_delta_v1_t));
  179. return false;
  180. }
  181. // Make sure this block is valid
  182. if (!qgf_validate_block_header(&delta_descriptor.header, QGF_FRAME_DELTA_DESCRIPTOR_TYPEID, (sizeof(qgf_delta_v1_t) - sizeof(qgf_block_header_v1_t)))) {
  183. return false;
  184. }
  185. return true;
  186. }
  187. bool qgf_validate_frame_data_descriptor(qp_stream_t *stream, uint16_t frame_number) {
  188. // Read and validate the data block
  189. qgf_data_v1_t data_descriptor;
  190. if (qp_stream_read(&data_descriptor, sizeof(qgf_data_v1_t), 1, stream) != 1) {
  191. qp_dprintf("Failed to read data_descriptor, expected length was not %d\n", (int)sizeof(qgf_data_v1_t));
  192. return false;
  193. }
  194. if (!qgf_validate_block_header(&data_descriptor.header, QGF_FRAME_DATA_DESCRIPTOR_TYPEID, -1)) {
  195. return false;
  196. }
  197. return true;
  198. }
  199. bool qgf_validate_stream(qp_stream_t *stream) {
  200. uint16_t frame_count;
  201. if (!qgf_read_graphics_descriptor(stream, NULL, NULL, &frame_count, NULL)) {
  202. return false;
  203. }
  204. // Read and validate all the frames (automatically validates the frame offset descriptor in the process)
  205. for (uint16_t i = 0; i < frame_count; ++i) {
  206. // Validate the frame descriptor block
  207. uint8_t bpp;
  208. bool has_palette;
  209. bool has_delta;
  210. if (!qgf_validate_frame_descriptor(stream, i, &bpp, &has_palette, &has_delta)) {
  211. return false;
  212. }
  213. // If we've got a palette block, check it
  214. if (has_palette && !qgf_validate_palette_descriptor(stream, i, bpp)) {
  215. return false;
  216. }
  217. // If we've got a delta block, check it
  218. if (has_delta && !qgf_validate_delta_descriptor(stream, i)) {
  219. return false;
  220. }
  221. // Check the data block
  222. if (!qgf_validate_frame_data_descriptor(stream, i)) {
  223. return false;
  224. }
  225. }
  226. return true;
  227. }
  228. // Work out the total size of an image definition, assuming we can read far enough into the file
  229. uint32_t qgf_get_total_size(qp_stream_t *stream) {
  230. // Get the original location
  231. uint32_t oldpos = qp_stream_tell(stream);
  232. // Read the graphics descriptor, grabbing the size
  233. uint32_t total_size;
  234. if (!qgf_read_graphics_descriptor(stream, NULL, NULL, NULL, &total_size)) {
  235. return false;
  236. }
  237. // Restore the original location
  238. qp_stream_setpos(stream, oldpos);
  239. return total_size;
  240. }