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