⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cba1deco.m

📁 针对二值图像的压缩实现了算术编码以及解码 并在此基础上实现了一节自适应算术编码 有很好的压缩效果
💻 M
字号:
function dseq = CBA1deco(code, len)

[row_cd, col_cd] = size(code);
if (row_cd > 1),
    code = code.';
end
counts =[1 1];
cum_counts = [0, 1,2];
total_count = 2;
context0 =[1 1];%00 01 de pinshu
cum_ct0 = [0,cumsum(context0)];
total_ct0 = cum_ct0(end);
context1 =[1 1];%10 11 de pinshu
cum_ct1 = [0,cumsum(context1)];
total_ct1 = cum_ct1(end);
N = 16;

% Initialize the lower and upper bounds.
dec_low = 0;
dec_up = 2^N-1;

% Read the first N number of bits into a temporary tag bin_tag
bin_tag = code(1:N);
dec_tag = bi2de(bin_tag, 'left-msb');

% Initialize DSEQ
dseq = zeros(1,len);
dseq_index = 1;

k=N;
ptr = 0;

% This loop runs untill all the symbols are decoded into DSEQ
while (dseq_index <= len)
    
    if dseq_index ==1
        
    % Compute dec_tag_new
    dec_tag_new =floor( ((dec_tag-dec_low+1)*total_count-1)/(dec_up-dec_low+1) );
    ptr = pick(cum_counts, dec_tag_new);
    dec_low_new = dec_low + floor( (dec_up-dec_low+1)*cum_counts(ptr-1+1)/total_count );
    dec_up = dec_low + floor( (dec_up-dec_low+1)*cum_counts(ptr+1)/total_count )-1;  
    % Decode a symbol based on dec_tag_new
 
    end 
    if dseq_index >1
        pre =dseq(dseq_index-1);
        if pre==1
            dec_tag_new =floor( ((dec_tag-dec_low+1)*total_ct0-1)/(dec_up-dec_low+1) );
            ptr = pick(cum_ct0, dec_tag_new);

            dec_low_new = dec_low + floor( (dec_up-dec_low+1)*cum_ct0(ptr-1+1)/total_ct0 );
            dec_up = dec_low + floor( (dec_up-dec_low+1)*cum_ct0(ptr+1)/total_ct0 )-1;
            
             context0(ptr) = context0(ptr)+1;
             cum_ct0 = [0, cumsum(context0)];
             total_ct0 = cum_ct0(end);
        end
        if pre==2
            dec_tag_new =floor( ((dec_tag-dec_low+1)*total_ct1-1)/(dec_up-dec_low+1) );
            ptr = pick(cum_ct1, dec_tag_new);
            dec_low_new = dec_low + floor( (dec_up-dec_low+1)*cum_ct1(ptr-1+1)/total_ct1 );
            dec_up = dec_low + floor( (dec_up-dec_low+1)*cum_ct1(ptr+1)/total_ct1 )-1;
            
             context1(ptr) = context1(ptr)+1;
             cum_ct1 = [0, cumsum(context1)];
             total_ct1 = cum_ct1(end);            
        end       
    end
    % Update DSEQ by adding the decoded symbol
    dseq(dseq_index) = ptr;
    dseq_index = dseq_index + 1;
    
%     % Compute the new lower bound
%     dec_low_new = dec_low + floor( (dec_up-dec_low+1)*cum_counts(ptr-1+1)/total_count );
%     
%     % Compute the new upper bound
%     dec_up = dec_low + floor( (dec_up-dec_low+1)*cum_counts(ptr+1)/total_count )-1;
    
    % Update the lower bound
    dec_low = dec_low_new;
    
    % Check for E1, E2 or E3 conditions and keep looping as long as they occur.
     while ( isequal(bitget(dec_low, N), bitget(dec_up, N)) | ...
        ( isequal(bitget(dec_low, N-1), 1) & isequal(bitget(dec_up, N-1), 0) ) ),
        
        % Break out if we have finished working with all the bits in CODE
        if ( k==length(code) ), break, end;
        k = k + 1;

        % If it is an E1 or E2 condition, do
        if isequal(bitget(dec_low, N), bitget(dec_up, N)),

            % Left shifts and update
            dec_low = bitshift(dec_low, 1) + 0;
            dec_up  = bitshift(dec_up,  1) + 1;

            % Left shift and read in code
            dec_tag = bitshift(dec_tag, 1) + code(k);

            % Reduce to N for next loop
            dec_low = bitset(dec_low, N+1, 0);
            dec_up  = bitset(dec_up,  N+1, 0);
            dec_tag = bitset(dec_tag, N+1, 0);
        
        % Else if it is an E3 condition        
        elseif ( isequal(bitget(dec_low, N-1), 1) & ...
            isequal(bitget(dec_up, N-1), 0) ),

            % Left shifts and update
            dec_low = bitshift(dec_low, 1) + 0;
            dec_up  = bitshift(dec_up,  1) + 1;

            % Left shift and read in code
            dec_tag = bitshift(dec_tag, 1) + code(k);
            
            % Reduce to N for next loop
            dec_low = bitset(dec_low, N+1, 0);
            dec_up  = bitset(dec_up,  N+1, 0);
            dec_tag = bitset(dec_tag, N+1, 0);
            
            % Complement the new MSB of dec_low, dec_up and dec_tag
            dec_low = bitxor(dec_low, 2^(N-1) );
            dec_up  = bitxor(dec_up,  2^(N-1) );
            dec_tag = bitxor(dec_tag, 2^(N-1) );

        end
    end % end while
%     counts(ptr) = counts(ptr)+1;    
%     cum_counts = [0, cumsum(counts)];
%     total_count = cum_counts(end);
end % end while length(dseq)

% Set the same output orientation as code
if (row_cd > 1)
    dseq = dseq.';
end
%-------------------------------------------------------------
function [ptr] = pick(cum_counts, value);
% This internal function is used to find where value is positioned

% Check for this case and quickly exit
if value == cum_counts(end)
    ptr = length(cum_counts)-1;
    return
end

c = find(cum_counts <= value);
ptr = c(end);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -