%
% 2017-921;    2016-5-03;
% Script file: h0.m modified from io6.m (check fread for raw image format)
% 1. pgm   2. bmp   3.  tif   4.  raw   5.  tif   6.  jpg
%----------------------------------------------------------------------
% 1. PGM
m=112;  n=92;
X=imread('1.pgm'); Z=round(double(X)); A=reshape(Z,m,n);
subplot(4,3,1)
imshow(Z/255.0)
title('112 \times 92 1.pgm')
H=zeros(1,256);
for i=1:m
    for j=1:n k=A(i,j); H(k+1)=H(k+1)+1; end
end
H1=H;
L=0:255;
subplot(4,3,4)
plot(L,H1,'b-');
% 2. BMP
m=128; n=128;
X=imread('891539.bmp');  % Z=round(double(X));  A=reshape(Z,m,n);
subplot(4,3,2)
imshow(X)
title('128 \times 128 891539.bmp')
H=zeros(1,256);
for i=1:m
    for j=1:n k=X(i,j); H(k+1)=H(k+1)+1; end
end
H2=H;
subplot(4,3,5)
plot(L,H2,'b-');
% 3. Raw
m=300; n=300;
subplot(4,3,3)
fin=fopen('fR01.raw','r');
X=fread(fin,m*n,'uint8=>uint8'); fclose(fin);  Y=reshape(X,m,n); Z=Y';
imshow(Z)
title('300 \times 300 fR01.raw')
H=zeros(1,256);
for i=1:m
    for j=1:n k=Z(i,j); H(k+1)=H(k+1)+1; end
end
H3=H;
L3=0:249;
subplot(4,3,6)
plot(L3,H3(1:250),'b-');
% 4. Tiff
m=512; n=512;
X=imread('lenna.tif');  % Z=round(double(X));  A=reshape(Z,m,n);
subplot(4,3,7)
imshow(X)
title('512 \times 512 lenna.tif')
H=zeros(1,256);
for i=1:m
    for j=1:n k=X(i,j); H(k+1)=H(k+1)+1; end
end
H4=H;
subplot(4,3,10)
plot(L,H4,'b-');
% 5. JPEG
m=512; n=512;
X=imread('koala512.jpg'); 
subplot(4,3,8)
imshow(X)
title('512 \times 512 koala512.jpg')
H=zeros(1,256);
for i=1:m
    for j=1:n  k=X(i,j,1); H(k+1)=H(k+1)+1; end
end
Hr=H;
H=zeros(1,256);
for i=1:m
    for j=1:n  k=X(i,j,2); H(k+1)=H(k+1)+1; end
end
Hb=H;
H=zeros(1,256);
for i=1:m
    for j=1:n  k=X(i,j,3); H(k+1)=H(k+1)+1; end
end
Hg=H;
subplot(4,3,11)
plot(L,Hr,'r',L,Hb,'b',L,Hg,'g')
xlabel('R-B-G histogram plots')
% 6. Tiff
m=450; n=150;
X=imread('array.tif','TIFF');  % Z=round(double(X)); A=reshape(Z,m,n);
subplot(4,3,9)
imshow(X)
title('450 \times 150 Color array.tif') 
H=zeros(1,256);
for i=1:m
    for j=1:n  k=X(i,j,1); H(k+1)=H(k+1)+1; end
end
Hr=H;
H=zeros(1,256);
for i=1:m
    for j=1:n  k=X(i,j,2); H(k+1)=H(k+1)+1; end
end
Hb=H;
H=zeros(1,256);
for i=1:m
    for j=1:n  k=X(i,j,3); H(k+1)=H(k+1)+1; end
end
Hg=H;
LC=0:40;
subplot(4,3,12)
plot(LC,Hr(6:46),'r',LC,Hb(6:46),'g',LC,Hg(6:46),'b')
xlabel('Display level 5 \sim 45 only')