1 | %'get_file_type': determine info about a file (image, multimage, civdata,...) . |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | % [FileType,FileInfo,Object]=get_file_type(fileinput) |
---|
4 | % |
---|
5 | % OUTPUT: |
---|
6 | % FileType: type of file |
---|
7 | % FileInfo: structure containing info on the file (case of images or video), in particular |
---|
8 | % .Height: image height in pixels |
---|
9 | % .Width: image width in pixels |
---|
10 | % .BitDepth: nbre of bits per pixel (8 of 16) |
---|
11 | % .ColorType: 'greyscale' or 'color' |
---|
12 | % .NumberOfFrames |
---|
13 | % .FrameRate: nbre of frames per second, =[] for images |
---|
14 | % Object: in case of video |
---|
15 | % |
---|
16 | % INPUT: |
---|
17 | % fileinput: name, including path, of the file to analyse |
---|
18 | function [FileType,FileInfo,VideoObject]=get_file_type(fileinput) |
---|
19 | |
---|
20 | FileInfo=[]; |
---|
21 | VideoObject=[]; |
---|
22 | if exist(fileinput,'file') |
---|
23 | FileType='txt';%default, text file |
---|
24 | else |
---|
25 | FileType=''; |
---|
26 | return |
---|
27 | end |
---|
28 | [tild,tild,FileExt]=fileparts(fileinput); |
---|
29 | |
---|
30 | switch FileExt |
---|
31 | case '.fig' |
---|
32 | FileType='figure'; |
---|
33 | case '.xml' |
---|
34 | FileType='xml'; |
---|
35 | case '.xls' |
---|
36 | FileType='xls'; |
---|
37 | otherwise |
---|
38 | if ~isempty(FileExt)% exclude empty extension |
---|
39 | FileExt=regexprep(FileExt,'^.','');% eliminate the dot of the extension |
---|
40 | if ~isempty(FileExt) |
---|
41 | if ~isempty(imformats(FileExt))%case of images |
---|
42 | try |
---|
43 | imainfo=imfinfo(fileinput); |
---|
44 | if length(imainfo) >1 %case of image with multiple frames |
---|
45 | FileType='multimage'; |
---|
46 | FileInfo=imainfo(1);%take info from the first frame |
---|
47 | FileInfo.NumberOfFrames=length(imainfo); |
---|
48 | else |
---|
49 | FileType='image'; |
---|
50 | FileInfo=imainfo; |
---|
51 | FileInfo.NumberOfFrames=1; |
---|
52 | end |
---|
53 | end |
---|
54 | else |
---|
55 | error_nc=0; |
---|
56 | try |
---|
57 | [Data,tild,tild,errormsg]=nc2struct(fileinput,'ListGlobalAttribute','absolut_time_T0','Conventions',... |
---|
58 | 'CivStage','patch2','fix2','civ2','patch','fix','hart'); |
---|
59 | if ~isempty(errormsg) |
---|
60 | error_nc=1; |
---|
61 | else |
---|
62 | if ~isempty(Data.absolut_time_T0') && ~isempty(Data.hart) |
---|
63 | FileType='civx'; % test for civx velocity fields |
---|
64 | if isequal(Data.patch2,1) |
---|
65 | FileInfo.CivStage=6; |
---|
66 | elseif isequal(Data.fix2,1) |
---|
67 | FileInfo.CivStage=5; |
---|
68 | elseif isequal(Data.civ2,1) |
---|
69 | FileInfo.CivStage=4; |
---|
70 | elseif isequal(Data.patch,1) |
---|
71 | FileInfo.CivStage=3; |
---|
72 | elseif isequal(Data.fix,1) |
---|
73 | FileInfo.CivStage=2; |
---|
74 | else |
---|
75 | FileInfo.CivStage=1; |
---|
76 | end |
---|
77 | elseif strcmp(Data.Conventions,'uvmat/civdata') |
---|
78 | FileType='civdata'; % test for civx velocity fields |
---|
79 | FileInfo.CivStage=Data.CivStage; |
---|
80 | else |
---|
81 | FileType='netcdf'; |
---|
82 | end |
---|
83 | end |
---|
84 | catch ME |
---|
85 | error_nc=1; |
---|
86 | end |
---|
87 | if error_nc |
---|
88 | try |
---|
89 | if exist('VideoReader.m','file')%recent version of Matlab |
---|
90 | VideoObject=VideoReader(fileinput); |
---|
91 | FileInfo=get(VideoObject); |
---|
92 | FileType='video'; |
---|
93 | elseif exist('mmreader.m','file')% Matlab 2009a |
---|
94 | VideoObject=mmreader(fileinput); |
---|
95 | FileInfo=get(VideoObject); |
---|
96 | FileType='mmreader'; |
---|
97 | end |
---|
98 | FileInfo.BitDepth=FileInfo.BitsPerPixel/3; |
---|
99 | end |
---|
100 | end |
---|
101 | end |
---|
102 | end |
---|
103 | end |
---|
104 | end |
---|