src/util/main.c
author Tero Marttila <terom@fixme.fi>
Mon, 25 Jan 2010 02:39:28 +0200
changeset 68 70737d141172
parent 63 2aab5d906dc8
child 73 5dfb245b814d
permissions -rw-r--r--
docfix --help
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "shared/log.h"
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
     3
#include "pngtile.h"
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
     4
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <getopt.h>
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
#include <stdio.h>
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include <stdbool.h>
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
/**
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
 * Command-line options
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
 */
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
static const struct option options[] = {
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    13
    { "help",           false,  NULL,   'h' },
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    14
    { "quiet",          false,  NULL,   'q' },
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    15
    { "verbose",        false,  NULL,   'v' },
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    16
    { "debug",          false,  NULL,   'D' },
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    17
    { "force-update",   false,  NULL,   'U' },
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    18
    { "no-update",      false,  NULL,   'N' },
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    19
    { "background",     true,   NULL,   'B' },
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    20
    { "width",          true,   NULL,   'W' },
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    21
    { "height",         true,   NULL,   'H' },
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    22
    { "x",              true,   NULL,   'x' },
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    23
    { "y",              true,   NULL,   'y' },
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    24
    { "zoom",           true,   NULL,   'z' },
19
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    25
    { "threads",        true,   NULL,   'j' },
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    26
    { 0,                0,      0,      0   }
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
};
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
/**
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
 * Print usage/help info on stderr
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
 */
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
void help (const char *argv0)
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
{
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    fprintf(stderr, "Usage: %s [options] <image> [...]\n", argv0);
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    fprintf(stderr,
68
70737d141172 docfix --help
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
    36
        "Open each of the given image files, check cache status, optionally update their cache, display image info, and\n"
70737d141172 docfix --help
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
    37
        "optionally render a tile of each.\n"
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        "\n"
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        "\t-h, --help           show this help and exit\n"
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        "\t-q, --quiet          supress informational output\n"
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        "\t-v, --verbose        display more informational output\n"
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        "\t-D, --debug          equivalent to -v\n"
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    43
        "\t-U, --force-update   unconditionally update image caches\n"
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    44
        "\t-N, --no-update      do not update the image cache\n"
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    45
        "\t-B, --background     set background pattern for cache update\n"
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    46
        "\t-W, --width          set tile width\n"
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    47
        "\t-H, --height         set tile height\n"
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    48
        "\t-x, --x              set tile x offset\n"
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    49
        "\t-y, --y              set tile z offset\n"
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    50
        "\t-z, --zoom           set zoom factor (<0)\n"
19
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    51
        "\t-j, --threads        number of threads\n"
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
    );
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
}
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
int main (int argc, char **argv)
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
{
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    int opt;
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    58
    bool force_update = false, no_update = false;
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    59
    struct pt_tile_info ti = {0, 0, 0, 0, 0};
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    60
    struct pt_image_params update_params = { };
19
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    61
    int threads = 2;
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    62
    int tmp, err;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    // parse arguments
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    65
    while ((opt = getopt_long(argc, argv, "hqvDUNB:W:H:x:y:z:j:", options, NULL)) != -1) {
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        switch (opt) {
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
            case 'h':
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
                // display help
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
                help(argv[0]);
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
                
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
                return EXIT_SUCCESS;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
            case 'q':
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
                // supress excess log output
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
                set_log_level(LOG_WARN);
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
                break;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
            case 'v':
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
            case 'D':
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
                // display additional output
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
                set_log_level(LOG_DEBUG);
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
                break;
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    85
            
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    86
            case 'U':
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    87
                // force update of image caches
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    88
                force_update = true;
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    89
                
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 5
diff changeset
    90
                break;
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    91
            
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    92
            case 'N':
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    93
                // supress update of image caches
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    94
                no_update = true;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    95
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    96
                break;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    98
            case 'B':
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    99
                // background pattern
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   100
                {
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   101
                    unsigned int b1 = 0, b2 = 0, b3 = 0, b4 = 0;
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   102
                    
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   103
                    // parse 0xXXXXXXXX
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   104
                    if (sscanf(optarg, "0x%02x%02x%02x%02x", &b1, &b2, &b3, &b4) < 1)
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   105
                        FATAL("Invalid hex value for -B/--background: %s", optarg);
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   106
                    
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   107
                    // store
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   108
                    update_params.background_color[0] = b1;
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   109
                    update_params.background_color[1] = b2;
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   110
                    update_params.background_color[2] = b3;
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   111
                    update_params.background_color[3] = b4;
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   112
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   113
                } break;
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   114
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   115
            case 'W':
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   116
                ti.width = strtol(optarg, NULL, 0); break;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   117
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   118
            case 'H':
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   119
                ti.height = strtol(optarg, NULL, 0); break;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   120
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   121
            case 'x':
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   122
                ti.x = strtol(optarg, NULL, 0); break;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   123
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   124
            case 'y':
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   125
                ti.y = strtol(optarg, NULL, 0); break;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   126
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   127
            case 'z':
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   128
                ti.zoom = strtol(optarg, NULL, 0); break;
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   129
19
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   130
            case 'j':
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   131
                if ((tmp = strtol(optarg, NULL, 0)) < 1)
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   132
                    FATAL("Invalid value for -j/--threads: %s", optarg);
19
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   133
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   134
                threads = tmp; break;
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   135
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
            case '?':
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
                // useage error
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
                help(argv[0]);
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
                return EXIT_FAILURE;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
            default:
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
                // getopt???
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
                FATAL("getopt_long returned unknown code %d", opt);
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
        }
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
    }
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
    // end-of-arguments?
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
    if (!argv[optind])
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
        EXIT_WARN(EXIT_FAILURE, "No images given");
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
    struct pt_ctx *ctx = NULL;
5
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   155
    struct pt_image *image = NULL;
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   156
    enum pt_cache_status status;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
19
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   158
    // build ctx
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   159
    log_debug("Construct pt_ctx with %d threads", threads);
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   160
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   161
    if ((err = pt_ctx_new(&ctx, threads)))
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   162
        EXIT_ERROR(EXIT_FAILURE, "pt_ctx_new: threads=%d", threads);
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   163
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   164
    
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   165
    // process each image in turn
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
    log_debug("Processing %d images...", argc - optind);
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   167
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
    for (int i = optind; i < argc; i++) {
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
        const char *img_path = argv[i];
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
5
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   171
        log_debug("Loading image from: %s...", img_path);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
        // open
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   174
        if ((err = pt_image_open(&image, ctx, img_path, PT_OPEN_UPDATE))) {
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   175
            log_errno("pt_image_open: %s: %s", img_path, pt_strerror(err));
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
            continue;
5
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   177
        }
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
5
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   179
        log_info("Opened image at: %s", img_path);
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   180
        
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   181
        // check if stale
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   182
        if ((status = pt_image_status(image)) < 0) {
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   183
            log_errno("pt_image_status: %s: %s", img_path, pt_strerror(status));
5
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   184
            goto error;
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   185
        }
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   186
        
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   187
        // update if stale
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   188
        if (status != PT_CACHE_FRESH || force_update) {
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   189
            if (status == PT_CACHE_NONE)
26
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   190
                log_debug("\tImage cache is missing");
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   191
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   192
            else if (status == PT_CACHE_STALE)
26
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   193
                log_debug("\tImage cache is stale");
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   194
            
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   195
            else if (status == PT_CACHE_INCOMPAT)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   196
                log_debug("\tImage cache is incompatible");
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   197
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   198
            else if (status == PT_CACHE_FRESH)
26
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   199
                log_debug("\tImage cache is fresh");
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   200
            
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   201
            if (!no_update) {
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   202
                log_debug("\tUpdating image cache...");
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   203
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   204
                if ((err = pt_image_update(image, &update_params))) {
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   205
                    log_warn_errno("pt_image_update: %s: %s", img_path, pt_strerror(err));
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   206
                }
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   207
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   208
                log_info("\tUpdated image cache");
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   209
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   210
            } else {
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   211
                log_warn("\tSupressing cache update");
5
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   212
            }
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   213
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   214
        } else {    
26
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   215
            log_debug("\tImage cache is fresh");
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
        }
5
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   217
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   218
        // show info
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   219
        const struct pt_image_info *info;
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   220
        
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   221
        if ((err = pt_image_info(image, &info))) {
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   222
            log_warn_errno("pt_image_info: %s: %s", img_path, pt_strerror(err));
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   223
54
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   224
        } else {
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   225
            log_info("\tImage dimensions: %zux%zu", info->img_width, info->img_height);
63
2aab5d906dc8 fix printf time_t warning
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   226
            log_info("\tImage mtime=%ld, bytes=%zu", (long) info->image_mtime, info->image_bytes);
2aab5d906dc8 fix printf time_t warning
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   227
            log_info("\tCache mtime=%ld, bytes=%zu, blocks=%zu (%zu bytes)", 
2aab5d906dc8 fix printf time_t warning
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   228
                    (long) info->cache_mtime, info->cache_bytes, info->cache_blocks, info->cache_blocks * 512
54
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   229
            );
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   230
        }
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   231
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   232
        // render tile?
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   233
        if (ti.width && ti.height) {
23
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   234
            char tmp_name[] = "pt-tile-XXXXXX";
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   235
            int fd;
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   236
            FILE *out;
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   237
            
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   238
            // temporary file for output
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   239
            if ((fd = mkstemp(tmp_name)) < 0) {
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   240
                log_errno("mkstemp");
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   241
                
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   242
                continue;
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   243
            }
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   244
23
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   245
            // open out
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   246
            if ((out = fdopen(fd, "w")) == NULL) {
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   247
                log_errno("fdopen");
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   248
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   249
                continue;
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   250
            }
26
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   251
            
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   252
            // ensure it's loaded
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   253
            log_debug("\tLoad image cache...");
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   254
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   255
            if ((err = pt_image_load(image)))
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   256
                log_errno("pt_image_load: %s", pt_strerror(err));
23
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   257
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   258
            // render
26
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   259
            log_info("\tAsync render tile %zux%zu@(%zu,%zu) -> %s", ti.width, ti.height, ti.x, ti.y, tmp_name);
72fd5ba8d8c1 use pt_image_load and fix output indent
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   260
23
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   261
9fd50fc1d223 render async tile to separate tempfile
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   262
            if ((err = pt_image_tile_async(image, &ti, out)))
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   263
                log_errno("pt_image_tile: %s: %s", img_path, pt_strerror(err));
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   264
        }
5
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   265
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   266
error:
4b440fa03183 move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
   267
        // cleanup
19
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   268
        // XXX: leak because of async: pt_image_destroy(image);
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   269
        ;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   270
    }
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   271
19
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   272
    log_info("Waiting for images to finish...");
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   273
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   274
    // wait for tile operations to finish...
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   275
    pt_ctx_shutdown(ctx);
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   276
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   277
    log_info("Done");
ebcc49de97d0 implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   278
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   279
    return 0;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   280
}
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   281