]> git.street.me.uk Git - andy/viking.git/blob - src/jpg.c
Fix mislabelled distance markers when using Nautical Miles.
[andy/viking.git] / src / jpg.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4  *
5  * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "jpg.h"
27 #ifdef HAVE_LIBEXIF
28 #include "geotag_exif.h"
29 #endif
30 #ifdef HAVE_MAGIC_H
31 #include <magic.h>
32 #endif
33
34 /**
35  * a_jpg_magic_check:
36  * @filename: The file
37  *
38  * Returns: Whether the file is a JPG.
39  *  Uses Magic library if available to determine the jpgness.
40  *  Otherwise uses a rudimentary extension name check.
41  */
42 gboolean a_jpg_magic_check ( const gchar *filename )
43 {
44         gboolean is_jpg = FALSE;
45 #ifdef HAVE_MAGIC_H
46         magic_t myt = magic_open ( MAGIC_CONTINUE|MAGIC_ERROR|MAGIC_MIME );
47         if ( myt ) {
48 #ifdef WINDOWS
49                 // We have to 'package' the magic database ourselves :(
50                 //  --> %PROGRAM FILES%\Viking\magic.mgc
51                 magic_load ( myt, "magic.mgc" );
52 #else
53                 // Use system default
54                 magic_load ( myt, NULL );
55 #endif
56                 const char* magic = magic_file (myt, filename);
57                 if ( g_strcmp0 (magic, "image/jpeg; charset=binary") == 0 )
58                         is_jpg = TRUE;
59
60                 magic_close ( myt );
61         }
62         else
63 #endif
64                 is_jpg = a_file_check_ext ( filename, ".jpg" );
65
66         return is_jpg;
67 }
68
69 /**
70  * Load a single JPG into a Trackwaypoint Layer as a waypoint
71  *
72  * @top:      The Aggregate layer that a new TRW layer may be created in
73  * @filename: The JPG filename
74  * @vvp:      The viewport
75  *
76  * Returns: Whether the loading was a success or not
77  *
78  * If the JPG has geotag information then the waypoint will be created with the appropriate position.
79  *  Otherwise the waypoint will be positioned at the current screen center.
80  * If a TRW layer is already selected the waypoint will be created in that layer.
81  */
82 gboolean a_jpg_load_file ( VikAggregateLayer *top, const gchar *filename, VikViewport *vvp )
83 {
84         gboolean auto_zoom = TRUE;
85         VikWindow *vw = (VikWindow *)(VIK_GTK_WINDOW_FROM_LAYER(VIK_LAYER(top)));
86         VikLayersPanel *vlp = vik_window_layers_panel ( vw );
87         // Auto load into TrackWaypoint layer if one is selected
88         VikLayer *vtl = vik_layers_panel_get_selected ( vlp );
89
90         gboolean create_layer = FALSE;
91         if ( vtl == NULL || vtl->type != VIK_LAYER_TRW ) {
92                 // Create layer if necessary
93                 vtl = vik_layer_create ( VIK_LAYER_TRW, vvp, FALSE );
94                 vik_layer_rename ( vtl, a_file_basename ( filename ) );
95                 create_layer = TRUE;
96         }
97
98         gchar *name = NULL;
99         VikWaypoint *wp = NULL;
100 #ifdef HAVE_LIBEXIF
101         wp = a_geotag_create_waypoint_from_file ( filename, vik_viewport_get_coord_mode (vvp), &name );
102 #endif
103         if ( wp ) {
104                 // Create name if geotag method didn't return one
105                 if ( !name )
106                         name = g_strdup ( a_file_basename ( filename ) );
107                 vik_trw_layer_filein_add_waypoint ( VIK_TRW_LAYER(vtl), name, wp );
108                 g_free ( name );
109         }
110         else {
111                 wp = vik_waypoint_new ();
112                 wp->visible = TRUE;
113                 vik_trw_layer_filein_add_waypoint ( VIK_TRW_LAYER(vtl), (gchar*) a_file_basename(filename), wp );
114                 vik_waypoint_set_image ( wp, filename );
115                 // Simply set position to the current center
116                 wp->coord = *( vik_viewport_get_center ( vvp ) );
117                 auto_zoom = FALSE;
118         }
119
120         // Complete the setup
121         vik_layer_post_read ( vtl, vvp, TRUE );
122         if ( create_layer )
123                 vik_aggregate_layer_add_layer ( top, vtl, FALSE );
124         if ( auto_zoom )
125                 vik_trw_layer_auto_set_view ( VIK_TRW_LAYER(vtl), vvp );
126
127         // ATM This routine can't fail
128         return TRUE;
129 }