summaryrefslogtreecommitdiff
path: root/source/tests/mu-office-test.c
blob: d729de88287ad26e487bdc3360e744c08c8afb31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * mu-office-test - Simple test for Muoffice.
 */

#include "mupdf/memento.h"
#include "mupdf/helpers/mu-office-lib.h"
#include <windows.h>
#include <stdio.h>
#include <assert.h>

static HANDLE loaded;

/* Forward definition */
static void save_png(const MuOfficeBitmap *bitmap, const char *filename);

static void
load_progress(void *cookie, int pages_loaded, int complete)
{
	assert((intptr_t)cookie == 1234);

	fprintf(stderr, "load_progress: pages_loaded=%d complete=%d\n", pages_loaded, complete);

	if (complete)
		(void)ReleaseSemaphore(loaded, 1, NULL);
}

static void
load_error(void *cookie, MuOfficeDocErrorType error)
{
	assert((intptr_t)cookie == 1234);

	fprintf(stderr, "load_error: error=%d\n", error);
}

static void render_progress(void *cookie, MuError error)
{
	assert((int)cookie == 5678);

	fprintf(stderr, "render_progress: error=%d\n", error);
	(void)ReleaseSemaphore(loaded, 1, NULL);
}

static int
test_async(MuOfficeLib *mu)
{
	MuOfficeDoc *doc;
	MuError err;
	int count;
	MuOfficePage *page;
	float w, h;
	MuOfficeBitmap bitmap;
	MuOfficeRenderArea area;
	MuOfficeRender *render;

	err = MuOfficeLib_loadDocument(mu,
					"../MyTests/pdf_reference17.pdf",
					load_progress,
					load_error,
					(void *)1234, /* Arbitrary */
					&doc);

	/* Wait for the load to complete */
	WaitForSingleObject(loaded, INFINITE);

	/* Test number of pages */
	err = MuOfficeDoc_getNumPages(doc, &count);
	if (err)
	{
		fprintf(stderr, "Failed to count pages: error=%d\n", err);
		return EXIT_FAILURE;
	}
	fprintf(stderr, "%d Pages in document\n", count);

	/* Get a page */
	err = MuOfficeDoc_getPage(doc, 0, NULL, (void *)4321, &page);
	if (err)
	{
		fprintf(stderr, "Failed to get page: error=%d\n", err);
		return EXIT_FAILURE;
	}

	/* Get size of page */
	err = MuOfficePage_getSize(page, &w, &h);
	if (err)
	{
		fprintf(stderr, "Failed to get page size: error=%d\n", err);
		return EXIT_FAILURE;
	}
	fprintf(stderr, "Page size = %g x %g\n", w, h);

	/* Allocate ourselves a bitmap */
	bitmap.width = (int)(w * 1.5f + 0.5f);
	bitmap.height = (int)(h * 1.5f + 0.5f);
	bitmap.lineSkip = bitmap.width * 4;
	bitmap.memptr = malloc(bitmap.lineSkip * bitmap.height);

	/* Set the area to render the whole bitmap */
	area.origin.x = 0;
	area.origin.y = 0;
	area.renderArea.x = 0;
	area.renderArea.y = 0;
	area.renderArea.width = bitmap.width;
	area.renderArea.height = bitmap.height;

	/* Render into the bitmap */
	err = MuOfficePage_render(page, 1.5f, &bitmap, &area, render_progress, (void *)5678, &render);
	if (err)
	{
		fprintf(stderr, "Page render failed: error=%d\n", err);
		return EXIT_FAILURE;
	}

	/* Wait for the render to complete */
	WaitForSingleObject(loaded, INFINITE);

	/* Kill the render */
	MuOfficeRender_destroy(render);

	/* Output the bitmap */
	save_png(&bitmap, "out.png");
	free(bitmap.memptr);

	MuOfficePage_destroy(page);

	MuOfficeDoc_destroy(doc);

	CloseHandle(loaded);
	loaded = NULL;

	return EXIT_SUCCESS;
}

