[4] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string> |
---|
| 4 | #include <time.h> |
---|
| 5 | #include <unistd.h> |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | #if defined(USE_POPPLER) |
---|
| 9 | |
---|
| 10 | #if SAGE_POPPLER_VERSION == 5 |
---|
| 11 | // Centos 5.5 |
---|
| 12 | #include "glib/poppler.h" |
---|
| 13 | PopplerDocument *document; |
---|
| 14 | PopplerBackend backend; |
---|
| 15 | PopplerPage *page; |
---|
| 16 | GEnumValue *enum_value; |
---|
| 17 | GError *error; |
---|
| 18 | GdkPixbuf *pixbuf; |
---|
| 19 | #endif |
---|
| 20 | |
---|
| 21 | #if SAGE_POPPLER_VERSION == 12 |
---|
| 22 | #include "poppler/goo/GooString.h" |
---|
| 23 | #include "poppler/GlobalParams.h" |
---|
| 24 | #include "poppler/PDFDoc.h" |
---|
| 25 | #include "poppler/UnicodeMap.h" |
---|
| 26 | #include "poppler/PDFDocEncoding.h" |
---|
| 27 | #include "poppler/DateInfo.h" |
---|
| 28 | #include "poppler/splash/SplashBitmap.h" |
---|
| 29 | #include "poppler/splash/Splash.h" |
---|
| 30 | #include "poppler/SplashOutputDev.h" |
---|
| 31 | // Rendering object |
---|
| 32 | SplashOutputDev *splashOut; |
---|
| 33 | PDFDoc *doc; |
---|
| 34 | #endif |
---|
| 35 | |
---|
| 36 | int pg_index; |
---|
| 37 | double x_resolution; |
---|
| 38 | double pg_w, pg_h; |
---|
| 39 | |
---|
| 40 | #else |
---|
| 41 | #include <wand/magick-wand.h> |
---|
| 42 | #endif |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | // headers for SAGE |
---|
| 46 | #include "sail.h" |
---|
| 47 | #include "misc.h" |
---|
| 48 | |
---|
| 49 | // for dxt compression |
---|
| 50 | #include "libdxt.h" |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | using namespace std; |
---|
| 54 | |
---|
| 55 | // if true, it will show the original image and not the dxt compressed one |
---|
| 56 | #if defined(USE_POPPLER) |
---|
| 57 | bool useDXT = false; // cannot yet since poppler returns RGB instead of RGBA |
---|
| 58 | #else |
---|
| 59 | bool useDXT = true; |
---|
| 60 | #endif |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | // other globals... |
---|
| 64 | byte *sageBuffer = NULL; // buffers for sage and dxt data |
---|
| 65 | byte *dxt = NULL; |
---|
| 66 | byte *rgba = NULL; |
---|
| 67 | unsigned int width, height; // image size |
---|
| 68 | string fileName; |
---|
| 69 | float lastX = 0; |
---|
| 70 | float lastY = 0; |
---|
| 71 | float dist = 0; |
---|
| 72 | int numImages = 0; |
---|
| 73 | int firstPage = 0; |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | #if ! defined(USE_POPPLER) |
---|
| 77 | // use ImageMagick to read all other formats |
---|
| 78 | MagickBooleanType status; |
---|
| 79 | MagickWand *wand; |
---|
| 80 | #endif |
---|
| 81 | |
---|
| 82 | // sail object |
---|
| 83 | sail sageInf; |
---|
| 84 | sailConfig scfg; |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | #define ThrowWandException(wand) \ |
---|
| 88 | { \ |
---|
| 89 | char \ |
---|
| 90 | *description; \ |
---|
| 91 | \ |
---|
| 92 | ExceptionType \ |
---|
| 93 | severity; \ |
---|
| 94 | \ |
---|
| 95 | description=MagickGetException(wand,&severity); \ |
---|
| 96 | (void) fprintf(stderr,"%s %s %ld %s\n",GetMagickModule(),description); \ |
---|
| 97 | description=(char *) MagickRelinquishMemory(description); \ |
---|
| 98 | exit(-1); \ |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | // ----------------------------------------------------------------------------- |
---|
| 103 | |
---|
| 104 | void getRGBA() |
---|
| 105 | { |
---|
| 106 | // get the pixels |
---|
| 107 | memset(rgba, 0, width*height*4); |
---|
| 108 | #if defined(USE_POPPLER) |
---|
| 109 | #if SAGE_POPPLER_VERSION == 5 |
---|
| 110 | poppler_page_render_to_pixbuf (page, 0, 0, width, height, x_resolution/72.0, 0, pixbuf); |
---|
| 111 | memcpy(rgba, gdk_pixbuf_get_pixels(pixbuf), width *height * 3); |
---|
| 112 | #endif |
---|
| 113 | |
---|
| 114 | #if SAGE_POPPLER_VERSION == 12 |
---|
| 115 | SplashBitmap *bitmap = splashOut->getBitmap(); |
---|
| 116 | memcpy(rgba, bitmap->getDataPtr(), width *height * 3); |
---|
| 117 | #endif |
---|
| 118 | #else |
---|
| 119 | MagickGetImagePixels(wand, 0, 0, width, height, "RGBA", CharPixel, rgba); |
---|
| 120 | #endif |
---|
| 121 | |
---|
| 122 | if (useDXT) { |
---|
| 123 | unsigned int numBytes; |
---|
| 124 | |
---|
| 125 | // compress into DXT |
---|
| 126 | memset(dxt, 0, width*height*4/8); |
---|
| 127 | numBytes = CompressDXT(rgba, dxt, width, height, FORMAT_DXT1, 1); |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | void swapBuffer() |
---|
| 132 | { |
---|
| 133 | // get buffer from SAGE and fill it with dxt data |
---|
| 134 | sageBuffer = (byte*)sageInf.getBuffer(); |
---|
| 135 | if (useDXT) |
---|
| 136 | memcpy(sageBuffer, dxt, width*height*4/8); |
---|
| 137 | else |
---|
| 138 | memcpy(sageBuffer, rgba, width*height*3); |
---|
| 139 | sageInf.swapBuffer(); |
---|
| 140 | |
---|
| 141 | // get buffer from SAGE and fill it with dxt data |
---|
| 142 | sageBuffer = (byte*)sageInf.getBuffer(); |
---|
| 143 | if (useDXT) |
---|
| 144 | memcpy(sageBuffer, dxt, width*height*4/8); |
---|
| 145 | else |
---|
| 146 | memcpy(sageBuffer, rgba, width*height*3); |
---|
| 147 | sageInf.swapBuffer(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | void onHomeBtn() |
---|
| 152 | { |
---|
| 153 | #if defined(USE_POPPLER) |
---|
| 154 | #if SAGE_POPPLER_VERSION == 5 |
---|
| 155 | pg_index = 0; |
---|
| 156 | page = poppler_document_get_page(document, pg_index); |
---|
| 157 | #endif |
---|
| 158 | #if SAGE_POPPLER_VERSION == 12 |
---|
| 159 | pg_index = 1; |
---|
| 160 | doc->displayPageSlice(splashOut, pg_index, x_resolution, x_resolution, 0, gTrue, gFalse, gFalse, 0, 0, pg_w, pg_h); |
---|
| 161 | #endif |
---|
| 162 | #else |
---|
| 163 | MagickSetImageIndex(wand, 0); |
---|
| 164 | #endif |
---|
| 165 | sage::printLog("Page %d of %d", 1, numImages); |
---|
| 166 | getRGBA(); |
---|
| 167 | swapBuffer(); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | void onEndBtn() |
---|
| 172 | { |
---|
| 173 | #if defined(USE_POPPLER) |
---|
| 174 | #if SAGE_POPPLER_VERSION == 5 |
---|
| 175 | pg_index = numImages-1; |
---|
| 176 | page = poppler_document_get_page(document, pg_index); |
---|
| 177 | #endif |
---|
| 178 | #if SAGE_POPPLER_VERSION == 12 |
---|
| 179 | pg_index = numImages; |
---|
| 180 | doc->displayPageSlice(splashOut, pg_index, x_resolution, x_resolution, 0, gTrue, gFalse, gFalse, 0, 0, pg_w, pg_h); |
---|
| 181 | #endif |
---|
| 182 | #else |
---|
| 183 | MagickSetImageIndex(wand, numImages-1); |
---|
| 184 | #endif |
---|
| 185 | sage::printLog("Page %d of %d", numImages, numImages); |
---|
| 186 | getRGBA(); |
---|
| 187 | swapBuffer(); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | void onPrevBtn() |
---|
| 191 | { |
---|
| 192 | #if defined(USE_POPPLER) |
---|
| 193 | #if SAGE_POPPLER_VERSION == 5 |
---|
| 194 | pg_index = pg_index - 1; |
---|
| 195 | if (pg_index < 0) pg_index = 0; |
---|
| 196 | page = poppler_document_get_page(document, pg_index); |
---|
| 197 | sage::printLog("Page %ld of %d", pg_index+1, numImages); |
---|
| 198 | #endif |
---|
| 199 | #if SAGE_POPPLER_VERSION == 12 |
---|
| 200 | pg_index = pg_index - 1; |
---|
| 201 | if (pg_index < 1) pg_index = 1; |
---|
| 202 | doc->displayPageSlice(splashOut, pg_index, x_resolution, x_resolution, 0, gTrue, gFalse, gFalse, 0, 0, pg_w, pg_h); |
---|
| 203 | sage::printLog("Page %ld of %d", pg_index, numImages); |
---|
| 204 | #endif |
---|
| 205 | #else |
---|
| 206 | MagickPreviousImage(wand); |
---|
| 207 | sage::printLog("Page %ld of %d", MagickGetImageIndex(wand)+1, numImages); |
---|
| 208 | #endif |
---|
| 209 | getRGBA(); |
---|
| 210 | swapBuffer(); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | void onNextBtn() |
---|
| 215 | { |
---|
| 216 | #if defined(USE_POPPLER) |
---|
| 217 | #if SAGE_POPPLER_VERSION == 5 |
---|
| 218 | pg_index = pg_index + 1; |
---|
| 219 | if (pg_index >= numImages) pg_index = numImages-1; |
---|
| 220 | page = poppler_document_get_page(document, pg_index); |
---|
| 221 | sage::printLog("Page %ld of %d", pg_index+1, numImages); |
---|
| 222 | #endif |
---|
| 223 | #if SAGE_POPPLER_VERSION == 12 |
---|
| 224 | pg_index = pg_index + 1; |
---|
| 225 | if (pg_index > numImages) pg_index = numImages; |
---|
| 226 | doc->displayPageSlice(splashOut, pg_index, x_resolution, x_resolution, 0, gTrue, gFalse, gFalse, 0, 0, pg_w, pg_h); |
---|
| 227 | sage::printLog("Page %ld of %d", pg_index, numImages); |
---|
| 228 | #endif |
---|
| 229 | #else |
---|
| 230 | MagickNextImage(wand); |
---|
| 231 | sage::printLog("Page %ld of %d", MagickGetImageIndex(wand)+1, numImages); |
---|
| 232 | #endif |
---|
| 233 | getRGBA(); |
---|
| 234 | swapBuffer(); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | |
---|
| 238 | #if defined(USE_POPPLER) |
---|
| 239 | #if SAGE_POPPLER_VERSION == 5 |
---|
| 240 | static void |
---|
| 241 | print_index (PopplerIndexIter *iter) |
---|
| 242 | { |
---|
| 243 | do |
---|
| 244 | { |
---|
| 245 | PopplerAction *action; |
---|
| 246 | PopplerIndexIter *child; |
---|
| 247 | |
---|
| 248 | action = poppler_index_iter_get_action (iter); |
---|
| 249 | sage::printLog ("Action: %d", action->type); |
---|
| 250 | poppler_action_free (action); |
---|
| 251 | child = poppler_index_iter_get_child (iter); |
---|
| 252 | if (child) |
---|
| 253 | print_index (child); |
---|
| 254 | poppler_index_iter_free (child); |
---|
| 255 | } |
---|
| 256 | while (poppler_index_iter_next (iter)); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | static void |
---|
| 260 | print_document_info (PopplerDocument *document) |
---|
| 261 | { |
---|
| 262 | gchar *title, *format, *author, *subject, *keywords, *creator, *producer, *linearized; |
---|
| 263 | GTime creation_date, mod_date; |
---|
| 264 | PopplerPageLayout layout; |
---|
| 265 | PopplerPageMode mode; |
---|
| 266 | PopplerViewerPreferences view_prefs; |
---|
| 267 | PopplerFontInfo *font_info; |
---|
| 268 | PopplerFontsIter *fonts_iter; |
---|
| 269 | PopplerIndexIter *index_iter; |
---|
| 270 | GEnumValue *enum_value; |
---|
| 271 | |
---|
| 272 | g_object_get (document, |
---|
| 273 | "title", &title, |
---|
| 274 | "format", &format, |
---|
| 275 | "author", &author, |
---|
| 276 | "subject", &subject, |
---|
| 277 | "keywords", &keywords, |
---|
| 278 | "creation-date", &creation_date, |
---|
| 279 | "mod-date", &mod_date, |
---|
| 280 | "creator", &creator, |
---|
| 281 | "producer", &producer, |
---|
| 282 | "linearized", &linearized, |
---|
| 283 | "page-mode", &mode, |
---|
| 284 | "page-layout", &layout, |
---|
| 285 | "viewer-preferences", &view_prefs, |
---|
| 286 | NULL); |
---|
| 287 | |
---|
| 288 | sage::printLog ("\t---------------------------------------------------------"); |
---|
| 289 | sage::printLog ("\tDocument Metadata"); |
---|
| 290 | sage::printLog ("\t---------------------------------------------------------"); |
---|
| 291 | if (title) sage::printLog ("\ttitle:\t\t%s", title); |
---|
| 292 | if (format) sage::printLog ("\tformat:\t\t%s", format); |
---|
| 293 | if (author) sage::printLog ("\tauthor:\t\t%s", author); |
---|
| 294 | if (subject) sage::printLog ("\tsubject:\t%s", subject); |
---|
| 295 | if (keywords) sage::printLog ("\tkeywords:\t%s", keywords); |
---|
| 296 | if (creator) sage::printLog ("\tcreator:\t%s", creator); |
---|
| 297 | if (producer) sage::printLog ("\tproducer:\t%s", producer); |
---|
| 298 | if (linearized) sage::printLog ("\tlinearized:\t%s", linearized); |
---|
| 299 | |
---|
| 300 | enum_value = g_enum_get_value ((GEnumClass *) g_type_class_peek (POPPLER_TYPE_PAGE_MODE), mode); |
---|
| 301 | sage::printLog ("\tpage mode:\t%s", enum_value->value_name); |
---|
| 302 | enum_value = g_enum_get_value ((GEnumClass *) g_type_class_peek (POPPLER_TYPE_PAGE_LAYOUT), layout); |
---|
| 303 | sage::printLog ("\tpage layout:\t%s", enum_value->value_name); |
---|
| 304 | |
---|
| 305 | sage::printLog ("\tcreation date:\t%d", creation_date); |
---|
| 306 | sage::printLog ("\tmodified date:\t%d", mod_date); |
---|
| 307 | |
---|
| 308 | sage::printLog ("\tfonts:"); |
---|
| 309 | font_info = poppler_font_info_new (document); |
---|
| 310 | while (poppler_font_info_scan (font_info, 20, &fonts_iter)) { |
---|
| 311 | if (fonts_iter) { |
---|
| 312 | do { |
---|
| 313 | sage::printLog ("\t\t\t%s", poppler_fonts_iter_get_name (fonts_iter)); |
---|
| 314 | } while (poppler_fonts_iter_next (fonts_iter)); |
---|
| 315 | poppler_fonts_iter_free (fonts_iter); |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | poppler_font_info_free (font_info); |
---|
| 319 | |
---|
| 320 | index_iter = poppler_index_iter_new (document); |
---|
| 321 | if (index_iter) |
---|
| 322 | { |
---|
| 323 | sage::printLog ("\tindex:"); |
---|
| 324 | print_index (index_iter); |
---|
| 325 | poppler_index_iter_free (index_iter); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | g_free (title); |
---|
| 329 | g_free (format); |
---|
| 330 | g_free (author); |
---|
| 331 | g_free (subject); |
---|
| 332 | g_free (keywords); |
---|
| 333 | g_free (creator); |
---|
| 334 | g_free (producer); |
---|
| 335 | g_free (linearized); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | #endif |
---|
| 339 | #if SAGE_POPPLER_VERSION == 12 |
---|
| 340 | // ----------------------------------------------------------------------------- |
---|
| 341 | static void printInfoString(Dict *infoDict, char *key, char *text, |
---|
| 342 | UnicodeMap *uMap) { |
---|
| 343 | Object obj; |
---|
| 344 | GooString *s1; |
---|
| 345 | GBool isUnicode; |
---|
| 346 | Unicode u; |
---|
| 347 | char buf[8]; |
---|
| 348 | int i, n; |
---|
| 349 | |
---|
| 350 | if (infoDict->lookup(key, &obj)->isString()) { |
---|
| 351 | fputs(text, stdout); |
---|
| 352 | s1 = obj.getString(); |
---|
| 353 | if ((s1->getChar(0) & 0xff) == 0xfe && |
---|
| 354 | (s1->getChar(1) & 0xff) == 0xff) { |
---|
| 355 | isUnicode = gTrue; |
---|
| 356 | i = 2; |
---|
| 357 | } else { |
---|
| 358 | isUnicode = gFalse; |
---|
| 359 | i = 0; |
---|
| 360 | } |
---|
| 361 | while (i < obj.getString()->getLength()) { |
---|
| 362 | if (isUnicode) { |
---|
| 363 | u = ((s1->getChar(i) & 0xff) << 8) | |
---|
| 364 | (s1->getChar(i+1) & 0xff); |
---|
| 365 | i += 2; |
---|
| 366 | } else { |
---|
| 367 | u = pdfDocEncoding[s1->getChar(i) & 0xff]; |
---|
| 368 | ++i; |
---|
| 369 | } |
---|
| 370 | n = uMap->mapUnicode(u, buf, sizeof(buf)); |
---|
| 371 | fwrite(buf, 1, n, stdout); |
---|
| 372 | } |
---|
| 373 | fputc('\n', stdout); |
---|
| 374 | } |
---|
| 375 | obj.free(); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | |
---|
| 379 | static void printInfoDate(Dict *infoDict, char *key, char *text) { |
---|
| 380 | Object obj; |
---|
| 381 | char *s; |
---|
| 382 | int year, mon, day, hour, min, sec, tz_hour, tz_minute; |
---|
| 383 | char tz; |
---|
| 384 | struct tm tmStruct; |
---|
| 385 | char buf[256]; |
---|
| 386 | |
---|
| 387 | if (infoDict->lookup(key, &obj)->isString()) { |
---|
| 388 | fputs(text, stdout); |
---|
| 389 | s = obj.getString()->getCString(); |
---|
| 390 | // TODO do something with the timezone info |
---|
| 391 | if ( parseDateString( s, &year, &mon, &day, &hour, &min, &sec, &tz, &tz_hour, &tz_minute ) ) { |
---|
| 392 | tmStruct.tm_year = year - 1900; |
---|
| 393 | tmStruct.tm_mon = mon - 1; |
---|
| 394 | tmStruct.tm_mday = day; |
---|
| 395 | tmStruct.tm_hour = hour; |
---|
| 396 | tmStruct.tm_min = min; |
---|
| 397 | tmStruct.tm_sec = sec; |
---|
| 398 | tmStruct.tm_wday = -1; |
---|
| 399 | tmStruct.tm_yday = -1; |
---|
| 400 | tmStruct.tm_isdst = -1; |
---|
| 401 | // compute the tm_wday and tm_yday fields |
---|
| 402 | if (mktime(&tmStruct) != (time_t)-1 && |
---|
| 403 | strftime(buf, sizeof(buf), "%c", &tmStruct)) { |
---|
| 404 | fputs(buf, stdout); |
---|
| 405 | } else { |
---|
| 406 | fputs(s, stdout); |
---|
| 407 | } |
---|
| 408 | } else { |
---|
| 409 | fputs(s, stdout); |
---|
| 410 | } |
---|
| 411 | fputc('\n', stdout); |
---|
| 412 | } |
---|
| 413 | obj.free(); |
---|
| 414 | } |
---|
| 415 | #endif |
---|
| 416 | #endif |
---|
| 417 | |
---|
| 418 | |
---|
| 419 | // ----------------------------------------------------------------------------- |
---|
| 420 | |
---|
| 421 | |
---|
| 422 | |
---|
| 423 | int main(int argc,char **argv) |
---|
| 424 | { |
---|
| 425 | unsigned int window_width=-1, window_height=-1; // sage window size |
---|
| 426 | |
---|
| 427 | sage::initUtil(); |
---|
| 428 | |
---|
| 429 | // parse command line arguments |
---|
| 430 | if (argc < 2){ |
---|
| 431 | sage::printLog("PDF> pdfviewer filename [width] [height] [-show_original] [-page num]"); |
---|
| 432 | return 0; |
---|
| 433 | } |
---|
| 434 | for (int argNum=2; argNum<argc; argNum++) |
---|
| 435 | { |
---|
| 436 | if (strcmp(argv[argNum], "-show_original") == 0) { |
---|
| 437 | useDXT = false; |
---|
| 438 | } |
---|
| 439 | else if(strcmp(argv[argNum], "-page") == 0) { |
---|
| 440 | int p = atoi(argv[argNum+1]); |
---|
| 441 | if (p != 0) |
---|
| 442 | firstPage = p-1; |
---|
| 443 | argNum++; |
---|
| 444 | } |
---|
| 445 | else if(atoi(argv[argNum]) != 0 && atoi(argv[argNum+1]) != 0) { |
---|
| 446 | window_width = atoi( argv[argNum] ); |
---|
| 447 | window_height = atoi( argv[argNum+1] ); |
---|
| 448 | argNum++; // increment because we read two args here |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | |
---|
| 453 | fileName = string(argv[1]); |
---|
| 454 | |
---|
| 455 | #if defined(USE_POPPLER) |
---|
| 456 | #if SAGE_POPPLER_VERSION == 5 |
---|
| 457 | g_type_init(); |
---|
| 458 | sage::printLog("PDF> Poppler version %s", poppler_get_version()); |
---|
| 459 | backend = poppler_get_backend (); |
---|
| 460 | enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_BACKEND), backend); |
---|
| 461 | sage::printLog ("PDF> Backend is %s", enum_value->value_name); |
---|
| 462 | |
---|
| 463 | error = NULL; |
---|
| 464 | const string filename = string((const char*)"file://") + string(argv[1]); |
---|
| 465 | document = poppler_document_new_from_file (filename.c_str(), NULL, &error); |
---|
| 466 | if (document == NULL) { |
---|
| 467 | sage::printLog("PDF> error for [%s]: %s", filename.c_str(), error->message); |
---|
| 468 | exit(0); |
---|
| 469 | } |
---|
| 470 | // get the number of pages |
---|
| 471 | numImages = poppler_document_get_n_pages(document); |
---|
| 472 | |
---|
| 473 | pg_index = 0; // index starts at 0 |
---|
| 474 | page = poppler_document_get_page(document, pg_index); |
---|
| 475 | if (page == NULL) { |
---|
| 476 | sage::printLog("PDF> error for [%s]: %s", filename.c_str()); |
---|
| 477 | exit(0); |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | // page size |
---|
| 481 | double mwidth, mheight; |
---|
| 482 | poppler_page_get_size (page, &mwidth, &mheight); |
---|
| 483 | sage::printLog ("PDF> page size: %g by %g ", mwidth, mheight); |
---|
| 484 | |
---|
| 485 | x_resolution = 200.0; |
---|
| 486 | pg_w = mwidth * (x_resolution / 72.0); |
---|
| 487 | pg_h = mheight * (x_resolution / 72.0); |
---|
| 488 | |
---|
| 489 | if (pg_w > 4096) { |
---|
| 490 | pg_w = 4096.0; |
---|
| 491 | pg_h = 4096.0 / (mwidth/mheight); |
---|
| 492 | x_resolution = pg_w * 72.0 / mwidth; |
---|
| 493 | } |
---|
| 494 | if (pg_h > 4096) { |
---|
| 495 | pg_w = 4096.0 / (mheight/mwidth); |
---|
| 496 | pg_h = 4096.0; |
---|
| 497 | x_resolution = pg_h * 72.0 / mheight; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | width = (unsigned int)pg_w; |
---|
| 501 | height = (unsigned int)pg_h; |
---|
| 502 | sage::printLog("PDF> Crop @ %d DPI: width %d height %d", int(x_resolution), int(pg_w), int(pg_h)); |
---|
| 503 | |
---|
| 504 | pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height); |
---|
| 505 | gdk_pixbuf_fill (pixbuf, 0xffffffff); |
---|
| 506 | |
---|
| 507 | // print some information |
---|
| 508 | print_document_info (document); |
---|
| 509 | |
---|
| 510 | #endif |
---|
| 511 | #if SAGE_POPPLER_VERSION == 12 |
---|
| 512 | GooString *fileName; |
---|
| 513 | GooString *ownerPW, *userPW; |
---|
| 514 | Object info; |
---|
| 515 | UnicodeMap *uMap; |
---|
| 516 | |
---|
| 517 | // read config file |
---|
| 518 | globalParams = new GlobalParams(); |
---|
| 519 | |
---|
| 520 | // get mapping to output encoding |
---|
| 521 | if (!(uMap = globalParams->getTextEncoding())) { |
---|
| 522 | sage::printLog("PDF> Couldn't get text encoding"); |
---|
| 523 | } |
---|
| 524 | |
---|
| 525 | fileName = new GooString(argv[1]); |
---|
| 526 | ownerPW = NULL; |
---|
| 527 | userPW = NULL; |
---|
| 528 | doc = new PDFDoc(fileName, ownerPW, userPW, NULL); |
---|
| 529 | sage::printLog("PDF> file [%s] is OK: %d", argv[1], doc->isOk() ); |
---|
| 530 | numImages = doc->getNumPages(); |
---|
| 531 | sage::printLog("PDF> num pages: %d", numImages ); |
---|
| 532 | |
---|
| 533 | SplashColor paperColor; |
---|
| 534 | paperColor[0] = 255; |
---|
| 535 | paperColor[1] = 255; |
---|
| 536 | paperColor[2] = 255; |
---|
| 537 | |
---|
| 538 | splashOut = new SplashOutputDev(splashModeRGB8, 3, gFalse, paperColor); |
---|
| 539 | splashOut->startDoc(doc->getXRef()); |
---|
| 540 | |
---|
| 541 | pg_index = firstPage+1; |
---|
| 542 | double mwidth = doc->getPageMediaWidth(pg_index); |
---|
| 543 | double mheight = doc->getPageMediaHeight(pg_index); |
---|
| 544 | sage::printLog("PDF> Media width %d height %d", int(mwidth), int(mheight)); |
---|
| 545 | |
---|
| 546 | x_resolution = 200.0; |
---|
| 547 | pg_w = mwidth * (x_resolution / 72.0); |
---|
| 548 | pg_h = mheight * (x_resolution / 72.0); |
---|
| 549 | |
---|
| 550 | if (pg_w > 4096) { |
---|
| 551 | pg_w = 4096.0; |
---|
| 552 | pg_h = 4096.0 / (mwidth/mheight); |
---|
| 553 | x_resolution = pg_w * 72.0 / mwidth; |
---|
| 554 | } |
---|
| 555 | if (pg_h > 4096) { |
---|
| 556 | pg_w = 4096.0 / (mheight/mwidth); |
---|
| 557 | pg_h = 4096.0; |
---|
| 558 | x_resolution = pg_h * 72.0 / mheight; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | width = pg_w; |
---|
| 562 | height = pg_h; |
---|
| 563 | sage::printLog("PDF> Crop @ %d DPI: width %d height %d", int(x_resolution), int(pg_w), int(pg_h)); |
---|
| 564 | |
---|
| 565 | doc->displayPageSlice(splashOut, 1, x_resolution, x_resolution, 0, gTrue, gFalse, gFalse, 0, 0, pg_w, pg_h); |
---|
| 566 | |
---|
| 567 | // print doc info |
---|
| 568 | doc->getDocInfo(&info); |
---|
| 569 | if (info.isDict()) { |
---|
| 570 | printInfoString(info.getDict(), (char*)"Title", (char*)"Title: ", uMap); |
---|
| 571 | printInfoString(info.getDict(), (char*)"Subject", (char*)"Subject: ", uMap); |
---|
| 572 | printInfoString(info.getDict(), (char*)"Keywords", (char*)"Keywords: ", uMap); |
---|
| 573 | printInfoString(info.getDict(), (char*)"Author", (char*)"Author: ", uMap); |
---|
| 574 | printInfoString(info.getDict(), (char*)"Creator", (char*)"Creator: ", uMap); |
---|
| 575 | printInfoString(info.getDict(), (char*)"Producer", (char*)"Producer: ", uMap); |
---|
| 576 | printInfoDate(info.getDict(), (char*)"CreationDate", (char*)"CreationDate: "); |
---|
| 577 | printInfoDate(info.getDict(), (char*)"ModDate", (char*)"ModDate: "); |
---|
| 578 | } |
---|
| 579 | info.free(); |
---|
| 580 | #endif |
---|
| 581 | #else |
---|
| 582 | // read file |
---|
| 583 | wand=NewMagickWand(); |
---|
| 584 | |
---|
| 585 | // set the resolution (pixel density) |
---|
| 586 | MagickSetResolution(wand, 200,200); |
---|
| 587 | |
---|
| 588 | status=MagickReadImage(wand, fileName.data()); |
---|
| 589 | if (status == MagickFalse) |
---|
| 590 | ThrowWandException(wand); |
---|
| 591 | numImages = MagickGetNumberImages(wand); |
---|
| 592 | MagickSetImageIndex(wand, firstPage); |
---|
| 593 | |
---|
| 594 | // get the image size |
---|
| 595 | width = MagickGetImageWidth(wand); |
---|
| 596 | height = MagickGetImageHeight(wand); |
---|
| 597 | |
---|
| 598 | // ------- TODO: need to keep track of images that have been flipped already... |
---|
| 599 | |
---|
| 600 | // crop the image if necessary to make sure it's a multiple of 4 |
---|
| 601 | if (useDXT) |
---|
| 602 | { |
---|
| 603 | if (width%4 != 0 || height%4 != 0) |
---|
| 604 | { |
---|
| 605 | fprintf(stderr, "\n**** Image cropped a few pixels to be a multiple of 4 for dxt"); |
---|
| 606 | width -= width%4; |
---|
| 607 | height -= height%4; |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | // flip the image to have the correct orientation for dxt |
---|
| 611 | MagickFlipImage(wand); |
---|
| 612 | } |
---|
| 613 | #endif |
---|
| 614 | |
---|
| 615 | // allocate buffers |
---|
| 616 | rgba = (byte*) memalign(16, width*height*4); |
---|
| 617 | if (useDXT) |
---|
| 618 | dxt = (byte*) memalign(16, width*height*4/8); |
---|
| 619 | |
---|
| 620 | // get the first page |
---|
| 621 | getRGBA(); |
---|
| 622 | |
---|
| 623 | // if the user didn't specify the window size, use the image size |
---|
| 624 | if (window_height == -1 && window_width == -1) |
---|
| 625 | { |
---|
| 626 | window_width = width; |
---|
| 627 | window_height = height; |
---|
| 628 | } |
---|
| 629 | |
---|
| 630 | // initialize SAIL |
---|
| 631 | |
---|
| 632 | // Search for a configuration file |
---|
| 633 | char *tmpconf = getenv("SAGE_APP_CONFIG"); |
---|
| 634 | if (tmpconf) { |
---|
| 635 | sage::printLog("PDFViewer> found SAGE_APP_CONFIG variable: [%s]", tmpconf); |
---|
| 636 | scfg.init(tmpconf); |
---|
| 637 | } |
---|
| 638 | else { |
---|
| 639 | sage::printLog("PDFViewer> using default pdfviewer.conf"); |
---|
| 640 | scfg.init((char*)"pdfviewer.conf"); |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | scfg.setAppName("pdfviewer"); |
---|
| 644 | |
---|
| 645 | scfg.resX = width; |
---|
| 646 | scfg.resY = height; |
---|
| 647 | |
---|
| 648 | // if it hasn't been specified by the config file, use the app-determined size |
---|
| 649 | if (scfg.winWidth == -1 || scfg.winHeight == -1) { |
---|
| 650 | scfg.winWidth = window_width; |
---|
| 651 | scfg.winHeight = window_height; |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | if (useDXT) |
---|
| 655 | { |
---|
| 656 | scfg.pixFmt = PIXFMT_DXT; |
---|
| 657 | scfg.rowOrd = BOTTOM_TO_TOP; |
---|
| 658 | } |
---|
| 659 | else |
---|
| 660 | { |
---|
| 661 | scfg.pixFmt = PIXFMT_888; |
---|
| 662 | scfg.rowOrd = TOP_TO_BOTTOM; |
---|
| 663 | } |
---|
| 664 | |
---|
| 665 | sageInf.init(scfg); |
---|
| 666 | |
---|
| 667 | // finally swap the first buffer |
---|
| 668 | swapBuffer(); |
---|
| 669 | |
---|
| 670 | |
---|
| 671 | // Wait the end |
---|
| 672 | while (1) |
---|
| 673 | { |
---|
| 674 | usleep(50000); // so that we don't keep spinning too frequently (0.5s) |
---|
| 675 | sageMessage msg; |
---|
| 676 | if (sageInf.checkMsg(msg, false) > 0) { |
---|
| 677 | char *data = (char*) msg.getData(); |
---|
| 678 | |
---|
| 679 | switch (msg.getCode()) { |
---|
| 680 | case APP_QUIT: |
---|
| 681 | // release the memory |
---|
| 682 | free(dxt); |
---|
| 683 | free(rgba); |
---|
| 684 | #if ! defined(USE_POPPLER) |
---|
| 685 | DestroyMagickWand(wand); |
---|
| 686 | #endif |
---|
| 687 | |
---|
| 688 | sageInf.shutdown(); |
---|
| 689 | exit(1); |
---|
| 690 | break; |
---|
| 691 | |
---|
| 692 | case EVT_CLICK: |
---|
| 693 | // Click event x and y location normalized to size of window |
---|
| 694 | float clickX, clickY; |
---|
| 695 | |
---|
| 696 | // Ckick device Id, button Id, and is down flag |
---|
| 697 | int clickDeviceId, clickButtonId, clickIsDown, clickEvent; |
---|
| 698 | |
---|
| 699 | // Parse message |
---|
| 700 | sscanf(data, |
---|
| 701 | "%d %f %f %d %d %d", |
---|
| 702 | &clickDeviceId, &clickX, &clickY, |
---|
| 703 | &clickButtonId, &clickIsDown, &clickEvent); |
---|
| 704 | |
---|
| 705 | // record the click position so we know how far we moved |
---|
| 706 | if (clickIsDown && clickEvent == EVT_PAN) { |
---|
| 707 | lastX = clickX; |
---|
| 708 | lastY = clickY; |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | |
---|
| 712 | if (clickIsDown) { |
---|
| 713 | bool doSwap = false; |
---|
| 714 | #if ! defined(USE_POPPLER) |
---|
| 715 | if (clickButtonId==1) |
---|
| 716 | doSwap = MagickPreviousImage(wand); |
---|
| 717 | else if(clickButtonId==2) |
---|
| 718 | doSwap = MagickNextImage(wand); |
---|
| 719 | #endif |
---|
| 720 | |
---|
| 721 | // if everything went well, swap the new page |
---|
| 722 | if (doSwap) { |
---|
| 723 | getRGBA(); |
---|
| 724 | swapBuffer(); |
---|
| 725 | } |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | |
---|
| 729 | break; |
---|
| 730 | |
---|
| 731 | |
---|
| 732 | case EVT_PAN: |
---|
| 733 | |
---|
| 734 | // Pan event properties |
---|
| 735 | int panDeviceId; |
---|
| 736 | bool doSwap = false; |
---|
| 737 | |
---|
| 738 | // Pan event x and y location and change in x, y and z direction |
---|
| 739 | // normalized to size of window |
---|
| 740 | float startX, startY, panDX, panDY, panDZ; |
---|
| 741 | sscanf(data, |
---|
| 742 | "%d %f %f %f %f %f", |
---|
| 743 | &panDeviceId, &startX, &startY, &panDX, &panDY, &panDZ); |
---|
| 744 | |
---|
| 745 | // keep track of distance |
---|
| 746 | dist += panDX; |
---|
| 747 | |
---|
| 748 | // we started a new drag |
---|
| 749 | if (lastX != startX) { |
---|
| 750 | lastX = startX; |
---|
| 751 | dist = 0; |
---|
| 752 | } |
---|
| 753 | |
---|
| 754 | // if we dragged more than a certain distance, change a page |
---|
| 755 | else if( fabs(dist) > 0.07 ) { |
---|
| 756 | #if ! defined(USE_POPPLER) |
---|
| 757 | if (dist > 0) |
---|
| 758 | doSwap = MagickPreviousImage(wand); |
---|
| 759 | else |
---|
| 760 | doSwap = MagickNextImage(wand); |
---|
| 761 | #endif |
---|
| 762 | |
---|
| 763 | // reset the counter |
---|
| 764 | lastX = startX; |
---|
| 765 | dist = 0; |
---|
| 766 | |
---|
| 767 | if (doSwap) { |
---|
| 768 | getRGBA(); |
---|
| 769 | swapBuffer(); |
---|
| 770 | //sprintf(l, "Page %ld of %d", MagickGetImageIndex(wand)+1, numImages); |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | break; |
---|
| 775 | } // end switch |
---|
| 776 | } // end if |
---|
| 777 | } // end while |
---|
| 778 | |
---|
| 779 | return 0; |
---|
| 780 | } |
---|
| 781 | |
---|