function Yq=q5(X,r)

% X is the image file
%p represents the loss parameter
%r represents the question number 3 ,4, 5, or 6 from Pg. 513 in Numerical
%Analysis Tim Sauer

if(ischar(X)==1)
    xd=double(imread(X)); %reads image to unit8 and converts to a double
else
    xd=X; %assumes that the previous step has already been done
end

[picWidth,picHeight,colorNum]=size(xd); %gets the dimensions
n=floor(picWidth/8); %necessary number of iterations
m=floor(picHeight/8);

%Check that the image size is a multiple of 8
if (or(picWidth/8~=n,picHeight/8~=m))
    Yq='The picture is not a multiple of 8. Please crop it to multiples of 8 to continue';
    return; 
end

 %construct the C matrix for 8x8 blocks
 k=8;
 for i=2:k
    for j=1:k
        C(i,j)=sqrt(2/k)*cos((i-1)*(j-1/2)*pi/k);
    end
end
C(1,1:k)=ones(1,k)/sqrt(k);
%end construction of C


switch r
    case 3
        compImg=zeros(picWidth,picHeight); %create holder for the final image
        %graph original
        figure(1)
        subplot(2,2,1)
        imagesc(uint8(xd(:,:,1)));colormap(gray);
        title('Original Image');
        
        xGray=color2gray(X,false);
        for w=0:2
            %construct Q linear matrix
            Qlin=zeros(8,8);
            for i=0:7
                for j=0:7
                    Qlin(i+1,j+1)=8*2^w*(i+j+1);
                end
            end %end Q linear matrix
            
            for i=1:n % Spans Left to Right
                for j=1:m %Spans Top to Bottom 
                    xc=xGray(8*i-7:8*i,8*j-7:8*j);
                    xc=xc-128; 
                    Z=C*xc*C';
                    Yq=round(Z./Qlin);
                    Ydq=Yq.*Qlin;
                    Xdq=C'*Ydq*C;
                    Xe=Xdq+128;
                    compImg(8*i-7:8*i,8*j-7:8*j,1)=Xe;
                end
            end
        subplot(2,2,w+2)
        imagesc(uint8(compImg));colormap(gray);
        t=strcat('Compressed Image P=',num2str(2^w));
        title(t);
        end
    case 4
        compImg=zeros(picWidth,picHeight); %create holder for the final image
        %graph original
        figure(2)
        subplot(2,2,1)
        imagesc(uint8(xd(:,:,1)));colormap(gray);
        title('Original Image');
        
        xGray=color2gray(X,false);
        for w=0:2
            %construct Q linear matrix
                Qjpeg=2^w*[[16 11 10 16 24 40 51 61];[12 12 14 19 26 58 60 55];[14 13 16 24 40 57 69 56];...
            [14 17 22 29 51 87 80 62];[18 22 37 56 68 109 103 77];[24 35 55 64 81 104 113 92];...
            [49 64 78 87 103 121 120 101];[72 92 95 98 112 100 103 99]];
            
            for i=1:n % Spans Left to Right
                for j=1:m %Spans Top to Bottom 
                    xc=xGray(8*i-7:8*i,8*j-7:8*j);
                    xc=xc-128; 
                    Z=C*xc*C';
                    Yq=round(Z./Qjpeg);
                    Ydq=Yq.*Qjpeg;
                    Xdq=C'*Ydq*C;
                    Xe=Xdq+128;
                    compImg(8*i-7:8*i,8*j-7:8*j,1)=Xe;
                end
            end
        subplot(2,2,w+2)
        imagesc(uint8(compImg));colormap(gray);
        t=strcat('Compressed Image P=',num2str(2^w));
        title(t);
        end
        
    case 5
        compImg=zeros(picWidth,picHeight,colorNum); %create holder for the final image
        %graph original
        figure(3)
        subplot(2,2,1)
        imagesc(uint8(xd));
        title('Original Image');
        
        for w=0:2
            %construct Q linear matrix
            Qlin=zeros(8,8);
            for i=0:7
                for j=0:7
                    Qlin(i+1,j+1)=8*2^w*(i+j+1);
                end
            end %end Q linear matrix
            for h=1:colorNum
                for i=1:n % Spans Left to Right
                    for j=1:m %Spans Top to Bottom 
                        xc=xd(8*i-7:8*i,8*j-7:8*j,h);
                        xc=xc-128; 
                        Z=C*xc*C';
                        Yq=round(Z./Qlin);
                        Ydq=Yq.*Qlin;
                        Xdq=C'*Ydq*C;
                        Xe=Xdq+128;
                        compImg(8*i-7:8*i,8*j-7:8*j,h)=Xe;
                    end
                end
            end
        subplot(2,2,w+2)
        imagesc(uint8(compImg));
        t=strcat('Compressed Image P=',num2str(2^w));
        title(t);
        end
    case 6
        compImg=zeros(picWidth,picHeight,colorNum); %create holder for the final image
        %graph original
        figure(4)
        subplot(2,2,1)
        imagesc(uint8(xd));
        title('Original Image');
        
        %constructs Qcol matrix for luminance used on U and V 
        Qcol=99*ones(8,8); Qcol(1,1:4)=[17 18 24 47]; Qcol(2,1:4)=[18 21 26 66];
        Qcol(3,1:3)=[24 26 56]; Qcol(4,1:2)=[47 66];

        %constructs the luminance matrix from imread data
        xl=zeros(picWidth,picHeight,3);
        xl(:,:,1)= 0.299*xd(:,:,1)+.587*xd(:,:,2)+.114*xd(:,:,3); %Y
        xl(:,:,2)=xd(:,:,3)-xl(:,:,1); % U
        xl(:,:,3)=xd(:,:,1)-xl(:,:,1); %V

        % stores the trasnform data
        temp=zeros(picWidth, picHeight, 3);
        
        for w=0:2
            %construct Quantization JPEG
            Qjpeg=2^w*[[16 11 10 16 24 40 51 61];[12 12 14 19 26 58 60 55];[14 13 16 24 40 57 69 56];...
            [14 17 22 29 51 87 80 62];[18 22 37 56 68 109 103 77];[24 35 55 64 81 104 113 92];...
            [49 64 78 87 103 121 120 101];[72 92 95 98 112 100 103 99]];

            for h=1:colorNum
                if h==1
                    for i=1:n % Spans Left to Right
                        for j=1:m %Spans Top to Bottom 
                            xc=xl(8*i-7:8*i,8*j-7:8*j,h); 
                            xc=xc-128;
                            Z=C*xc*C';
                            Yq=round(Z./Qjpeg);
                            Ydq=Yq.*Qjpeg;
                            Xe=C'*Ydq*C;
                            Xf=Xe+128;
                            temp(8*i-7:8*i,8*j-7:8*j,h)=Xf;
                        end
                    end
                else
                    for i=1:n % Spans Left to Right
                        for j=1:m %Spans Top to Bottom 
                            xc=xl(8*i-7:8*i,8*j-7:8*j,h); 
                            xc=xc-128;
                            Z=C*xc*C';
                            Yq=round(Z./Qcol);
                            Ydq=Yq.*Qcol;
                            Xe=C'*Ydq*C;
                            Xf=Xe+128;
                            temp(8*i-7:8*i,8*j-7:8*j,h)=Xf;
                        end
                    end
                end
            end
            compImg(:,:,1)=temp(:,:,1)+temp(:,:,3); %recover red
            compImg(:,:,2)=(temp(:,:,1)-0.299*xd(:,:,1)-.114*xd(:,:,3))/(.587); %recover green
            compImg(:,:,3)=temp(:,:,1)+temp(:,:,2); %recover blue
            subplot(2,2,w+2)
            imagesc(uint8(compImg));
            t=strcat('Compressed Image P=',num2str(2^w));
            title(t);
        end
    end
