1 /*
2 
3     GeoPackage extensions for SpatiaLite / SQLite
4 
5 Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 
7 The contents of this file are subject to the Mozilla Public License Version
8 1.1 (the "License"); you may not use this file except in compliance with
9 the License. You may obtain a copy of the License at
10 http://www.mozilla.org/MPL/
11 
12 Software distributed under the License is distributed on an "AS IS" basis,
13 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 for the specific language governing rights and limitations under the
15 License.
16 
17 The Original Code is GeoPackage Extensions
18 
19 The Initial Developer of the Original Code is Brad Hards (bradh@frogmouth.net)
20 
21 Portions created by the Initial Developer are Copyright (C) 2012-2015
22 the Initial Developer. All Rights Reserved.
23 
24 Contributor(s):
25 
26 Alternatively, the contents of this file may be used under the terms of
27 either the GNU General Public License Version 2 or later (the "GPL"), or
28 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 in which case the provisions of the GPL or the LGPL are applicable instead
30 of those above. If you wish to allow use of your version of this file only
31 under the terms of either the GPL or the LGPL, and not to allow others to
32 use your version of this file under the terms of the MPL, indicate your
33 decision by deleting the provisions above and replace them with the notice
34 and other provisions required by the GPL or the LGPL. If you do not delete
35 the provisions above, a recipient may use your version of this file under
36 the terms of any one of the MPL, the GPL or the LGPL.
37 
38 */
39 
40 #include "spatialite/geopackage.h"
41 #include <spatialite/gaiaexif.h>
42 #include "geopackage_internal.h"
43 
44 #if defined(_WIN32) && !defined(__MINGW32__)
45 #include "config-msvc.h"
46 #else
47 #include "config.h"
48 #endif
49 
50 #ifdef ENABLE_GEOPACKAGE
51 
52 GEOPACKAGE_PRIVATE void
fnct_gpkgGetImageType(sqlite3_context * context,int argc,sqlite3_value ** argv)53 fnct_gpkgGetImageType (sqlite3_context * context, int argc,
54 		       sqlite3_value ** argv)
55 {
56 /* SQL function:
57 / gpkgGetImageType(blob)
58 /
59 / Gets the image type (as a string) of the blob argument, or "unknown" if
60 / the image type is not one of the PNG, JPEG, TIFF or WebP format types that
61 / are supported in GeoPackage. This function raises exception on error (e.g.
62 / wrong argument type).
63 /
64 / The result will be one of:
65 / - "png" - for PNG
66 / - "jpeg" - for JPEG
67 / - "tiff" - for TIFF
68 / - "x-webp" - for WebP
69 / These are the mime type for the image format (without the "image/" prefix)
70 /
71 */
72     unsigned char *p_blob = NULL;
73     int n_bytes = 0;
74     int blobType;
75 
76     if (argc == 0)
77 	argc = 0;		/* suppressing stupid compiler warnings */
78 
79     if (sqlite3_value_type (argv[0]) != SQLITE_BLOB)
80       {
81 	  sqlite3_result_error (context,
82 				"gpkgGetImageType() error: argument 1 [image blob] is not of the BLOB type",
83 				-1);
84 	  return;
85       }
86     p_blob = (unsigned char *) sqlite3_value_blob (argv[0]);
87     n_bytes = sqlite3_value_bytes (argv[0]);
88 
89     blobType = gaiaGuessBlobType (p_blob, n_bytes);
90     switch (blobType)
91       {
92       case GAIA_TIFF_BLOB:
93 	  sqlite3_result_text (context, "tiff", strlen ("tiff"),
94 			       SQLITE_TRANSIENT);
95 	  break;
96       case GAIA_PNG_BLOB:
97 	  sqlite3_result_text (context, "png", strlen ("png"),
98 			       SQLITE_TRANSIENT);
99 	  break;
100       case GAIA_JPEG_BLOB:
101 	  sqlite3_result_text (context, "jpeg", strlen ("jpeg"),
102 			       SQLITE_TRANSIENT);
103 	  break;
104       case GAIA_WEBP_BLOB:
105 	  sqlite3_result_text (context, "x-webp", strlen ("x-webp"),
106 			       SQLITE_TRANSIENT);
107 	  break;
108       default:
109 	  sqlite3_result_text (context, "unknown", strlen ("unknown"),
110 			       SQLITE_TRANSIENT);
111 	  break;
112       }
113 }
114 #endif
115