You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: xASL_im_Lesion2Mask.m
    path of file: ./repos/ExploreASL/Functions/ImageProcessing
    
    the code of the file until where you have to start completion: function LesionIM = xASL_im_Lesion2Mask(LesionPath, x)
% xASL_im_Lesion2Mask Create multiple masks from single ROI/lesion mask, to
% be used as custom "atlas"
%
% FORMAT: LesionIM = xASL_im_Lesion2Mask(LesionPath, x)
% 
% INPUT:
%   LesionPath  - (string) path to the NIfTI containing the mask of
%                  ROI/lesion (REQUIRED)
%   x           - structure containing fields with all information required to run this function within ExploreASL (REQUIRED)
%
% OUTPUT:       - new image containing the masks
% -----------------------------------------------------------------------------------------------------------------------------------------------------
% DESCRIPTION: This function takes a mask and adds several ROIs, to be used as custom "atlas", e.g. when computing region-average CBF values.
% The mask % can be an ROI or lesion, if we assume it is a lesion, the following masks are created:
%
% 1. Intralesional
% 2. Perilesional (15 mm rim around the lesion)
% 3. Hemisphere (ipsilateral to lesion)
% 4. Contralateral version of 1
% 4a First create separate masks
% 4b Check if they are mutually exclusive
% 4c Save NIfTI file
% 5. Contralateral version of 2
% 6. Contralateral version of 3
%
% All these masks are masked by a brainmask `(pGM+pWM)>0.5`
% 

    
function LesionIM = xASL_im_Lesion2Mask(LesionPath, x)
% xASL_im_Lesion2Mask Create multiple masks from single ROI/lesion mask, to
% be used as custom "atlas"
%
% FORMAT: LesionIM = xASL_im_Lesion2Mask(LesionPath, x)
% 
% INPUT:
%   LesionPath  - (string) path to the NIfTI containing the mask of
%                  ROI/lesion (REQUIRED)
%   x           - structure containing fields with all information required to run this function within ExploreASL (REQUIRED)
%
% OUTPUT:       - new image containing the masks
% -----------------------------------------------------------------------------------------------------------------------------------------------------
% DESCRIPTION: This function takes a mask and adds several ROIs, to be used as custom "atlas", e.g. when computing region-average CBF values.
% The mask % can be an ROI or lesion, if we assume it is a lesion, the following masks are created:
%
% 1. Intralesional
% 2. Perilesional (15 mm rim around the lesion)
% 3. Hemisphere (ipsilateral to lesion)
% 4. Contralateral version of 1
% 4a First create separate masks
% 4b Check if they are mutually exclusive
% 4c Save NIfTI file
% 5. Contralateral version of 2
% 6. Contralateral version of 3
%
% All these masks are masked by a brainmask `(pGM+pWM)>0.5`
% 
% This function performs the following steps:
%
% 1. If lesion is empty, skip this & delete the file
% 2. BrainMasking
% 3. Create hemispheres
% 4. Save mutually exclusive masks
% 5. Create tsv-sidecar containing the names of the ROIs
% 6. Visual QC
%
% -----------------------------------------------------------------------------------------------------------------------------------------------------
% EXAMPLE: xASL_im_Lesion2Mask('/MyStudy/sub-001/Lesion_T1_1.nii.gz', x);
% __________________________________
% Copyright 2015-2021 ExploreASL


%% --------------------------------------------------------
%% Admin
if nargin<1 || isempty(LesionPath)
    error('Missing input argument LesionPath');
elseif ~xASL_exist(LesionPath,'file')
    fprintf('%s\n',['Skipped because mask didnt exist: ' LesionPath]);
	return;
end