7 struct custom_read_userdata {
12 ssize_t custom_read(void* userdata, uint8_t* buf, size_t count)
14 struct custom_read_userdata* ud = (struct custom_read_userdata*)userdata;
15 size_t num_to_read = (ud->bytes_left > count ? count : ud->bytes_left);
16 memcpy(buf, ud->bytes, num_to_read);
17 ud->bytes += num_to_read;
18 ud->bytes_left -= num_to_read;
22 // Uses the example from section K.3.1 from the JPEG standard.
26 // Chunk length: Chunk length (2) + coefficient class (1) +
27 // code lengths (16) + code words (12)
30 // DC coefficient, table 0
33 // List of code lengths
34 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
40 // Expected results (table K.3)
44 } expected_table[12] = {
59 struct custom_read_userdata ud;
61 ud.bytes_left = sizeof(bytes);
63 huffman_tables_t tables;
64 read_huffman_tables(&tables, custom_read, &ud);
66 struct huffman_table* tbl = &tables[DC_CLASS][0];
67 for (unsigned i = 0; i < 12; ++i) {
68 assert(tbl->huffsize[i] == expected_table[i].code_length);
69 assert(tbl->huffcode[i] == expected_table[i].code_word);
75 printf("test_table_gen()\n");
78 printf("All tests pass.\n");