Skip to content

Commit f652ab1

Browse files
hummeltechtimautin
andauthored
Configurable ImageIO output format (#318)
* Added the possibility to provide ImageIO's output format in the TYPE string. For instance: TYPE=png image/png TYPE=png image/png png256 TYPE=png image/png png24 TYPE=png image/png png * Removed useless comments. Added documentation for output format. * Added tests for 'webp' output format * Also added tests for 'png32' output format * Fix 'webp' sums vary based on version * Add JPG & use multiple layers on example-map --------- Co-authored-by: Tim <autintim@gmail.com>
1 parent 2287afe commit f652ab1

8 files changed

Lines changed: 155 additions & 47 deletions

File tree

etc/renderd/renderd.conf.examples

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ XML=/var/www/example-map/mapnik.xml
4646
;** config options used by mod_tile, but not renderd **
4747
;MINZOOM=0
4848
;MAXZOOM=18
49-
;TYPE=png image/png
49+
;TYPE=png image/png png256 ; Values are: <extension> <mime-type> <output-format> (for more information about output format see https://github.com/mapnik/mapnik/wiki/Image-IO)
5050
;DESCRIPTION=This is a description of the tile layer used in the tile json request
5151
;ATTRIBUTION=&copy;<a href=\"http://www.openstreetmap.org/\">OpenStreetMap</a> and <a href=\"http://wiki.openstreetmap.org/wiki/Contributors\">contributors</a>, <a href=\"http://opendatacommons.org/licenses/odbl/\">ODbL</a>
5252
;SERVER_ALIAS=http://localhost/
@@ -65,7 +65,7 @@ XML=/var/www/example-map/mapnik.xml
6565
;** config options used by mod_tile, but not renderd **
6666
;MINZOOM=0
6767
;MAXZOOM=22
68-
;TYPE=png image/png
68+
;TYPE=png image/png png256 ; Values are: <extension> <mime-type> <output-format> (for more information about output format see https://github.com/mapnik/mapnik/wiki/Image-IO)
6969
;DESCRIPTION=This is a description of the tile layer used in the tile json request
7070
;ATTRIBUTION=&copy;<a href=\"http://www.openstreetmap.org/\">OpenStreetMap</a> and <a href=\"http://wiki.openstreetmap.org/wiki/Contributors\">contributors</a>, <a href=\"http://opendatacommons.org/licenses/odbl/\">ODbL</a>
7171
;SERVER_ALIAS=http://localhost/

includes/daemon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ typedef struct {
5353
char host[PATH_MAX];
5454
char htcpip[PATH_MAX];
5555
char tile_dir[PATH_MAX];
56+
char output_format[INILINE_MAX];
5657
char parameterization[PATH_MAX];
5758
int tile_px_size;
5859
double scale_factor;

src/daemon.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,17 @@ int main(int argc, char **argv)
10241024

10251025
strcpy(maps[iconf].parameterization, ini_parameterize);
10261026

1027+
snprintf(buffer, sizeof(buffer), "%s:type", name);
1028+
const char *ini_type = iniparser_getstring(ini, buffer, "png image/png png256");
1029+
1030+
const char ini_fileExtension[INILINE_MAX] = "png";
1031+
const char ini_mimeType[INILINE_MAX] = "image/png";
1032+
const char ini_outputFormat[INILINE_MAX] = "png256";
1033+
1034+
sscanf(ini_type, "%[^ ] %[^ ] %[^;#]", ini_fileExtension, ini_mimeType, ini_outputFormat);
1035+
1036+
strcpy(maps[iconf].output_format, ini_outputFormat);
1037+
10271038
/* Pass this information into the rendering threads,
10281039
* as it is needed to configure mapniks number of connections
10291040
*/
@@ -1079,9 +1090,9 @@ int main(int argc, char **argv)
10791090

10801091
for (iconf = 0; iconf < XMLCONFIGS_MAX; ++iconf) {
10811092
if (maps[iconf].xmlname[0] != 0) {
1082-
g_logger(G_LOG_LEVEL_INFO, "config map %d: name(%s) file(%s) uri(%s) htcp(%s) host(%s)",
1093+
g_logger(G_LOG_LEVEL_INFO, "config map %d: name(%s) file(%s) uri(%s) output_format(%s) htcp(%s) host(%s)",
10831094
iconf, maps[iconf].xmlname, maps[iconf].xmlfile, maps[iconf].xmluri,
1084-
maps[iconf].htcpip, maps[iconf].host);
1095+
maps[iconf].output_format, maps[iconf].htcpip, maps[iconf].host);
10851096
}
10861097
}
10871098

src/gen_tile.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ struct projectionconfig {
104104
struct xmlmapconfig {
105105
char xmlname[XMLCONFIG_MAX];
106106
char xmlfile[PATH_MAX];
107+
char output_format[XMLCONFIG_MAX];
107108
struct storage_backend * store;
108109
Map map;
109110
struct projectionconfig * prj;
@@ -324,14 +325,14 @@ static enum protoCmd render(struct xmlmapconfig * map, int x, int y, int z, char
324325
#else
325326
mapnik::image_view<mapnik::image_data_32> vw(xx * map->tilesize, yy * map->tilesize, map->tilesize, map->tilesize, buf.data());
326327
#endif
327-
tiles.set(xx, yy, save_to_string(vw, "png256"));
328+
tiles.set(xx, yy, save_to_string(vw, map->output_format));
328329
}
329330
}
330331

331332
return cmdDone; // OK
332333
}
333334
#else //METATILE
334-
static enum protoCmd render(Map &m, const char *tile_dir, char *xmlname, projection &prj, int x, int y, int z)
335+
static enum protoCmd render(Map &m, const char *tile_dir, char *xmlname, projection &prj, int x, int y, int z, char* outputFormat)
335336
{
336337
char filename[PATH_MAX];
337338
char tmp[PATH_MAX];
@@ -365,7 +366,7 @@ static enum protoCmd render(Map &m, const char *tile_dir, char *xmlname, project
365366

366367
mapnik::image_view<mapnik::image_data_32> vw(128, 128, 256, 256, buf.data());
367368
g_logger(G_LOG_LEVEL_DEBUG, "Render %i %i %i %s", z, x, y, filename)
368-
mapnik::save_to_file(vw, tmp, "png256");
369+
mapnik::save_to_file(vw, tmp, outputFormat);
369370

370371
if (rename(tmp, filename)) {
371372
perror(tmp);
@@ -403,6 +404,7 @@ void *render_thread(void * arg)
403404

404405
strcpy(maps[iMaxConfigs].xmlname, parentxmlconfig[iMaxConfigs].xmlname);
405406
strcpy(maps[iMaxConfigs].xmlfile, parentxmlconfig[iMaxConfigs].xmlfile);
407+
strcpy(maps[iMaxConfigs].output_format, parentxmlconfig[iMaxConfigs].output_format);
406408
maps[iMaxConfigs].store = init_storage_backend(parentxmlconfig[iMaxConfigs].tile_dir);
407409
maps[iMaxConfigs].tilesize = parentxmlconfig[iMaxConfigs].tile_px_size;
408410
maps[iMaxConfigs].scale = parentxmlconfig[iMaxConfigs].scale_factor;
@@ -521,7 +523,7 @@ void *render_thread(void * arg)
521523
}
522524

523525
#else //METATILE
524-
ret = render(maps[i].map, maps[i].tile_dir, req->xmlname, maps[i].prj, req->x, req->y, req->z);
526+
ret = render(maps[i].map, maps[i].tile_dir, req->xmlname, maps[i].prj, req->x, req->y, req->z, maps[i].output_format);
525527
#ifdef HTCP_EXPIRE_CACHE
526528
cache_expire(maps[i].htcpsock, maps[i].host, maps[i].xmluri, req->x, req->y, req->z);
527529
#endif

src/mod_tile.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,7 @@ static const char *load_tile_config(cmd_parms *cmd, void *mconfig, const char *c
21032103
const char * result;
21042104
char fileExtension[INILINE_MAX];
21052105
char mimeType[INILINE_MAX];
2106+
char outputFormat[INILINE_MAX];
21062107
char * description = NULL;
21072108
char * attribution = NULL;
21082109
char * cors = NULL;
@@ -2214,6 +2215,7 @@ static const char *load_tile_config(cmd_parms *cmd, void *mconfig, const char *c
22142215
strcpy(url, "");
22152216
strcpy(fileExtension, "png");
22162217
strcpy(mimeType, "image/png");
2218+
strcpy(outputFormat, "png256");
22172219
description = NULL;
22182220
cors = NULL;
22192221
attribution = NULL;
@@ -2293,7 +2295,7 @@ static const char *load_tile_config(cmd_parms *cmd, void *mconfig, const char *c
22932295
return "TYPE too long";
22942296
}
22952297

2296-
if (sscanf(value, "%[^ ] %[^;#]", fileExtension, mimeType) != 2) {
2298+
if (sscanf(value, "%[^ ] %[^ ] %[^;#]", fileExtension, mimeType, outputFormat) < 2) {
22972299
if (description) {
22982300
free(description);
22992301
description = NULL;

tests/CMakeLists.txt

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,24 @@ execute_process(COMMAND ${ID_EXECUTABLE} -gn nobody
3939
#-----------------------------------------------------------------------------
4040

4141
set(MAP_NAME "default")
42-
set(TILE_SHA256SUM "dbf26531286e844a3a9735cdd193598dca78d22f77cafe5824bcaf17f88cbb08")
43-
set(TILE_URL "http://localhost:8081/tiles/renderd-example/9/297/191.png")
44-
set(TILE_CMD "${CURL_EXECUTABLE} --fail --silent ${TILE_URL}")
42+
43+
set(TILE_DEFAULT_URL "http://localhost:8081/tiles/renderd-example/9/297/191.png")
44+
set(TILE_JPG_URL "http://localhost:8081/tiles/renderd-example-jpg/9/297/191.jpg")
45+
set(TILE_PNG256_URL "http://localhost:8081/tiles/renderd-example-png256/9/297/191.png")
46+
set(TILE_PNG32_URL "http://localhost:8081/tiles/renderd-example-png32/9/297/191.png")
47+
set(TILE_WEBP_URL "http://localhost:8081/tiles/renderd-example-webp/9/297/191.webp")
48+
49+
set(TILE_DEFAULT_CMD "${CURL_EXECUTABLE} --fail --silent ${TILE_DEFAULT_URL}")
50+
set(TILE_DEFAULT_SHA256SUM "dbf26531286e844a3a9735cdd193598dca78d22f77cafe5824bcaf17f88cbb08")
51+
set(TILE_JPG_CMD "${CURL_EXECUTABLE} --fail --silent ${TILE_JPG_URL}")
52+
set(TILE_JPG_SHA256SUM "e09c3406c02f03583dadf0c8404c2d3efdc06a40d399e381ed2f47f49fde42d7")
53+
set(TILE_PNG256_CMD "${CURL_EXECUTABLE} --fail --silent ${TILE_PNG256_URL}")
54+
set(TILE_PNG256_SHA256SUM "${TILE_DEFAULT_SHA256SUM}")
55+
set(TILE_PNG32_CMD "${CURL_EXECUTABLE} --fail --silent ${TILE_PNG32_URL}")
56+
set(TILE_PNG32_SHA256SUM "1006d92152f1e18896e0016fb43201b14bbcf7655955b74495ad3610541d325b")
57+
set(TILE_WEBP_CMD "${CURL_EXECUTABLE} --fail --silent ${TILE_WEBP_URL}")
58+
set(TILE_WEBP_SHA256SUM_6 "96fc0455b2269a7bcd4a5b3c9844529c3c77e3bb15f56e72f78a5af3bc15b6b5") # libwebp6
59+
set(TILE_WEBP_SHA256SUM_7 "a82ef9ba5dc333de88af7b645084c30ab2b01c664e17162cbf6659c287cc4df4") # libwebp7
4560

4661
configure_file(
4762
renderd.conf.in
@@ -128,27 +143,47 @@ add_test(
128143
"
129144
)
130145
add_test(
131-
NAME download_tile
146+
NAME download_tiles
132147
COMMAND ${BASH} -c "
133-
until $(${TILE_CMD} --output tile.png); do
148+
until $(${TILE_DEFAULT_CMD} --output tile.png); do
149+
echo 'Sleeping 1s';
150+
sleep 1;
151+
done
152+
until $(${TILE_JPG_CMD} --output tile.jpg); do
153+
echo 'Sleeping 1s';
154+
sleep 1;
155+
done
156+
until $(${TILE_PNG256_CMD} --output tile.png256); do
157+
echo 'Sleeping 1s';
158+
sleep 1;
159+
done
160+
until $(${TILE_PNG32_CMD} --output tile.png32); do
161+
echo 'Sleeping 1s';
162+
sleep 1;
163+
done
164+
until $(${TILE_WEBP_CMD} --output tile.webp); do
134165
echo 'Sleeping 1s';
135166
sleep 1;
136167
done
137168
"
138169
)
139170
add_test(
140-
NAME check_tile
171+
NAME check_tiles
141172
COMMAND ${BASH} -c "
142-
echo '${TILE_SHA256SUM} tile.png' | ${SHA256SUM_EXECUTABLE} -c
173+
(echo '${TILE_DEFAULT_SHA256SUM} tile.png' | ${SHA256SUM_EXECUTABLE} -c) && \
174+
(echo '${TILE_JPG_SHA256SUM} tile.jpg' | ${SHA256SUM_EXECUTABLE} -c) && \
175+
(echo '${TILE_PNG256_SHA256SUM} tile.png256' | ${SHA256SUM_EXECUTABLE} -c) && \
176+
(echo '${TILE_PNG32_SHA256SUM} tile.png32' | ${SHA256SUM_EXECUTABLE} -c) && \
177+
((echo '${TILE_WEBP_SHA256SUM_6} tile.webp' | ${SHA256SUM_EXECUTABLE} -c) || (echo '${TILE_WEBP_SHA256SUM_7} tile.webp' | ${SHA256SUM_EXECUTABLE} -c))
143178
"
144179
)
145180
add_test(
146181
NAME dirty_tile
147182
COMMAND ${BASH} -c "
148-
TILE_STATUS_CMD=\"${TILE_CMD}/status | cut -d. -f2\"
183+
TILE_STATUS_CMD=\"${TILE_DEFAULT_CMD}/status | cut -d. -f2\"
149184
TILE_STATUS_OUTPUT_OLD=$(\${TILE_STATUS_CMD})
150185
sleep 5;
151-
TILE_DIRTY_OUTPUT=$(${TILE_CMD}/dirty)
186+
TILE_DIRTY_OUTPUT=$(${TILE_DEFAULT_CMD}/dirty)
152187
if [ \"\${TILE_DIRTY_OUTPUT}\" != \"Tile submitted for rendering\" ]; then
153188
exit 1;
154189
fi
@@ -159,8 +194,8 @@ add_test(
159194
"
160195
)
161196
add_test(
162-
NAME remove_tile
163-
COMMAND ${RM} -v tile.png
197+
NAME remove_tiles
198+
COMMAND ${RM} -v tile.png tile.jpg tile.png256 tile.png32 tile.webp
164199
)
165200
add_test(
166201
NAME stop_renderd
@@ -227,24 +262,24 @@ set_tests_properties(render_old PROPERTIES
227262
FIXTURES_REQUIRED httpd_started
228263
TIMEOUT 20
229264
)
230-
set_tests_properties(download_tile PROPERTIES
265+
set_tests_properties(download_tiles PROPERTIES
231266
FIXTURES_REQUIRED httpd_started
232-
FIXTURES_SETUP tile_downloaded
233-
TIMEOUT 10
267+
FIXTURES_SETUP tiles_downloaded
268+
TIMEOUT 20
234269
)
235-
set_tests_properties(check_tile PROPERTIES
236-
DEPENDS download_tile
237-
FIXTURES_REQUIRED "httpd_started;tile_downloaded"
238-
REQUIRED_FILES tile.png
270+
set_tests_properties(check_tiles PROPERTIES
271+
DEPENDS download_tiles
272+
FIXTURES_REQUIRED "httpd_started;tiles_downloaded"
273+
REQUIRED_FILES "tile.png;tile.jpg;tile.png256;tile.png32;tile.webp"
239274
)
240275
set_tests_properties(dirty_tile PROPERTIES
241-
DEPENDS download_tile
242-
FIXTURES_REQUIRED "httpd_started;tile_downloaded"
243-
REQUIRED_FILES tile.png
276+
DEPENDS download_tiles
277+
FIXTURES_REQUIRED "httpd_started;tiles_downloaded"
278+
REQUIRED_FILES "tile.png;tile.jpg;tile.png256;tile.png32;tile.webp"
244279
TIMEOUT 20
245280
)
246-
set_tests_properties(remove_tile PROPERTIES
247-
DEPENDS download_tile
248-
FIXTURES_CLEANUP tile_downloaded
249-
REQUIRED_FILES tile.png
281+
set_tests_properties(remove_tiles PROPERTIES
282+
DEPENDS download_tiles
283+
FIXTURES_CLEANUP tiles_downloaded
284+
REQUIRED_FILES "tile.png;tile.jpg;tile.png256;tile.png32;tile.webp"
250285
)

tests/renderd.conf.in

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ TILEDIR=@PROJECT_BINARY_DIR@/tests/tiles
88
URI=/tiles/renderd-example
99
XML=@PROJECT_SOURCE_DIR@/utils/example-map/mapnik.xml
1010

11+
[@MAP_NAME@_jpg]
12+
TILEDIR=@PROJECT_BINARY_DIR@/tests/tiles
13+
TYPE=jpg image/jpeg jpeg
14+
URI=/tiles/renderd-example-jpg
15+
XML=@PROJECT_BINARY_DIR@/tests/www/mapnik.xml
16+
17+
[@MAP_NAME@_png256]
18+
TILEDIR=@PROJECT_BINARY_DIR@/tests/tiles
19+
TYPE=png image/png png256
20+
URI=/tiles/renderd-example-png256
21+
XML=@PROJECT_BINARY_DIR@/tests/www/mapnik.xml
22+
23+
[@MAP_NAME@_png32]
24+
TILEDIR=@PROJECT_BINARY_DIR@/tests/tiles
25+
TYPE=png image/png png32
26+
URI=/tiles/renderd-example-png32
27+
XML=@PROJECT_BINARY_DIR@/tests/www/mapnik.xml
28+
29+
[@MAP_NAME@_webp]
30+
TILEDIR=@PROJECT_BINARY_DIR@/tests/tiles
31+
TYPE=webp image/webp webp
32+
URI=/tiles/renderd-example-webp
33+
XML=@PROJECT_BINARY_DIR@/tests/www/mapnik.xml
34+
1135
[renderd1]
1236
iphostname=127.0.0.1
1337
ipport=8881

utils/example-map/index.html

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,57 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
3+
<head>
44
<title>renderd example map</title>
55
<meta charset="utf-8" />
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
88
<link rel="stylesheet" href="leaflet/leaflet.css" />
99
<script src="leaflet/leaflet.min.js"></script>
10-
</head>
10+
</head>
1111

12-
<body style="margin: 0;">
13-
<div id="map" style="position: absolute; width: 100%; height: 100%;"></div>
12+
<body style="margin: 0">
13+
<div id="map" style="position: absolute; width: 100%; height: 100%"></div>
1414

1515
<script>
16-
var map = L.map('map').setView([0, 0], 4);
17-
L.tileLayer('http://localhost:8081/tiles/renderd-example/{z}/{x}/{y}.png', {
18-
maxZoom: 12,
19-
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors.',
20-
}).addTo(map);
21-
</script>
16+
var attribution =
17+
'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors.';
18+
var options = {
19+
maxZoom: 12,
20+
attribution: attribution,
21+
};
22+
23+
var png = L.tileLayer(
24+
"http://localhost:8081/tiles/renderd-example/{z}/{x}/{y}.png",
25+
options
26+
);
27+
28+
var jpg = L.tileLayer(
29+
"http://localhost:8081/tiles/renderd-example-jpg/{z}/{x}/{y}.jpg",
30+
options
31+
);
32+
var png32 = L.tileLayer(
33+
"http://localhost:8081/tiles/renderd-example-png32/{z}/{x}/{y}.png",
34+
options
35+
);
36+
var webp = L.tileLayer(
37+
"http://localhost:8081/tiles/renderd-example-webp/{z}/{x}/{y}.webp",
38+
options
39+
);
2240

23-
</body>
41+
var map = L.map("map", {
42+
center: [0, 0],
43+
zoom: 4,
44+
layers: [png],
45+
});
46+
47+
var baseMaps = {
48+
JPG: jpg,
49+
PNG: png,
50+
PNG32: png32,
51+
WEBP: webp,
52+
};
53+
54+
var layerControl = L.control.layers(baseMaps).addTo(map);
55+
</script>
56+
</body>
2457
</html>

0 commit comments

Comments
 (0)