static int
test_sync(MuOfficeLib *mu)
{
	MuOfficeDoc *doc;
	MuError err;
	int count;
	MuOfficePage *page;
	float w, h;
	MuOfficeBitmap bitmap;
	MuOfficeRenderArea area;
	MuOfficeRender *render;

	loaded = CreateSemaphore(NULL, 0, 1, NULL);

	err = MuOfficeLib_loadDocument(mu,
					"../MyTests/pdf_reference17.pdf",
					NULL,
					NULL,
					NULL,
					&doc);

	/* Test number of pages */
	err = MuOfficeDoc_getNumPages(doc, &count);
	if (err)
	{
		fprintf(stderr, "Failed to count pages: error=%d\n", err);
		return EXIT_FAILURE;
	}
	fprintf(stderr, "%d Pages in document\n", count);

	/* Get a page */
	err = MuOfficeDoc_getPage(doc, 1, NULL, (void *)4321, &page);
	if (err)
	{
		fprintf(stderr, "Failed to get page: error=%d\n", err);
		return EXIT_FAILURE;
	}

	/* Get size of page */
	err = MuOfficePage_getSize(page, &w, &h);
	if (err)
	{
		fprintf(stderr, "Failed to get page size: error=%d\n", err);
		return EXIT_FAILURE;
	}
	fprintf(stderr, "Page size = %g x %g\n", w, h);

	/* Allocate ourselves a bitmap */
	bitmap.width = (int)(w * 1.5f + 0.5f);
	bitmap.height = (int)(h * 1.5f + 0.5f);
	bitmap.lineSkip = bitmap.width * 4;
	bitmap.memptr = malloc(bitmap.lineSkip * bitmap.height);

	/* Set the area to render the whole bitmap */
	area.origin.x = 0;
	area.origin.y = 0;
	area.renderArea.x = 0;
	area.renderArea.y = 0;
	area.renderArea.width = bitmap.width;
	area.renderArea.height = bitmap.height;

	/* Render into the bitmap */
	err = MuOfficePage_render(page, 1.5f, &bitmap, &area, NULL, NULL, &render);
	if (err)
	{
		fprintf(stderr, "Page render failed: error=%d\n", err);
		return EXIT_FAILURE;
	}

	err = MuOfficeRender_waitUntilComplete(render);
	if (err)
	{
		fprintf(stderr, "Page render failed to complete: error=%d\n", err);
		return EXIT_FAILURE;
	}

	/* Kill the render */
	MuOfficeRender_destroy(render);

	/* Output the bitmap */
	save_png(&bitmap, "out1.png");
	free(bitmap.memptr);

	MuOfficePage_destroy(page);

	MuOfficeDoc_destroy(doc);

	return EXIT_SUCCESS;
}

int main(int argc, char **argv)
{
	MuOfficeLib *mu;
	MuError err;
	int ret;

	err = MuOfficeLib_create(&mu);
	if (err)
	{
		fprintf(stderr, "Failed to create lib instance: error=%d\n", err);
		return EXIT_FAILURE;
	}

	ret = test_async(mu);
	if (ret)
		return ret;

	ret = test_sync(mu);
	if (ret)
		return ret;

	MuOfficeLib_destroy(mu);

	return EXIT_SUCCESS;
}

/*
	Code beneath here calls MuPDF directly, purely because
	this is the easiest way to get PNG saving functionality.
	This is not part of the test, which is why this is put
	at the end.
*/

#include "mupdf/fitz.h"

static void
save_png(const MuOfficeBitmap *bitmap, const char *filename)
{
	fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
	fz_pixmap *pix;

	if (ctx == NULL)
	{
		fprintf(stderr, "save_png failed!\n");
		exit(EXIT_FAILURE);
	}

	fz_try(ctx)
	{
		pix = fz_new_pixmap_with_data(ctx, fz_device_rgb(ctx), bitmap->width, bitmap->height, NULL, 1, bitmap->lineSkip, bitmap->memptr);

		fz_save_pixmap_as_png(ctx, pix, filename);
	}
	fz_always(ctx)
	{
		fz_drop_pixmap(ctx, pix);
	}
	fz_catch(ctx)
	{
		fprintf(stderr, "save_png failed!\n");
		fz_drop_context(ctx);
		exit(EXIT_FAILURE);
	}

	fz_drop_context(ctx);
}