%% File: corrupt.m
infile='Nikolaus_Kopernikus.png';
outfile='Nikolaus_Kopernikus_bsc.png';
%% Corrupt a file by applying BSC
p=.05;
A=imread(infile);
%% Print information about image pixels
disp(['Pixels of class ', class(A)]);
sz=size(A);
disp(['Image size (height-by-width-by-number-of-color-planes): ', num2str(sz)]);
%% Convert to Galois array in GF(2)
message=gf(de2bi(A,[],2));
%% Model symmetric binary channel by adding a random GF(2) vector
disp(['Target error rate is ', num2str(p)]);
err=gf(rand(size(message))<=p);
%% Add error to the message, to simulate received message
received=message+err;
%% Find actual error rate
difference=received-message;
difference=difference ~= 0;
rate=sum(difference(:))/length(message);
disp(['Actual error rate is ', num2str(rate)]);
%% Write image
data=reshape(uint8(bi2de(received.x)),sz);
imwrite(data,outfile,'png');