]> git.street.me.uk Git - andy/viking.git/blobdiff - src/dem.c
Add Refresh to consider reloading a Mapnik Rendering configuration.
[andy/viking.git] / src / dem.c
index 9c86a0529b6d476c503ab40498523fa5e2a41a7b..461c2bdf78fce2681438bfe6852a4279382b2907 100644 (file)
--- a/src/dem.c
+++ b/src/dem.c
@@ -2,6 +2,7 @@
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2008, Evan Battaglia <gtoevan@gmx.net>
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2008, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -34,7 +35,6 @@
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
-#include <zlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #ifdef HAVE_UNISTD_H
 #include <sys/types.h>
 #include <sys/stat.h>
 #ifdef HAVE_UNISTD_H
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
 
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
 
+#include "compression.h"
 #include "dem.h"
 #include "dem.h"
-#include "file.h"
+#include "coords.h"
+#include "fileutils.h"
+
+/* Compatibility */
+#if ! GLIB_CHECK_VERSION(2,22,0)
+#define g_mapped_file_unref g_mapped_file_free
+#endif
 
 #define DEM_BLOCK_SIZE 1024
 #define GET_COLUMN(dem,n) ((VikDEMColumn *)g_ptr_array_index( (dem)->columns, (n) ))
 
 #define DEM_BLOCK_SIZE 1024
 #define GET_COLUMN(dem,n) ((VikDEMColumn *)g_ptr_array_index( (dem)->columns, (n) ))
@@ -245,78 +252,6 @@ static void dem_parse_block ( gchar *buffer, VikDEM *dem, gint *cur_column, gint
   }
 }
 
   }
 }
 
-/* return size of unzip data or 0 if failed */
-/* can be made generic to uncompress zip, gzip, bzip2 data */
-static guint uncompress_data(void *uncompressed_buffer, guint uncompressed_size, void *compressed_data, guint compressed_size)
-{
-       z_stream stream;
-       int err;
-
-       stream.next_in = compressed_data;
-       stream.avail_in = compressed_size;
-       stream.next_out = uncompressed_buffer;
-       stream.avail_out = uncompressed_size;
-       stream.zalloc = (alloc_func)0;
-       stream.zfree = (free_func)0;
-       stream.opaque = (voidpf)0;
-
-       /* negative windowBits to inflateInit2 means "no header" */
-       if ((err = inflateInit2(&stream, -MAX_WBITS)) != Z_OK) {
-               g_warning("%s(): inflateInit2 failed", __PRETTY_FUNCTION__);
-               return 0;
-       }
-
-       err = inflate(&stream, Z_FINISH);
-       if ((err != Z_OK) && (err != Z_STREAM_END) && stream.msg) {
-               g_warning("%s() inflate failed err=%d \"%s\"", __PRETTY_FUNCTION__, err, stream.msg == NULL ? "unknown" : stream.msg);
-               inflateEnd(&stream);
-               return 0;
-       }
-
-       inflateEnd(&stream);
-       return(stream.total_out);
-}
-
-static void *unzip_hgt_file(gchar *zip_file, gulong *unzip_size)
-{
-       void *unzip_data = NULL;
-       gchar *zip_data;
-       struct _lfh {
-               guint32 sig;
-               guint16 extract_version;
-               guint16 flags;
-               guint16 comp_method;
-               guint16 time;
-               guint16 date;
-               guint32 crc_32;
-               guint32 compressed_size;
-               guint32 uncompressed_size;
-               guint16 filename_len;
-               guint16 extra_field_len;
-       }  __attribute__ ((__packed__)) *local_file_header = NULL;
-
-
-       local_file_header = (struct _lfh *) zip_file;
-       if (local_file_header->sig != 0x04034b50) {
-               g_warning("%s(): wrong format", __PRETTY_FUNCTION__);
-               g_free(unzip_data);
-               goto end;
-       }
-
-       zip_data = zip_file + sizeof(struct _lfh) + local_file_header->filename_len + local_file_header->extra_field_len;
-       unzip_data = g_malloc(local_file_header->uncompressed_size);
-       gulong uncompressed_size = local_file_header->uncompressed_size;
-
-       if (!(*unzip_size = uncompress_data(unzip_data, uncompressed_size, zip_data, local_file_header->compressed_size))) {
-               g_free(unzip_data);
-               unzip_data = NULL;
-               goto end;
-       }
-
-end:
-       return(unzip_data);
-}
-
 static VikDEM *vik_dem_read_srtm_hgt(const gchar *file_name, const gchar *basename, gboolean zip)
 {
   gint i, j;
 static VikDEM *vik_dem_read_srtm_hgt(const gchar *file_name, const gchar *basename, gboolean zip)
 {
   gint i, j;
@@ -351,7 +286,7 @@ static VikDEM *vik_dem_read_srtm_hgt(const gchar *file_name, const gchar *basena
   dem->n_columns = 0;
 
   if ((mf = g_mapped_file_new(file_name, FALSE, &error)) == NULL) {
   dem->n_columns = 0;
 
   if ((mf = g_mapped_file_new(file_name, FALSE, &error)) == NULL) {
-    g_error(_("Couldn't map file %s: %s"), file_name, error->message);
+    g_critical(_("Couldn't map file %s: %s"), file_name, error->message);
     g_error_free(error);
     g_free(dem);
     return NULL;
     g_error_free(error);
     g_free(dem);
     return NULL;
@@ -363,8 +298,9 @@ static VikDEM *vik_dem_read_srtm_hgt(const gchar *file_name, const gchar *basena
     void *unzip_mem = NULL;
     gulong ucsize;
 
     void *unzip_mem = NULL;
     gulong ucsize;
 
-    if ((unzip_mem = unzip_hgt_file(dem_file, &ucsize)) == NULL) {
-      g_mapped_file_free(mf);
+    if ((unzip_mem = unzip_file(dem_file, &ucsize)) == NULL) {
+      g_mapped_file_unref(mf);
+      g_ptr_array_foreach ( dem->columns, (GFunc)g_free, NULL );
       g_ptr_array_free(dem->columns, TRUE);
       g_free(dem);
       return NULL;
       g_ptr_array_free(dem->columns, TRUE);
       g_free(dem);
       return NULL;
@@ -373,6 +309,8 @@ static VikDEM *vik_dem_read_srtm_hgt(const gchar *file_name, const gchar *basena
     dem_mem = unzip_mem;
     file_size = ucsize;
   }
     dem_mem = unzip_mem;
     file_size = ucsize;
   }
+  else
+    dem_mem = (gint16 *)dem_file;
 
   if (file_size == (num_rows_3sec * num_rows_3sec * sizeof(gint16)))
     arcsec = 3;
 
   if (file_size == (num_rows_3sec * num_rows_3sec * sizeof(gint16)))
     arcsec = 3;
@@ -380,7 +318,7 @@ static VikDEM *vik_dem_read_srtm_hgt(const gchar *file_name, const gchar *basena
     arcsec = 1;
   else {
     g_warning("%s(): file %s does not have right size", __PRETTY_FUNCTION__, basename);
     arcsec = 1;
   else {
     g_warning("%s(): file %s does not have right size", __PRETTY_FUNCTION__, basename);
-    g_mapped_file_free(mf);
+    g_mapped_file_unref(mf);
     g_free(dem);
     return NULL;
   }
     g_free(dem);
     return NULL;
   }
@@ -408,7 +346,7 @@ static VikDEM *vik_dem_read_srtm_hgt(const gchar *file_name, const gchar *basena
 
   if (zip)
     g_free(dem_mem);
 
   if (zip)
     g_free(dem_mem);
-  g_mapped_file_free(mf);
+  g_mapped_file_unref(mf);
   return dem;
 }
 
   return dem;
 }
 
@@ -448,6 +386,7 @@ VikDEM *vik_dem_new_from_file(const gchar *file)
   buffer[fread(buffer, 1, DEM_BLOCK_SIZE, f)] = '\0';
   if ( ! dem_parse_header ( buffer, rv ) ) {
     g_free ( rv );
   buffer[fread(buffer, 1, DEM_BLOCK_SIZE, f)] = '\0';
   if ( ! dem_parse_header ( buffer, rv ) ) {
     g_free ( rv );
+    fclose(f);
     return NULL;
   }
   /* TODO: actually use header -- i.e. GET # OF COLUMNS EXPECTED */
     return NULL;
   }
   /* TODO: actually use header -- i.e. GET # OF COLUMNS EXPECTED */
@@ -497,6 +436,7 @@ void vik_dem_free ( VikDEM *dem )
   guint i;
   for ( i = 0; i < dem->n_columns; i++)
     g_free ( GET_COLUMN(dem, i)->points );
   guint i;
   for ( i = 0; i < dem->n_columns; i++)
     g_free ( GET_COLUMN(dem, i)->points );
+  g_ptr_array_foreach ( dem->columns, (GFunc)g_free, NULL );
   g_ptr_array_free ( dem->columns, TRUE );
   g_free ( dem );
 }
   g_ptr_array_free ( dem->columns, TRUE );
   g_free ( dem );
 }