From 4d90addd364d2fd654c4cbce70712d1808e85bd2 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 13 Nov 2021 17:29:07 +0100 Subject: [PATCH 1/4] Allow to process images up to 30Kx30K Some integer overflows had to be fixed. --- stitch.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stitch.c b/stitch.c index 336d83e..3284de2 100644 --- a/stitch.c +++ b/stitch.c @@ -176,7 +176,7 @@ struct image *read_png(char *s, int len) { int main(int argc, char **argv) { extern int optind; extern char *optarg; - int i; + long long i; char *outfile = NULL; int tilesize = 256; @@ -288,7 +288,7 @@ int main(int argc, char **argv) { fprintf(stderr, "==Pixel Size: x:%.17g y:%.17g\n", px, py); long long dim = (long long) width * height; - if (dim > 10000 * 10000) { + if (dim > 35000 * 35000) { fprintf(stderr, "that's too big\n"); exit(EXIT_FAILURE); } @@ -297,6 +297,7 @@ int main(int argc, char **argv) { memset(buf, '\0', dim * 4); if (buf == NULL) { fprintf(stderr, "Can't allocate memory for %lld\n", dim * 4); + exit(EXIT_FAILURE); } unsigned int tx, ty; @@ -390,7 +391,7 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } - int x, y; + long long x, y; for (y = 0; y < i->height; y++) { for (x = 0; x < i->width; x++) { int xd = x + xoff; From 7257b8bf5093bfca157c52a478ac73238215e81e Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 13 Nov 2021 17:28:42 +0100 Subject: [PATCH 2/4] Support a new -s option to allow to configure the tile size Some tile sites, like StravaHeatmap, use a 512 pixel tiles --- stitch.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stitch.c b/stitch.c index 3284de2..97cf9c5 100644 --- a/stitch.c +++ b/stitch.c @@ -183,7 +183,7 @@ int main(int argc, char **argv) { int centered = 0; int outfmt = OUTFMT_PNG; unsigned int writeworldfile = FALSE; - while ((i = getopt(argc, argv, "o:t:cf:w")) != -1) { + while ((i = getopt(argc, argv, "o:t:cf:ws:")) != -1) { switch (i) { case 'o': outfile = optarg; @@ -201,6 +201,10 @@ int main(int argc, char **argv) { writeworldfile = TRUE; break; + case 's': + tilesize = atoi(optarg); + break; + case 'f': if (strcmp(optarg, "png") == 0) { outfmt = OUTFMT_PNG; From 1c8fc593a7b9ebe3e8671577ba0759ac856078ef Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 13 Nov 2021 18:05:54 +0100 Subject: [PATCH 3/4] Don't warn for unrecognized format if a tile is missing This is an expected case. --- stitch.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stitch.c b/stitch.c index 97cf9c5..004b782 100644 --- a/stitch.c +++ b/stitch.c @@ -380,7 +380,9 @@ int main(int argc, char **argv) { } else if (data.len >= 2 && memcmp(data.buf, "\xFF\xD8", 2) == 0) { i = read_jpeg(data.buf, data.len); } else { - fprintf(stderr, "Don't recognize file format\n"); + /* if the tile is missing, don't warn */ + if (data.len != 0) + fprintf(stderr, "Don't recognize file format\n"); free(data.buf); curl_easy_cleanup(curl); From ef19aa19897e365ccbb5a84cab6392c6cb12378b Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 13 Nov 2021 21:49:52 +0100 Subject: [PATCH 4/4] Mark the 4 band as Alpha This is required by GDAL tools. --- stitch.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stitch.c b/stitch.c index 004b782..c4e0c34 100644 --- a/stitch.c +++ b/stitch.c @@ -494,7 +494,7 @@ int main(int argc, char **argv) { //TODO : Handle writing to stdout if required if (outfile != NULL) { - fprintf(stderr, "Output PNG: %s\n", outfile); + fprintf(stderr, "Output TIFF: %s\n", outfile); TIFF *tif = (TIFF *) 0; /* TIFF-level descriptor */ GTIF *gtif = (GTIF *) 0; /* GeoKey-level descriptor */ @@ -527,6 +527,10 @@ int main(int argc, char **argv) { TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); + uint16 out[1]; + out[0] = EXTRASAMPLE_ASSOCALPHA; + TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, 1, &out ); + GTIFKeySet(gtif, GTModelTypeGeoKey, TYPE_SHORT, 1, ModelTypeProjected); GTIFKeySet(gtif, GTRasterTypeGeoKey, TYPE_SHORT, 1, RasterPixelIsArea); GTIFKeySet(gtif, GTCitationGeoKey, TYPE_ASCII, 0, "WGS 84 / Pseudo-Mercator");