terom@2: #include "shared/log.h" terom@2: terom@17: #include "pngtile.h" terom@17: terom@2: #include terom@2: #include terom@2: #include terom@2: terom@2: /** terom@2: * Command-line options terom@2: */ terom@2: static const struct option options[] = { terom@6: { "help", false, NULL, 'h' }, terom@6: { "quiet", false, NULL, 'q' }, terom@6: { "verbose", false, NULL, 'v' }, terom@6: { "debug", false, NULL, 'D' }, terom@6: { "force-update", false, NULL, 'U' }, terom@56: { "no-update", false, NULL, 'N' }, terom@52: { "background", true, NULL, 'B' }, terom@9: { "width", true, NULL, 'W' }, terom@9: { "height", true, NULL, 'H' }, terom@9: { "x", true, NULL, 'x' }, terom@9: { "y", true, NULL, 'y' }, terom@34: { "zoom", true, NULL, 'z' }, terom@19: { "threads", true, NULL, 'j' }, terom@6: { 0, 0, 0, 0 } terom@2: }; terom@2: terom@2: /** terom@2: * Print usage/help info on stderr terom@2: */ terom@2: void help (const char *argv0) terom@2: { terom@2: fprintf(stderr, "Usage: %s [options] [...]\n", argv0); terom@2: fprintf(stderr, terom@68: "Open each of the given image files, check cache status, optionally update their cache, display image info, and\n" terom@68: "optionally render a tile of each.\n" terom@2: "\n" terom@2: "\t-h, --help show this help and exit\n" terom@2: "\t-q, --quiet supress informational output\n" terom@2: "\t-v, --verbose display more informational output\n" terom@2: "\t-D, --debug equivalent to -v\n" terom@6: "\t-U, --force-update unconditionally update image caches\n" terom@56: "\t-N, --no-update do not update the image cache\n" terom@73: "\t-B, --background set background pattern for sparse cache file: 0xHH..\n" terom@9: "\t-W, --width set tile width\n" terom@9: "\t-H, --height set tile height\n" terom@9: "\t-x, --x set tile x offset\n" terom@9: "\t-y, --y set tile z offset\n" terom@34: "\t-z, --zoom set zoom factor (<0)\n" terom@19: "\t-j, --threads number of threads\n" terom@2: ); terom@2: } terom@2: terom@2: int main (int argc, char **argv) terom@2: { terom@2: int opt; terom@56: bool force_update = false, no_update = false; terom@34: struct pt_tile_info ti = {0, 0, 0, 0, 0}; terom@52: struct pt_image_params update_params = { }; terom@19: int threads = 2; terom@19: int tmp, err; terom@2: terom@2: // parse arguments terom@56: while ((opt = getopt_long(argc, argv, "hqvDUNB:W:H:x:y:z:j:", options, NULL)) != -1) { terom@2: switch (opt) { terom@2: case 'h': terom@2: // display help terom@2: help(argv[0]); terom@2: terom@2: return EXIT_SUCCESS; terom@2: terom@2: case 'q': terom@2: // supress excess log output terom@2: set_log_level(LOG_WARN); terom@2: terom@2: break; terom@2: terom@2: case 'v': terom@2: case 'D': terom@2: // display additional output terom@2: set_log_level(LOG_DEBUG); terom@2: terom@2: break; terom@6: terom@6: case 'U': terom@6: // force update of image caches terom@6: force_update = true; terom@6: terom@6: break; terom@56: terom@56: case 'N': terom@56: // supress update of image caches terom@56: no_update = true; terom@56: terom@56: break; terom@2: terom@52: case 'B': terom@52: // background pattern terom@52: { terom@56: unsigned int b1 = 0, b2 = 0, b3 = 0, b4 = 0; terom@52: terom@52: // parse 0xXXXXXXXX terom@56: if (sscanf(optarg, "0x%02x%02x%02x%02x", &b1, &b2, &b3, &b4) < 1) terom@52: FATAL("Invalid hex value for -B/--background: %s", optarg); terom@52: terom@52: // store terom@52: update_params.background_color[0] = b1; terom@52: update_params.background_color[1] = b2; terom@52: update_params.background_color[2] = b3; terom@52: update_params.background_color[3] = b4; terom@52: terom@52: } break; terom@52: terom@9: case 'W': terom@9: ti.width = strtol(optarg, NULL, 0); break; terom@9: terom@9: case 'H': terom@9: ti.height = strtol(optarg, NULL, 0); break; terom@9: terom@9: case 'x': terom@9: ti.x = strtol(optarg, NULL, 0); break; terom@9: terom@9: case 'y': terom@9: ti.y = strtol(optarg, NULL, 0); break; terom@9: terom@34: case 'z': terom@34: ti.zoom = strtol(optarg, NULL, 0); break; terom@34: terom@19: case 'j': terom@19: if ((tmp = strtol(optarg, NULL, 0)) < 1) terom@52: FATAL("Invalid value for -j/--threads: %s", optarg); terom@19: terom@19: threads = tmp; break; terom@19: terom@2: case '?': terom@2: // useage error terom@2: help(argv[0]); terom@2: terom@2: return EXIT_FAILURE; terom@2: terom@2: default: terom@2: // getopt??? terom@2: FATAL("getopt_long returned unknown code %d", opt); terom@2: } terom@2: } terom@2: terom@2: // end-of-arguments? terom@2: if (!argv[optind]) terom@2: EXIT_WARN(EXIT_FAILURE, "No images given"); terom@2: terom@2: terom@2: terom@2: struct pt_ctx *ctx = NULL; terom@5: struct pt_image *image = NULL; terom@8: enum pt_cache_status status; terom@2: terom@19: // build ctx terom@19: log_debug("Construct pt_ctx with %d threads", threads); terom@19: terom@19: if ((err = pt_ctx_new(&ctx, threads))) terom@19: EXIT_ERROR(EXIT_FAILURE, "pt_ctx_new: threads=%d", threads); terom@19: terom@19: terom@19: // process each image in turn terom@2: log_debug("Processing %d images...", argc - optind); terom@2: terom@2: for (int i = optind; i < argc; i++) { terom@2: const char *img_path = argv[i]; terom@2: terom@5: log_debug("Loading image from: %s...", img_path); terom@2: terom@2: // open terom@17: if ((err = pt_image_open(&image, ctx, img_path, PT_OPEN_UPDATE))) { terom@17: log_errno("pt_image_open: %s: %s", img_path, pt_strerror(err)); terom@2: continue; terom@5: } terom@2: terom@5: log_info("Opened image at: %s", img_path); terom@5: terom@5: // check if stale terom@8: if ((status = pt_image_status(image)) < 0) { terom@17: log_errno("pt_image_status: %s: %s", img_path, pt_strerror(status)); terom@5: goto error; terom@5: } terom@5: terom@5: // update if stale terom@8: if (status != PT_CACHE_FRESH || force_update) { terom@8: if (status == PT_CACHE_NONE) terom@26: log_debug("\tImage cache is missing"); terom@8: terom@8: else if (status == PT_CACHE_STALE) terom@26: log_debug("\tImage cache is stale"); terom@56: terom@56: else if (status == PT_CACHE_INCOMPAT) terom@56: log_debug("\tImage cache is incompatible"); terom@8: terom@8: else if (status == PT_CACHE_FRESH) terom@26: log_debug("\tImage cache is fresh"); terom@56: terom@56: if (!no_update) { terom@56: log_debug("\tUpdating image cache..."); terom@8: terom@56: if ((err = pt_image_update(image, &update_params))) { terom@56: log_warn_errno("pt_image_update: %s: %s", img_path, pt_strerror(err)); terom@56: } terom@2: terom@56: log_info("\tUpdated image cache"); terom@56: terom@56: } else { terom@56: log_warn("\tSupressing cache update"); terom@5: } terom@5: terom@56: } else { terom@26: log_debug("\tImage cache is fresh"); terom@2: } terom@5: terom@7: // show info terom@56: const struct pt_image_info *info; terom@7: terom@56: if ((err = pt_image_info(image, &info))) { terom@17: log_warn_errno("pt_image_info: %s: %s", img_path, pt_strerror(err)); terom@7: terom@54: } else { terom@56: log_info("\tImage dimensions: %zux%zu", info->img_width, info->img_height); terom@63: log_info("\tImage mtime=%ld, bytes=%zu", (long) info->image_mtime, info->image_bytes); terom@63: log_info("\tCache mtime=%ld, bytes=%zu, blocks=%zu (%zu bytes)", terom@63: (long) info->cache_mtime, info->cache_bytes, info->cache_blocks, info->cache_blocks * 512 terom@54: ); terom@54: } terom@7: terom@9: // render tile? terom@9: if (ti.width && ti.height) { terom@23: char tmp_name[] = "pt-tile-XXXXXX"; terom@23: int fd; terom@23: FILE *out; terom@23: terom@23: // temporary file for output terom@23: if ((fd = mkstemp(tmp_name)) < 0) { terom@23: log_errno("mkstemp"); terom@23: terom@23: continue; terom@23: } terom@9: terom@23: // open out terom@23: if ((out = fdopen(fd, "w")) == NULL) { terom@23: log_errno("fdopen"); terom@23: terom@23: continue; terom@23: } terom@26: terom@26: // ensure it's loaded terom@26: log_debug("\tLoad image cache..."); terom@26: terom@26: if ((err = pt_image_load(image))) terom@26: log_errno("pt_image_load: %s", pt_strerror(err)); terom@23: terom@23: // render terom@26: log_info("\tAsync render tile %zux%zu@(%zu,%zu) -> %s", ti.width, ti.height, ti.x, ti.y, tmp_name); terom@26: terom@23: terom@23: if ((err = pt_image_tile_async(image, &ti, out))) terom@17: log_errno("pt_image_tile: %s: %s", img_path, pt_strerror(err)); terom@9: } terom@5: terom@5: error: terom@5: // cleanup terom@19: // XXX: leak because of async: pt_image_destroy(image); terom@19: ; terom@2: } terom@2: terom@19: log_info("Waiting for images to finish..."); terom@19: terom@19: // wait for tile operations to finish... terom@19: pt_ctx_shutdown(ctx); terom@19: terom@19: log_info("Done"); terom@19: terom@2: return 0; terom@2: } terom@2: