function data = loadbf(fullname)
% ====
% function to load data from bio-format (bf) supported file types
% refer to loci.wisc.edu for more information on bio-formats
% 
% usage: data = loadbf(fullname)
% fullname is full file name including path
% data contains accessory information about the image/movie file
% 
% Xiaolin Nan, OHSU, 09/01/2012
% version 0: only takes series 1
% in the next version, will modify the img_data global array into
% a structure array to contain meta data for each frame
% to prepare for multicolor imaging data
%
% Xiaolin Nan, OHSU, 09/03/2012
% combined the original bfopen.m with loadbf.m; only retrieving image data
% in series 1 and no meta data nor colormap information
%

global imgdata h_mainfig;

% suppress the warning on multiple bindings etc.
warning off; 

	% read the data file with bfopen_wfi function
	[path name ext] = fileparts(fullname);
	msg = sprintf('Parsing file %s. Please wait ...', [name ext]);
	showmsg(h_mainfig, 'message', msg);	pause(0.1);
	
	%%%%%%%%
	% code below comes from bfopen.m
	autoloadBioFormats = 1;

	% Toggle the stitchFiles flag to control grouping of similarly
	% named files into a single dataset based on file numbering.
	stitchFiles = 0;

	% load the Bio-Formats library into the MATLAB environment
	status = bfCheckJavaPath(autoloadBioFormats);
	assert(status, ['Missing Bio-Formats library. Either add loci_tools.jar '...
    	'to the static Java path or add it to the Matlab path.']);

	% initialize logging (will complain if not)
	loci.common.DebugTools.enableLogging('INFO');

	% Get the channel filler
	r = bfGetReader(fullname, stitchFiles);

	%numSeries = r.getSeriesCount();
	%result = cell(numSeries, 2);

	%for s = 1:numSeries
	    %fprintf('Reading series #%d', s);
	r.setSeries(0);
	%pixelType = r.getPixelType();
	%bpp = loci.formats.FormatTools.getBytesPerPixel(pixelType);
	%bppMax = power(2, bpp * 8);
	
	%% end of code from bfopen.m
	
	frames = r.getImageCount();
	%imageList = cell(numImages, 2);
	%colorMaps = cell(numImages);
	
	% get frame 1 for the width and height information
	temp = bfGetPlane(r, 1);
	%image1 = temp{1, 1};
	[width height] = size(temp);
	
	% create the imgdata array
	imgdata = zeros(frames, width, height, 'uint16');
	
	if(isempty(imgdata) == 1)
		disp('Cannot create matrix to store image. Possible reason: file too large.');
		data = [];
		return;
	end
	
	% reading in the frames
    for i= 1 : frames
        temp = bfGetPlane(r, i);
        imgdata (i, :, :) =  temp; %{1, 1};
        
        if mod(i, 100) == 0 || i == frames
            msg = sprintf('Reading file %s. Please wait ... frame %d of %d', [name ext], i, frames);
            showmsg(gcf, 'message', msg);
            pause(0.001);
        end
    end
	
	% fill the data structure for compatibility issues
	data.badframes = 0;
	% assign data fields
	data.width = width;
	data.height = height;
	data.frames = frames;
	%data.intensity = intensity;
	data.rbad = 0;
	
	r.close();
return
