1#include <stdio.h>
2#include <assert.h>
3
4#include "mtmd.h"
5
6int main(void) {
7 printf(format: "\n\nTesting libmtmd C API...\n");
8 printf(format: "--------\n\n");
9
10 struct mtmd_context_params params = mtmd_context_params_default();
11 printf(format: "Default image marker: %s\n", params.image_marker);
12
13 mtmd_input_chunks * chunks = mtmd_test_create_input_chunks();
14
15 if (!chunks) {
16 fprintf(stderr, format: "Failed to create input chunks\n");
17 return 1;
18 }
19
20 size_t n_chunks = mtmd_input_chunks_size(chunks);
21 printf(format: "Number of chunks: %zu\n", n_chunks);
22 assert(n_chunks > 0);
23
24 for (size_t i = 0; i < n_chunks; i++) {
25 const mtmd_input_chunk * chunk = mtmd_input_chunks_get(chunks, idx: i);
26 assert(chunk != NULL);
27 enum mtmd_input_chunk_type type = mtmd_input_chunk_get_type(chunk);
28 printf(format: "Chunk %zu type: %d\n", i, type);
29
30 if (type == MTMD_INPUT_CHUNK_TYPE_TEXT) {
31 size_t n_tokens;
32 const llama_token * tokens = mtmd_input_chunk_get_tokens_text(chunk, n_tokens_output: &n_tokens);
33 printf(format: " Text chunk with %zu tokens\n", n_tokens);
34 assert(tokens != NULL);
35 assert(n_tokens > 0);
36 for (size_t j = 0; j < n_tokens; j++) {
37 assert(tokens[j] >= 0);
38 printf(format: " > Token %zu: %d\n", j, tokens[j]);
39 }
40
41 } else if (type == MTMD_INPUT_CHUNK_TYPE_IMAGE) {
42 const mtmd_image_tokens * image_tokens = mtmd_input_chunk_get_tokens_image(chunk);
43 size_t n_tokens = mtmd_image_tokens_get_n_tokens(image_tokens);
44 size_t nx = mtmd_image_tokens_get_nx(image_tokens);
45 size_t ny = mtmd_image_tokens_get_ny(image_tokens);
46 const char * id = mtmd_image_tokens_get_id(image_tokens);
47 assert(n_tokens > 0);
48 assert(nx > 0);
49 assert(ny > 0);
50 assert(id != NULL);
51 printf(format: " Image chunk with %zu tokens\n", n_tokens);
52 printf(format: " Image size: %zu x %zu\n", nx, ny);
53 printf(format: " Image ID: %s\n", id);
54 }
55 }
56
57 // Free the chunks
58 mtmd_input_chunks_free(chunks);
59
60 printf(format: "\n\nDONE: test libmtmd C API...\n");
61
62 return 0;
63}
64