end

        
        
        
        
        
        
        
        
        
        
% if q==false                             %Does RGB compression
%     compImg=zeros(picWidth,picHeight,3);
%     Qy=p*[[16 11 10 16 24 40 51 61];[12 12 14 19 26 58 60 55];[14 13 16 24 40 57 69 56];...
%     [14 17 22 29 51 87 80 62];[18 22 37 56 68 109 103 77];[24 35 55 64 81 104 113 92];...
%     [49 64 78 87 103 121 120 101];[72 92 95 98 112 100 103 99]];
%     for h=1:colorNum %one for each color
%         for i=1:n % Spans Left to Right
%             for j=1:m %Spans Top to Bottom 
%                 xc=xd(8*i-7:8*i,8*j-7:8*j,h);
%                 xc=xc-128; 
%                 Z=C*xc*C';
%                 Yq=round(Z./Qy);
%                 Ydq=Yq.*Qy;
%                 Xdq=C'*Ydq*C;
%                 Xe=Xdq+128;
%                 compImg(8*i-7:8*i,8*j-7:8*j,h)=Xe;
%             end
%         end
%     end
% else                        %does luminance construction
%     %constructs Qc matrix for luminance used on U and V 
%     Qcol=99*ones(8,8); Qcol(1,1:4)=[17 18 24 47]; Qcol(2,1:4)=[18 21 26 66];
%     Qcol(3,1:3)=[24 26 56]; Qcol(4,1:2)=[47 66];
%     
%     %constructs the luminance matrix from imread data
%     xl=zeros(picWidth,picHeight,3);
%     xl(:,:,1)= 0.299*xd(:,:,1)+.587*xd(:,:,2)+.114*xd(:,:,3); %Y
%     xl(:,:,2)=xd(:,:,3)-xl(:,:,1); % U
%     xl(:,:,3)=xd(:,:,1)-xl(:,:,1); %V
%     
%     % stores the trasnform data
%     temp=zeros(picWidth, picHeight, 3);
%     
%     
%     %h=1 case
%     Qy=p*[[16 11 10 16 24 40 51 61];[12 12 14 19 26 58 60 55];...
%         [14 13 16 24 40 57 69 56];[14 17 22 29 51 87 80 62];...
%         [18 22 37 56 68 109 103 77];[24 35 55 64 81 104 113 92];...
%         [49 64 78 87 103 121 120 101];[72 92 95 98 112 100 103 99]];
%     
%     for h=1:colorNum
%         if h==1
%             for i=1:n % Spans Left to Right
%                 for j=1:m %Spans Top to Bottom 
%                     xc=xl(8*i-7:8*i,8*j-7:8*j,h); 
%                     xc=xc-128;
%                     Z=C*xc*C';
%                     Yq=round(Z./Qy);
%                     Ydq=Yq.*Qy;
%                     Xe=C'*Ydq*C;
%                     Xf=Xe+128;
%                     temp(8*i-7:8*i,8*j-7:8*j,h)=Xf;
%                 end
%             end
%         else
%             for i=1:n % Spans Left to Right
%                 for j=1:m %Spans Top to Bottom 
%                     xc=xl(8*i-7:8*i,8*j-7:8*j,h); 
%                     xc=xc-128;
%                     Z=C*xc*C';
%                     Yq=round(Z./Qcol);
%                     Ydq=Yq.*Qcol;
%                     Xe=C'*Ydq*C;
%                     Xf=Xe+128;
%                     temp(8*i-7:8*i,8*j-7:8*j,h)=Xf;
%                 end
%             end
%         end
%     end
%     compImg(:,:,1)=temp(:,:,1)+temp(:,:,3); %recover red
%     compImg(:,:,2)=(temp(:,:,1)-0.299*xd(:,:,1)-.114*xd(:,:,3))/(.587); %recover green
%     compImg(:,:,3)=temp(:,:,1)+temp(:,:,2); %recover blue    
% end
% 
% subplot(1,2,2)
% if(colorNum==1)
%     imagesc(uint8(compImg(:,:,1)))
%     colormap(gray);
% else
%     imagesc(uint8(compImg))
% end
%  t=strcat('Compressed Image P=',num2str(p));
%  title(t);
%  
% end
%   