function file = dicom_open_msg(file, mode)
%DICOM_OPEN_MSG  Open the next DICOM message in the pool for processing.
%   FILE = DICOM_OPEN_MSG(FILE, 'r') opens the next DICOM message in the
%   pool for reading.
%
%   FILE = DICOM_OPEN_MSG(FILE, 'w') opens the next DICOM message in the
%   pool for writing.
%
%   Note: The pool of messages is generated by DICOM_GET_MSG.

%   Copyright 1993-2010 The MathWorks, Inc.

% Open the message.
switch (lower(mode))
case 'r'
    
    file.FID = fopen(file.Filename, 'r', 'ieee-le');
    
    if (file.FID < 0)
        
        error(message('images:dicom_open_msg:fileOpenRead', file.Filename{ file.Current_Message }));
        
    end

case 'w'

    file.FID = fopen(file.Filename, 'w', 'ieee-le');

    if (file.FID < 0)
        
        error(message('images:dicom_open_msg:fileOpenWrite', file.Filename));
        
    end
    
otherwise
    
    error(message('images:dicom_open_msg:openMode'))
    
end

% Set local files to default transfer syntax (for fragments).
% DICOM_SET_MMETA_ENCODING will change this if appropriate.
file.Current_Endian = 'ieee-le';
file.Pixel_Endian = 'ieee-le';
file.Current_VR = 'Implicit';
