1 // ==========================================================
2 // GetType / Validate
3 //
4 // Design and implementation by
5 // - Floris van den Berg (flvdberg@wxs.nl)
6 // - Herv� Drolon (drolon@infonie.fr)
7 //
8 // This file is part of FreeImage 3
9 //
10 // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
11 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
12 // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
13 // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
14 // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
15 // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
16 // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
17 // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
18 // THIS DISCLAIMER.
19 //
20 // Use at your own risk!
21 // ==========================================================
22 
23 #ifdef _MSC_VER
24 #pragma warning (disable : 4786) // identifier was truncated to 'number' characters
25 #endif
26 
27 #include "FreeImage.h"
28 #include "Utilities.h"
29 #include "FreeImageIO.h"
30 #include "Plugin.h"
31 
32 // =====================================================================
33 // Generic stream file type access
34 // =====================================================================
35 
36 FREE_IMAGE_FORMAT DLL_CALLCONV
FreeImage_GetFileTypeFromHandle(FreeImageIO * io,fi_handle handle,int size)37 FreeImage_GetFileTypeFromHandle(FreeImageIO *io, fi_handle handle, int size) {
38 	if (handle != NULL) {
39 		int fif_count = FreeImage_GetFIFCount();
40 
41 		for (int i = 0; i < fif_count; ++i) {
42 			FREE_IMAGE_FORMAT fif = (FREE_IMAGE_FORMAT)i;
43 			if (FreeImage_ValidateFIF(fif, io, handle)) {
44 				if(fif == FIF_TIFF) {
45 					// many camera raw files use a TIFF signature ...
46 					// ... try to revalidate against FIF_RAW (even if it breaks the code genericity)
47 					if (FreeImage_ValidateFIF(FIF_RAW, io, handle)) {
48 						return FIF_RAW;
49 					}
50 				}
51 				return fif;
52 			}
53 		}
54 	}
55 
56 	return FIF_UNKNOWN;
57 }
58 
59 // =====================================================================
60 // File stream file type access
61 // =====================================================================
62 
63 FREE_IMAGE_FORMAT DLL_CALLCONV
FreeImage_GetFileType(const char * filename,int size)64 FreeImage_GetFileType(const char *filename, int size) {
65 	FreeImageIO io;
66 	SetDefaultIO(&io);
67 
68 	FILE *handle = fopen(filename, "rb");
69 
70 	if (handle != NULL) {
71 		FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromHandle(&io, (fi_handle)handle, size);
72 
73 		fclose(handle);
74 
75 		return format;
76 	}
77 
78 	return FIF_UNKNOWN;
79 }
80 
81 FREE_IMAGE_FORMAT DLL_CALLCONV
FreeImage_GetFileTypeU(const wchar_t * filename,int size)82 FreeImage_GetFileTypeU(const wchar_t *filename, int size) {
83 #ifdef _WIN32
84 	FreeImageIO io;
85 	SetDefaultIO(&io);
86 	FILE *handle = _wfopen(filename, L"rb");
87 
88 	if (handle != NULL) {
89 		FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromHandle(&io, (fi_handle)handle, size);
90 
91 		fclose(handle);
92 
93 		return format;
94 	}
95 #endif
96 	return FIF_UNKNOWN;
97 }
98 
99 // =====================================================================
100 // Memory stream file type access
101 // =====================================================================
102 
103 FREE_IMAGE_FORMAT DLL_CALLCONV
FreeImage_GetFileTypeFromMemory(FIMEMORY * stream,int size)104 FreeImage_GetFileTypeFromMemory(FIMEMORY *stream, int size) {
105 	FreeImageIO io;
106 	SetMemoryIO(&io);
107 
108 	if (stream != NULL) {
109 		return FreeImage_GetFileTypeFromHandle(&io, (fi_handle)stream, size);
110 	}
111 
112 	return FIF_UNKNOWN;
113 }
114 
115 // --------------------------------------------------------------------------
116 
117 BOOL DLL_CALLCONV
FreeImage_ValidateFromHandle(FREE_IMAGE_FORMAT fif,FreeImageIO * io,fi_handle handle)118 FreeImage_ValidateFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle) {
119 	return FreeImage_ValidateFIF(fif, io, handle);
120 }
121 
122 BOOL DLL_CALLCONV
FreeImage_Validate(FREE_IMAGE_FORMAT fif,const char * filename)123 FreeImage_Validate(FREE_IMAGE_FORMAT fif, const char *filename) {
124 	FreeImageIO io;
125 	SetDefaultIO(&io);
126 
127 	FILE *handle = fopen(filename, "rb");
128 
129 	if (handle != NULL) {
130 		BOOL bIsValidFIF = FreeImage_ValidateFromHandle(fif, &io, (fi_handle)handle);
131 		fclose(handle);
132 		return bIsValidFIF;
133 	}
134 
135 	return FALSE;
136 }
137 
138 BOOL DLL_CALLCONV
FreeImage_ValidateU(FREE_IMAGE_FORMAT fif,const wchar_t * filename)139 FreeImage_ValidateU(FREE_IMAGE_FORMAT fif, const wchar_t *filename) {
140 #ifdef _WIN32
141 	FreeImageIO io;
142 	SetDefaultIO(&io);
143 	FILE *handle = _wfopen(filename, L"rb");
144 
145 	if (handle != NULL) {
146 		BOOL bIsValidFIF = FreeImage_ValidateFromHandle(fif, &io, (fi_handle)handle);
147 		fclose(handle);
148 		return bIsValidFIF;
149 	}
150 #endif
151 	return FALSE;
152 }
153 
154 BOOL DLL_CALLCONV
FreeImage_ValidateFromMemory(FREE_IMAGE_FORMAT fif,FIMEMORY * stream)155 FreeImage_ValidateFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream) {
156 	FreeImageIO io;
157 	SetMemoryIO(&io);
158 
159 	if (stream != NULL) {
160 		BOOL bIsValidFIF = FreeImage_ValidateFromHandle(fif, &io, (fi_handle)stream);
161 		return bIsValidFIF;
162 	}
163 
164 	return FALSE;
165 }
166