iCodec

iCodec is a software that is used to compress image of any extension to a specific format. iCodec could also decompress the file to restore back the file to the original image file.

I.  Compression Explanation

The objective ofimage compressionis to reduce irrelevance and redundancy of the image data in order to be able to store ortransmitdata in an efficient form. Image compression may belossyorlossless. Lossless compression is preferred for archival purposes and often for medical imaging, technical drawings,clip art, or comics. This is because lossy compression methods, especially when used at lowbit rates, introducecompression artifacts.In this project we would try to promote lossless compression.

II.  Global Algorithm

Run Length Encoding

Run-length encoding(RLE) is a very simple form ofdata compressionin whichrunsof data (that is, sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run. This is most useful on data that contains many such runs: for example, simple graphic images such as icons, line drawings, and animations. It is not useful with files that don't have many runs as it could greatly increase the file size.

Huffman

Huffman codingis anentropy encodingalgorithmused forlossless data compression. The term refers to the use of avariable-length codetable for encoding a source symbol (such as a character in a file) where the variable-length code table has been derived in a particular way based on the estimated probability of occurrence for each possible value of the source symbol. It was developed byDavid A. Huffmanwhile he was aPh.D.student atMIT, and published in the 1952 paper "A Method for the Construction of Minimum-Redundancy Codes".

Huffman coding uses a specific method for choosing the representation for each symbol, resulting in aprefix code(sometimes called "prefix-free codes", that is, the bit string representing some particular symbol is never a prefix of the bit string representing any other symbol) that expresses the most common source symbols using shorter strings of bits than are used for less common source symbols. Huffman was able to design the most efficient compression methodof this type: no other mapping of individual source symbols to unique strings of bits will produce a smaller average output size when the actual symbol frequencies agree with those used to create the code. The running time of Huffman's method is fairly efficient, it takesoperations to construct it.

III.  Source Code and Explanation

RLE

Compressor

%Reading image

img = imread('D:\RLE\ted1.png');

%Get size of the matrices

[a,b,c] = size(img);

%For Writing file binary

fid = fopen('test.bin' , 'w');

%Read width (Mod and Div approach)

width = a;

counter = 0;

while width ~= 0

if width > 255

width = width - 255;

counter = counter + 1;

else

width = width - width;

counter = counter + 1;

end

end

fwrite(fid,counter,'uint8');

%Write width to binary

width = a;

full = 255;

while width ~= 0

if width > 255

fwrite(fid,full,'uint8');

width = width - 255;

else

fwrite(fid,width,'uint8');

width = 0;

end

end

%Read Height (Mod and Div)

counter = 0;

height = b;

while height ~= 0

if height > 255

height = height - 255;

counter = counter + 1;

else

height = 0;

counter = counter + 1;

end

end

fwrite(fid,counter,'uint8');

%Write the height into binary

height= b;

full = 255;

while height ~= 0

if height > 255

fwrite(fid,full,'uint8');

height = height - 255;

else

fwrite(fid,height,'uint8');

height = 0;

end

end

%Set the pointer to initial coordinate of matrices

curr =img(1,1);

freq = 0;

%Writing the data of pixels into binary file

for k = 1:c

for i=1:a

for j=1:b

if curr ~= img(i,j,k)

fwrite(fid,freq,'uint8');

fwrite(fid,curr,'uint8');

curr = img(i,j,k);

freq = 1;

else

freq = freq +1;

if freq > 255

fwrite(fid,freq,'uint8');

fwrite(fid,curr,'uint8');

freq =1;

end

end

end

end

end

fwrite(fid,freq,'uint8');

fwrite(fid,curr,'uint8');

%close the operation

fclose(fid);

%fwrite(fid,c,'uint8');

Decompressor

%Open the binary file

fid = fopen('test.bin' , 'r');

data = fread(fid, 'uint8');

%Seek the pixel or color that comes in an order and write them into one

curr = 2;

temp = data(1);

width = 0;

for a = 1:temp

width = width + data(curr);

curr = curr + 1;

end

fseek(fid,curr+1,'bof');

temp = data(curr);

height = 0;

curr = curr + 1;

for b = 1:temp

height = height + data(curr);

curr = curr + 1;

end

fseek(fid,curr,'bof');

current = curr;

freq = data(current);

for k=1:3

for i=1:width,

for j=1:height,

if freq ~= 0

z(i,j,k) = uint8(data(current+1));

freq = freq - 1;

else

current = current + 2;

freq = data(current);

z(i,j,k) = uint8(data(current+1));

freq = freq - 1;

end

end

end

end

%Write the output

imwrite(z,'output.png');

%Close the operation

fclose(fid);

Huffman Coding

%Reading image

a=imread('samsung.jpg');

%converting an image to grayscale

I=rgb2gray(a);

%size of the image

[m,n]=size(I);

Totalcount=m*n;

%variables using to find the probability

cnt=1;

sigma=0;

%computing the cumulative probability.

for i=0:255

k=I==i;

count(cnt)=sum(k(:));

%pro array is having the probabilities

pro(cnt)=count(cnt)/Totalcount;

sigma=sigma+pro(cnt);

cumpro(cnt)=sigma;

cnt=cnt+1;

end;

%Symbols for an image

symbols = [0:255];

%Huffman code Dictionary

dict = huffmandict(symbols,pro);

%function which converts array to vector

vec_size = 1;

for p = 1:m

for q = 1:n

newvec(vec_size) = I(p,q);

vec_size = vec_size+1;

end

end

%Huffman Encodig

hcode = huffmanenco(newvec,dict);

%Huffman Decoding

dhsig1 = huffmandeco(hcode,dict);

%convertign dhsig1 double to dhsig uint8

dhsig = uint8(dhsig1);

%vector to array conversion

dec_row=sqrt(length(dhsig));

dec_col=dec_row;

%variables using to convert vector 2 array

arr_row = 1;

arr_col = 1;

vec_si = 1;

for x = 1:m

for y = 1:n

back(x,y)=dhsig(vec_si);

arr_col = arr_col+1;

vec_si = vec_si + 1;

end

arr_row = arr_row+1;

end

%converting image from grayscale to rgb

[deco, map] = gray2ind(back,256);

RGB = ind2rgb(deco,map);

imwrite(RGB,'decoded.JPG');

figure,imshow('decoded.JPG');

GUI and Bonus Features

Using GUI we create the GUIDE. GUIDE would be like Visual Studio or NetBeans. The GUIDE would cover the button, textfield, axes, and handle. We would input the button command on callBack (…) method. To view the code, right click and select “Editor”

Some Useful Functions:

•  Concatenate Strings (for outputting .bin and png files)

Combining two strings

Strcat(s1,s2,….sn-1)

•  File Open Dialog (Open image browser)

Open file, or obtain information about open files

fileID = fopen(filename, permission)

Writing text into TextField

•  Use handles

•  set (handles.TEXTFIELDNAME,'String', ANYTHING YOU WANT TO WRITE);

Calculate Operation Time

•  CPUtime

We use the function to calculate the progress time

t = cputime;

%progress

e = cputime –t

Global Variables

Assign Global variables by using Global variable_name;

Add this variable declaration at every function you want to use the variable. This is not encouraged, though because the whole complexion of the variable could be changed easily and could affect the whole program.

IV.  Experimental Result

RLE

Using RLE we got the result that if we try to compress PNG files, we get a much larger binary file, this is because the PNG file is already very small and very compressed. It has very solid components, so if we are trying to compress the pixels into binary file, it just would become larger.

Note that some PNG files become smaller after compression, this is because the Matlab pixel consideration is very precise and therefore ‘corrects’ the previous or original image pixels composition that is not very well composed (This would happen especially if we use MS. Paint to generate the image)

In the case that we try to decompress the large file such as BMP, the compressor does its job very well. We could reduce a 1MB file into an 1.9 KB binary file, then we could decompress it back to its shape and size without any problem.

Huffman Coding

Huffman coding works like a charm without any problem with any file size and it also has better compression rate that RLE. This is possible because Huffman use shorter code for more frequent characters.