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: jpegls_modified.m
    path of file: ./repos/microshift_compression/verilog code/matlab_files/Toolkits
    
    the code of the file until where you have to start completion: function jpegls
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% License for more details.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This encoder codes 8 bit per pixel single tone images
% it currently is coded to encoded the example in section H.3 from ITU T.87
% and attach the jpeg header for said example. It outputs code to a text
% file for easy readability.
% This code is to better understand the algorithm and to eventually have
% a full encoder/decoder for use in matlab.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% example image values from ITU standard, H.3
%image = [0,0,90,74,68,50,43,205,64,145,145,145,100,145,145,145];
bitnum=3;
I = imread('C:\Users\bzhangai\OneDrive\Bit depth compression\further compression\Lenna\I_quant9.bmp');
%I = imread('C:\Users\bzhangai\OneDrive\Bit depth compression\further compression\Lichtenstein\I_quant9.bmp');
[x,y] = size(I);
I = double(I')./(256/2^bitnum);
image = I(:)';
%load ImageData;
%image = I_vector;
width = x;
height = y;
maxval = 7;
bit_depth = 3;

%% initialize JPEG-LS step 1
near = 0; %center region; default, lossless
range = maxval + 1; %percision; default, lossless,  = 256
qbpp = bit_depth;   %bit depth, from image, = 8
bpp = max(2,ceil(log2(maxval+1))); %bit depth, use d for limit calc, = 8
limit = 2*(bpp + max(8,bpp)); %limit of code length, = 32
%quantization regions; default 8-qbpp
T1 = 2; T2 = 12; T3 = 21;  %default: 3, 7, 31
%sign variable for adaptive correction
sign = 1; %1 = pos, 0 = neg
error_value = 0; %computed error value residual
%when to reset adaptive parameter counter
reset = 64; %default, lossless
%adaptive correction parameters
C = zeros(1,365); % (cumulative) prediction correction values
B = zeros(1,365); % bias
N = ones(1,367);
Nn = zeros(1,367); %for run mode, only need last 2 indexes
A_init = max(2,floor((range+2^5)/2^6));
A = ones(1,367) * A_init; %empty array A
%closed range for variable C forall indexes
min_C = -128;
max_C = 127;
bitstream_index=0;
encoded_bitstream= repmat(char(0),1,length(image)*3);
%run mode variables
run_count = 0; %how long the run is
run_index = 1; %index of run vector J (start at 1 for matlab
run_index_fixed = 1; %refer to this index when coding run intfor. variable
run_value = 0; %comparison variable to check equality of x during run
run_interupt_type = 0; %coding identifier for run length
J = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3, ...

    
function jpegls
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% License for more details.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This encoder codes 8 bit per pixel single tone images
% it currently is coded to encoded the example in section H.3 from ITU T.87
% and attach the jpeg header for said example. It outputs code to a text
% file for easy readability.
% This code is to better understand the algorithm and to eventually have
% a full encoder/decoder for use in matlab.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% example image values from ITU standard, H.3
%image = [0,0,90,74,68,50,43,205,64,145,145,145,100,145,145,145];
bitnum=3;
I = imread('C:\Users\bzhangai\OneDrive\Bit depth compression\further compression\Lenna\I_quant9.bmp');
%I = imread('C:\Users\bzhangai\OneDrive\Bit depth compression\further compression\Lichtenstein\I_quant9.bmp');
[x,y] = size(I);
I = double(I')./(256/2^bitnum);
image = I(:)';
%load ImageData;
%image = I_vector;
width = x;
height = y;
maxval = 7;
bit_depth = 3;

%% initialize JPEG-LS step 1
near = 0; %center region; default, lossless
range = maxval + 1; %percision; default, lossless,  = 256
qbpp = bit_depth;   %bit depth, from image, = 8
bpp = max(2,ceil(log2(maxval+1))); %bit depth, use d for limit calc, = 8
limit = 2*(bpp + max(8,bpp)); %limit of code length, = 32
%quantization regions; default 8-qbpp
T1 = 2; T2 = 12; T3 = 21;  %default: 3, 7, 31
%sign variable for adaptive correction
sign = 1; %1 = pos, 0 = neg
error_value = 0; %computed error value residual
%when to reset adaptive parameter counter
reset = 64; %default, lossless
%adaptive correction parameters
C = zeros(1,365); % (cumulative) prediction correction values
B = zeros(1,365); % bias
N = ones(1,367);
Nn = zeros(1,367); %for run mode, only need last 2 indexes
A_init = max(2,floor((range+2^5)/2^6));
A = ones(1,367) * A_init; %empty array A
%closed range for variable C forall indexes
min_C = -128;
max_C = 127;
bitstream_index=0;
encoded_bitstream= repmat(char(0),1,length(image)*3);
%run mode variables
run_count = 0; %how long the run is
run_index = 1; %index of run vector J (start at 1 for matlab
run_index_fixed = 1; %refer to this index when coding run intfor. variable
run_value = 0; %comparison variable to check equality of x during run
run_interupt_type = 0; %coding identifier for run length
J = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3, ...
    4,4,5,5,6,6,7,7,8,9,10,11,12,13,14,15];
%context map variables
num_gradients = 729; %based on quantization regions; defaunt 8qbpp
map_size = 365; %size of context map; default
total_quantized_gradients = cell(1,num_gradients); %empty cell of gradients
context_map = cell(1,map_size);
init_q1 = -4; init_q2 = -4; init_q3 = -4;
j = 2;
for i = 1:num_gradients %fill all defined quantized values
    total_quantized_gradients{i} = [init_q1,init_q2,init_q3];   %729 contexts
    if(init_q3 < 4)
        init_q3 = init_q3 + 1;
    else
        init_q3 = -4;
        init_q2 = init_q2 + 1;
    end