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