+ GdkPixbuf *pixbuf = NULL;
+
+#ifdef HAVE_SQLITE3_H
+ if ( vml->mbtiles ) {
+ /*
+ gchar *statement = g_strdup_printf ( "SELECT name FROM sqlite_master WHERE type='table';" );
+ char *errMsg = NULL;
+ int ans = sqlite3_exec ( vml->mbtiles, statement, sql_select_tile_dump_cb, pixbuf, &errMsg );
+ if ( ans != SQLITE_OK ) {
+ // Only to console for information purposes only
+ g_warning ( "SQL problem: %d for %s - error: %s", ans, statement, errMsg );
+ sqlite3_free( errMsg );
+ }
+ g_free ( statement );
+ */
+
+ // Reading BLOBS is a bit more involved and so can't use the simpler sqlite3_exec ()
+ // Hence this specific function
+ pixbuf = get_pixbuf_sql_exec ( vml->mbtiles, xx, yy, zoom );
+ }
+#endif
+
+ return pixbuf;
+}
+
+static GdkPixbuf *get_pixbuf_from_metatile ( VikMapsLayer *vml, gint xx, gint yy, gint zz )
+{
+ const int tile_max = METATILE_MAX_SIZE;
+ char err_msg[PATH_MAX];
+ char *buf;
+ int len;
+ int compressed;
+
+ buf = malloc(tile_max);
+ if (!buf) {
+ return NULL;
+ }
+
+ err_msg[0] = 0;
+ len = metatile_read(vml->cache_dir, xx, yy, zz, buf, tile_max, &compressed, err_msg);
+
+ if (len > 0) {
+ if (compressed) {
+ // Not handled yet - I don't think this is used often - so implement later if necessary
+ g_warning ( "Compressed metatiles not implemented:%s\n", __FUNCTION__);
+ return NULL;
+ }
+
+ // Convert these buf bytes into a pixbuf via these streaming operations
+ GdkPixbuf *pixbuf = NULL;
+
+ GInputStream *stream = g_memory_input_stream_new_from_data ( buf, len, NULL );
+ GError *error = NULL;
+ pixbuf = gdk_pixbuf_new_from_stream ( stream, NULL, &error );
+ if (error || (!pixbuf)) {
+ g_warning ( "%s: %s", __FUNCTION__, error->message );
+ g_error_free ( error );
+ }
+ g_input_stream_close ( stream, NULL, NULL );
+
+ free(buf);
+ return pixbuf;
+ }
+ else {
+ g_warning ( "FAILED:%s %s", __FUNCTION__, err_msg);
+ return NULL;
+ }
+}
+
+
+static GdkPixbuf *pixbuf_apply_settings ( GdkPixbuf *pixbuf, VikMapsLayer *vml, MapCoord *mapcoord, gdouble xshrinkfactor, gdouble yshrinkfactor )
+{
+ // Apply alpha setting
+ if ( pixbuf && vml->alpha < 255 )
+ pixbuf = ui_pixbuf_set_alpha ( pixbuf, vml->alpha );
+
+ if ( pixbuf && ( xshrinkfactor != 1.0 || yshrinkfactor != 1.0 ) )
+ pixbuf = pixbuf_shrink ( pixbuf, xshrinkfactor, yshrinkfactor );
+
+ if ( pixbuf )
+ a_mapcache_add ( pixbuf, (mapcache_extra_t) {0.0}, mapcoord->x, mapcoord->y,
+ mapcoord->z, vik_map_source_get_uniq_id(MAPS_LAYER_NTH_TYPE(vml->maptype)),
+ mapcoord->scale, vml->alpha, xshrinkfactor, yshrinkfactor, vml->filename );
+
+ return pixbuf;
+}
+
+static void get_filename ( const gchar *cache_dir,
+ VikMapsCacheLayout cl,
+ guint16 id,
+ const gchar *name,
+ gint scale,
+ gint z,
+ gint x,
+ gint y,
+ gchar *filename_buf,
+ gint buf_len,
+ const gchar* file_extension )
+{
+ switch ( cl ) {
+ case VIK_MAPS_CACHE_LAYOUT_OSM:
+ if ( name ) {
+ if ( g_strcmp0 ( cache_dir, MAPS_CACHE_DIR ) )
+ // Cache dir not the default - assume it's been directed somewhere specific
+ g_snprintf ( filename_buf, buf_len, DIRECTDIRACCESS, cache_dir, (17 - scale), x, y, file_extension );
+ else
+ // Using default cache - so use the map name in the directory path
+ g_snprintf ( filename_buf, buf_len, DIRECTDIRACCESS_WITH_NAME, cache_dir, name, (17 - scale), x, y, file_extension );
+ }
+ else
+ g_snprintf ( filename_buf, buf_len, DIRECTDIRACCESS, cache_dir, (17 - scale), x, y, file_extension );
+ break;
+ default:
+ g_snprintf ( filename_buf, buf_len, DIRSTRUCTURE, cache_dir, id, scale, z, x, y );
+ break;
+ }