Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/tests/mu-office-test.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2021 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 /* | |
| 24 * mu-office-test - Simple test for Muoffice. | |
| 25 */ | |
| 26 | |
| 27 #include "mupdf/memento.h" | |
| 28 #include "mupdf/helpers/mu-office-lib.h" | |
| 29 #include <windows.h> | |
| 30 #include <stdio.h> | |
| 31 #include <assert.h> | |
| 32 | |
| 33 static HANDLE loaded; | |
| 34 | |
| 35 /* Forward definition */ | |
| 36 static void save_png(const MuOfficeBitmap *bitmap, const char *filename); | |
| 37 | |
| 38 static void | |
| 39 load_progress(void *cookie, int pages_loaded, int complete) | |
| 40 { | |
| 41 assert((intptr_t)cookie == 1234); | |
| 42 | |
| 43 fprintf(stderr, "load_progress: pages_loaded=%d complete=%d\n", pages_loaded, complete); | |
| 44 | |
| 45 if (complete) | |
| 46 (void)ReleaseSemaphore(loaded, 1, NULL); | |
| 47 } | |
| 48 | |
| 49 static void | |
| 50 load_error(void *cookie, MuOfficeDocErrorType error) | |
| 51 { | |
| 52 assert((intptr_t)cookie == 1234); | |
| 53 | |
| 54 fprintf(stderr, "load_error: error=%d\n", error); | |
| 55 } | |
| 56 | |
| 57 static void render_progress(void *cookie, MuError error) | |
| 58 { | |
| 59 assert((intptr_t)cookie == 5678); | |
| 60 | |
| 61 fprintf(stderr, "render_progress: error=%d\n", error); | |
| 62 (void)ReleaseSemaphore(loaded, 1, NULL); | |
| 63 } | |
| 64 | |
| 65 static int | |
| 66 test_async(MuOfficeLib *mu) | |
| 67 { | |
| 68 MuOfficeDoc *doc; | |
| 69 MuError err; | |
| 70 int count; | |
| 71 MuOfficePage *page; | |
| 72 float w, h; | |
| 73 MuOfficeBitmap bitmap; | |
| 74 MuOfficeRenderArea area; | |
| 75 MuOfficeRender *render; | |
| 76 | |
| 77 err = MuOfficeLib_loadDocument(mu, | |
| 78 "../MyTests/pdf_reference17.pdf", | |
| 79 load_progress, | |
| 80 load_error, | |
| 81 (void *)1234, /* Arbitrary */ | |
| 82 &doc); | |
| 83 | |
| 84 /* Wait for the load to complete */ | |
| 85 WaitForSingleObject(loaded, INFINITE); | |
| 86 | |
| 87 /* Test number of pages */ | |
| 88 err = MuOfficeDoc_getNumPages(doc, &count); | |
| 89 if (err) | |
| 90 { | |
| 91 fprintf(stderr, "Failed to count pages: error=%d\n", err); | |
| 92 return EXIT_FAILURE; | |
| 93 } | |
| 94 fprintf(stderr, "%d Pages in document\n", count); | |
| 95 | |
| 96 /* Get a page */ | |
| 97 err = MuOfficeDoc_getPage(doc, 0, NULL, (void *)4321, &page); | |
| 98 if (err) | |
| 99 { | |
| 100 fprintf(stderr, "Failed to get page: error=%d\n", err); | |
| 101 return EXIT_FAILURE; | |
| 102 } | |
| 103 | |
| 104 /* Get size of page */ | |
| 105 err = MuOfficePage_getSize(page, &w, &h); | |
| 106 if (err) | |
| 107 { | |
| 108 fprintf(stderr, "Failed to get page size: error=%d\n", err); | |
| 109 return EXIT_FAILURE; | |
| 110 } | |
| 111 fprintf(stderr, "Page size = %g x %g\n", w, h); | |
| 112 | |
| 113 /* Allocate ourselves a bitmap */ | |
| 114 bitmap.width = (int)(w * 1.5f + 0.5f); | |
| 115 bitmap.height = (int)(h * 1.5f + 0.5f); | |
| 116 bitmap.lineSkip = bitmap.width * 4; | |
| 117 bitmap.memptr = malloc(bitmap.lineSkip * bitmap.height); | |
| 118 | |
| 119 /* Set the area to render the whole bitmap */ | |
| 120 area.origin.x = 0; | |
| 121 area.origin.y = 0; | |
| 122 area.renderArea.x = 0; | |
| 123 area.renderArea.y = 0; | |
| 124 area.renderArea.width = bitmap.width; | |
| 125 area.renderArea.height = bitmap.height; | |
| 126 | |
| 127 /* Render into the bitmap */ | |
| 128 err = MuOfficePage_render(page, 1.5f, &bitmap, &area, render_progress, (void *)5678, &render); | |
| 129 if (err) | |
| 130 { | |
| 131 fprintf(stderr, "Page render failed: error=%d\n", err); | |
| 132 return EXIT_FAILURE; | |
| 133 } | |
| 134 | |
| 135 /* Wait for the render to complete */ | |
| 136 WaitForSingleObject(loaded, INFINITE); | |
| 137 | |
| 138 /* Kill the render */ | |
| 139 MuOfficeRender_destroy(render); | |
| 140 | |
| 141 /* Output the bitmap */ | |
| 142 save_png(&bitmap, "out.png"); | |
| 143 free(bitmap.memptr); | |
| 144 | |
| 145 MuOfficePage_destroy(page); | |
| 146 | |
| 147 MuOfficeDoc_destroy(doc); | |
| 148 | |
| 149 CloseHandle(loaded); | |
| 150 loaded = NULL; | |
| 151 | |
| 152 return EXIT_SUCCESS; | |
| 153 } | |
| 154 | |
| 155 static int | |
| 156 test_sync(MuOfficeLib *mu) | |
| 157 { | |
| 158 MuOfficeDoc *doc; | |
| 159 MuError err; | |
| 160 int count; | |
| 161 MuOfficePage *page; | |
| 162 float w, h; | |
| 163 MuOfficeBitmap bitmap; | |
| 164 MuOfficeRenderArea area; | |
| 165 MuOfficeRender *render; | |
| 166 | |
| 167 loaded = CreateSemaphore(NULL, 0, 1, NULL); | |
| 168 | |
| 169 err = MuOfficeLib_loadDocument(mu, | |
| 170 "../MyTests/pdf_reference17.pdf", | |
| 171 NULL, | |
| 172 NULL, | |
| 173 NULL, | |
| 174 &doc); | |
| 175 | |
| 176 /* Test number of pages */ | |
| 177 err = MuOfficeDoc_getNumPages(doc, &count); | |
| 178 if (err) | |
| 179 { | |
| 180 fprintf(stderr, "Failed to count pages: error=%d\n", err); | |
| 181 return EXIT_FAILURE; | |
| 182 } | |
| 183 fprintf(stderr, "%d Pages in document\n", count); | |
| 184 | |
| 185 /* Get a page */ | |
| 186 err = MuOfficeDoc_getPage(doc, 1, NULL, (void *)4321, &page); | |
| 187 if (err) | |
| 188 { | |
| 189 fprintf(stderr, "Failed to get page: error=%d\n", err); | |
| 190 return EXIT_FAILURE; | |
| 191 } | |
| 192 | |
| 193 /* Get size of page */ | |
| 194 err = MuOfficePage_getSize(page, &w, &h); | |
| 195 if (err) | |
| 196 { | |
| 197 fprintf(stderr, "Failed to get page size: error=%d\n", err); | |
| 198 return EXIT_FAILURE; | |
| 199 } | |
| 200 fprintf(stderr, "Page size = %g x %g\n", w, h); | |
| 201 | |
| 202 /* Allocate ourselves a bitmap */ | |
| 203 bitmap.width = (int)(w * 1.5f + 0.5f); | |
| 204 bitmap.height = (int)(h * 1.5f + 0.5f); | |
| 205 bitmap.lineSkip = bitmap.width * 4; | |
| 206 bitmap.memptr = malloc(bitmap.lineSkip * bitmap.height); | |
| 207 | |
| 208 /* Set the area to render the whole bitmap */ | |
| 209 area.origin.x = 0; | |
| 210 area.origin.y = 0; | |
| 211 area.renderArea.x = 0; | |
| 212 area.renderArea.y = 0; | |
| 213 area.renderArea.width = bitmap.width; | |
| 214 area.renderArea.height = bitmap.height; | |
| 215 | |
| 216 /* Render into the bitmap */ | |
| 217 err = MuOfficePage_render(page, 1.5f, &bitmap, &area, NULL, NULL, &render); | |
| 218 if (err) | |
| 219 { | |
| 220 fprintf(stderr, "Page render failed: error=%d\n", err); | |
| 221 return EXIT_FAILURE; | |
| 222 } | |
| 223 | |
| 224 err = MuOfficeRender_waitUntilComplete(render); | |
| 225 if (err) | |
| 226 { | |
| 227 fprintf(stderr, "Page render failed to complete: error=%d\n", err); | |
| 228 return EXIT_FAILURE; | |
| 229 } | |
| 230 | |
| 231 /* Kill the render */ | |
| 232 MuOfficeRender_destroy(render); | |
| 233 | |
| 234 /* Output the bitmap */ | |
| 235 save_png(&bitmap, "out1.png"); | |
| 236 free(bitmap.memptr); | |
| 237 | |
| 238 MuOfficePage_destroy(page); | |
| 239 | |
| 240 MuOfficeDoc_destroy(doc); | |
| 241 | |
| 242 return EXIT_SUCCESS; | |
| 243 } | |
| 244 | |
| 245 int main(int argc, char **argv) | |
| 246 { | |
| 247 MuOfficeLib *mu; | |
| 248 MuError err; | |
| 249 int ret; | |
| 250 | |
| 251 err = MuOfficeLib_create(&mu); | |
| 252 if (err) | |
| 253 { | |
| 254 fprintf(stderr, "Failed to create lib instance: error=%d\n", err); | |
| 255 return EXIT_FAILURE; | |
| 256 } | |
| 257 | |
| 258 ret = test_async(mu); | |
| 259 if (ret) | |
| 260 return ret; | |
| 261 | |
| 262 ret = test_sync(mu); | |
| 263 if (ret) | |
| 264 return ret; | |
| 265 | |
| 266 MuOfficeLib_destroy(mu); | |
| 267 | |
| 268 return EXIT_SUCCESS; | |
| 269 } | |
| 270 | |
| 271 /* | |
| 272 Code beneath here calls MuPDF directly, purely because | |
| 273 this is the easiest way to get PNG saving functionality. | |
| 274 This is not part of the test, which is why this is put | |
| 275 at the end. | |
| 276 */ | |
| 277 | |
| 278 #include "mupdf/fitz.h" | |
| 279 | |
| 280 static void | |
| 281 save_png(const MuOfficeBitmap *bitmap, const char *filename) | |
| 282 { | |
| 283 fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); | |
| 284 fz_pixmap *pix; | |
| 285 | |
| 286 if (ctx == NULL) | |
| 287 { | |
| 288 fprintf(stderr, "save_png failed!\n"); | |
| 289 exit(EXIT_FAILURE); | |
| 290 } | |
| 291 | |
| 292 pix = fz_new_pixmap_with_data(ctx, fz_device_rgb(ctx), bitmap->width, bitmap->height, NULL, 1, bitmap->lineSkip, bitmap->memptr); | |
| 293 | |
| 294 fz_try(ctx) | |
| 295 { | |
| 296 fz_save_pixmap_as_png(ctx, pix, filename); | |
| 297 } | |
| 298 fz_always(ctx) | |
| 299 { | |
| 300 fz_drop_pixmap(ctx, pix); | |
| 301 } | |
| 302 fz_catch(ctx) | |
| 303 { | |
| 304 fprintf(stderr, "save_png failed!\n"); | |
| 305 fz_drop_context(ctx); | |
| 306 exit(EXIT_FAILURE); | |
| 307 } | |
| 308 | |
| 309 fz_drop_context(ctx); | |
| 310 } |
