0001 % This script uses dcraw to convert your camera's raw file format into Portable
0002 % GrayMaps (PGM) or Tagged Image File Format (TIFF) . The variable in_ftype
0003 % specifies the raw file format to be processed as well as the name of the
0004 % directories that contains this type of file.
0005 
0006 s = filesep;
0007 
0008 % ==== modify these variables =====
0009 %dir_data = 'data\A590\frames\tone_frames_paper';
0010 %dir_data = ['data' s 'A590' s 'frames' s 'bias_frames'];
0011 %dir_data = ['data' s 'A590' s 'frames' s 'dark_frames'];
0012 %dir_data = ['data' s 'A590' s 'frames' s 'dark_frames' s 'light_darks' s '2s'];
0013 %dir_data = ['data' s 'A590' s 'frames' s 'flat_frames'];
0014 dir_data = ['data' s 'A590' s 'frames' s 'light_frames' s '2s' s '2012_08_30'];
0015 %dir_data = ['data' s 'A590' s 'frames' s 'light_frames' s '2s' s '2012_09_13'];
0016 
0017 in_ftype = 'DNG';
0018 %in_ftype = 'CRW';
0019 
0020 out_ftype = 'PGM';
0021 %out_ftype = 'TIFF';
0022 % ==== end, modify these variables =====
0023 
0024 if strcmp(s, '/') % in Linux
0025     dcraw = ['/usr/bin/dcraw'];
0026 elseif strcmp(s, '\') % in Windows
0027     dcraw = ['bin' s 'dcraw'];
0028 end
0029 
0030 decode_args = '-c -D -4 -t 0 -j';
0031 finfo_args = '-v -i';
0032 
0033 if strcmpi(out_ftype, 'TIFF')
0034     decode_args = [decode_args ' -T'];
0035 end
0036 
0037 decode_cmd = [dcraw ' ' decode_args ' '];
0038 finfo_cmd = [dcraw ' ' finfo_args ' '];
0039 
0040 flne = {}; %initialize to empty for ~isempty(flne) test.
0041 
0042 g = waitbar(0, ['Decoding raw spectrographs. ' ...
0043                 'This may take several minutes.']);
0044 waitbar(0.001, g); % Octave's buggy waitbar doesn't like 0 or 1
0045 dl = list_dir(dir_data, in_ftype, 3);
0046 Ns = length(dl);
0047 
0048 for jj=1:Ns
0049     idir = dl{jj};
0050     [ign, pd] = pop_dirname(idir);
0051     odir = [pd s out_ftype];
0052 
0053     if (exist(odir) == 0)
0054         [status, msg, msgid] = mkdir(odir);
0055         if (status == 0)
0056             error(['batch_raw_decode: error creating ' odir ' . ' msg]);
0057         end
0058     elseif (exist(odir) ~= 7)
0059         error(['batch_raw_decode: ' odir ...
0060                 ' already exists, but is not a directory!']);
0061     end
0062 
0063     fl = list_dir(idir, ['*.' in_ftype], 1);
0064     for kk=1:length(fl)
0065         [ign, fname, ext] = fileparts(fl{kk});
0066         system([decode_cmd fl{kk} ' > ' odir s fname '.' out_ftype]);
0067         flne = fl;
0068     end
0069     waitbar((jj/Ns)-0.001, g);
0070 end
0071 
0072 disp('')
0073 
0074 if ~isempty(flne)
0075     % Use the last file processed to get the information dcraw can output
0076     % about the file.
0077     [ign, output] = system([finfo_cmd flne{1}]);
0078     si = strfind(output, 'Image size:');
0079     sf = strfind(output, 'Output size:');
0080     hwstr = output(si:(sf-1));
0081     %[ign, ign, ign, M, ign, ign] = regexp(hwstr, '[0-9]+');
0082     [ign, ign, ign, M] = regexp(hwstr, '[0-9]+');
0083     w = str2num(M{1});
0084     h = str2num(M{2});
0085 
0086     si = strfind(output, 'Filter pattern:');
0087     sf = strfind(output, 'Daylight multipliers:');
0088     bfpstr = output(si:(sf-1));
0089     [ign, idx] = regexp(bfpstr, ':\s\w');
0090     bfp = bfpstr(idx:idx+3);
0091     fprintf(['These images have width: %d, height: %d, ' ...
0092              'and Bayer filter pattern: %s.\n'], w, h, bfp);
0093 end
0094 
0095 clear all