JPEG Image Compression
JPEG Image Compression
Digital
Communications
Jonathan
Evangelista Brian
England
Lucas Muller
[JPEG IMAGE
COMPRESSION]
The paper analyzes JPEG image compression utilizing the discrete cosine
transform algorithm, quantization tables for compression quality factors and runlength encoding technique.
ENGI-4557
Digital Communications 1
JPEG Image Compression
Table of Contents
Introduction................................................................................................................ 2
Discrete Cosine Transform.......................................................................................... 3
DCT in Basis Vectors................................................................................................ 5
MATLAB Functions for Discrete Cosine Transform dct2 and idct2..........................8
Zig Zag Transformation.............................................................................................. 9
Run-Length Encoding................................................................................................ 10
Message Decoding................................................................................................ 12
Quantization for a JPEG Image.................................................................................. 12
Appendix A Code for DCT and Graphical User Interfaces (GUIs)...........................16
DCT as Basis Vectors (DCTTest.m).........................................................................16
JPEG Compression (used only for a sample block) (JPEGCompression.m)............17
JPEG Compression for Gray Scale Image (used in Lena Image)
(JPEGCompressionGrayscale,m)............................................................................ 19
90% Compression (JPEGCompression90.m)..........................................................21
10% Compression (JPEGCompression10.m)..........................................................23
Homepage (Homepage.m).................................................................................... 25
DCT Test Using Basis Vectors (DCTTest.m)............................................................27
GUI For Test Bench First Page (DCTFig.m)..............................................................28
GUI For Test Bench Second Page (DCTFig2.m).......................................................33
GUI for Lena First Page (DCTLena.m).....................................................................36
GUI For Lena Second Page (DCTLena2.m).............................................................38
Zig Zag Transform for Quantized, Rounded Matrix (zigzag.m)............................44
Run Length Encoding for Zig Zag Transformed Image (RLE_encode.m)..............46
Run Length Decoding for Received Encoded Image (RLE_decode.m)....................47
Inverse Zig Zag Function for Decoded Image (invzigzag.m)...............................48
Appendix B MATLAB Graphical User Interfaces......................................................50
Works Cited................................................................................................................. 52
ENGI-4557
Digital Communications 2
JPEG Image Compression
ENGI-4557
Digital Communications 3
JPEG Image Compression
Introduction
This project analyzes the method of modifying a raw image in spatial domain, such as a bitmap
file, into a compressed state. The compression method used is JPEG compression. The discrete
cosine transform (DCT) formula is a mathematical algorithm which takes the values of an image
in spatial domain and transforms them to the frequency domain; which is required for the
compression. A quantization table determines the type and amount of compression which is
desired on the image which is to be reconstructed. Many redundant high frequency values from
the image are removed using run-length encoding. This is what ultimately reduces the size of the
image file. Once the image is to be viewed, the file is decoded and reconstructed. The final
result is a compressed image.
ENGI-4557
Digital Communications 4
JPEG Image Compression
( 2 x +1 ) u
( 2 y +1 ) v
1
F ( u , v )= C u C v f ( x , y ) cos
cos
4
16
16
x=0 y=0
) (
(Eq. 1.1)
The formula which describes the inverse discrete cosine transform is:
7
( 2 x +1 ) u
( 2 y+ 1 ) v
1
F ( x , y )= C u C v f (u , v ) cos
cos
4
16
16
u=0 v=0
In both above cases,
Cu =
1
1
,u=0
, v=0
,
C
=
v
2
2
1,u 0
1,v 0
) (
(Eq. 1.2)
Digital Communications 5
JPEG Image Compression
ENGI-4557
The best way to understand this formula is to analyze a simple mathematical example. Observe
the 8x8 block of pixels below:
v
*
*
u
Figure 1.1
In the above 8x8 table which represents an 8x8 block of pixels, each asterisk (*) represents a
pixel with a value from 0-255. To get an idea of how the DCT formula works, an X has been
placed in the table in the 4th column and 3rd row, and this specific pixel will be analyzed in the
formula:
7
( 2 x +1 ) u
( 2 y +1 ) v
1
F ( u , v )= C u C v f ( x , y ) cos
cos
4
16
16
x=0 y=0
) (
So in this case, u = 3, and v = 2 because the eight columns and rows are from values 0 to 7 (not 1
to 8). Since u and v are not equal to zero, both Cu and Cv are both equal to 1. This yields the
equation below:
7
7
( 2 x+ 1 ) 3
( 2 y +1 ) 2
1
F ( 3,2 )= f ( x , y ) cos
cos
4 x=0 y=0
16
16
) (
(Eq. 1.3)
ENGI-4557
Digital Communications 6
JPEG Image Compression
Solving the DCT formula would provide the DCT coefficient which is desired at this location of
the 8x8 block.
The position of DCT coefficients in the new 8x8 block of values in the frequency domain can be
represented as a high frequency or low frequency value. Below is an image provided by XIL
Programmers Guide August 1994 and gives an illustration to explain the frequency
distribution or DCT coefficient distribution in any given 8x8 block
Figure 1.2
The inverse DCT would provide the coefficient which is desired in the original state of the raw
image in the spatial domain.
F ( u , v )= p ( y , x ) d u [ x ] d v [ y ]
x=0 y=0
(Eq. 1.4)
ENGI-4557
Digital Communications 7
JPEG Image Compression
Where:
f 00 f 07
F=
f 70 f 77
] [
,
p 00
P=
p 70
p07
p77
] [
,
d 00 d 07
D=
d 70 d 77
C ()
(2t +1)
cos
2
16
(Eq. 1.5)
Where,
C()=
1
, =0
,
2
1, 0
And,
=uv
t={0,1,2, , 7 }
Observing these matrices, a comparison can be made to the original DCT equation (Eq. 1.1).
MATLAB code may be useful in understanding the methodology of the DCT in basis vectors.
The example below will go through the process of obtaining DCT coefficients from a raw image
8x8 block of values in the spatial domain:
%This is an example of an 8x8 block of spatial domain values. Each value
%represents an unsigned integer from 0 - 255 to represent a colour.
P0 =
ENGI-4557
150
159
159
161
162
162
155
161
160
161
162
162
160
162
161
161
161
161
163
160
162
161
163
161
158
160
162
160
162
163
156
159
155
157
157
158
156
159
155
157
157
158
Digital Communications 8
JPEG Image Compression
156;
159;
155;
157;
157;
158];
%The line below subtracts 128 from each element in the above matrix to
%make an signed integer to work with for the DCT algorithm.
P = P0 - 128;
%The two lines below are to produce the C(omega) term in Eq. 1.5.
S = eye(8)/2;
S(1,1) = 1/2/sqrt(2);
%Below, the DCT vectors are arranged and then multiplied by S to complete
%equation Eq 1.5.
D = zeros(8,8);
for t = [0:7]
for w = [0:7] %where w is u and v
D(w+1, t+1) = cos((2*t+1)*w*pi/16);
end
end
%Equation Eq. 1.5 therefore is:
D = S * D;
%Perform forward DCT to obtain the 8x8 matrix of DCT coefficients.
F = D * P * D';
This program outputs the 8x8 matrix value of F (which would further be rounded to 0 decimal
places):
ENGI-4557
Digital Communications 9
JPEG Image Compression
This is one possible way to achieve DCT coefficients in MATLAB, and is simply just an
explanation of the formula. In the case of this project, the MATLAB functions dct2 and
idct2 are utilized.
ENGI-4557
Digital Communications 10
JPEG Image Compression
Figure 2.1
Once the basic direction of the node has been determined, additional rules are required
for the proper functionality of the Zig Zag transformation.
For the case where the modulo 2 sum equals zero, the diagonals have even numbered
coordinate sums. When the X coordinate is at the minimum (1) and Y is at the maximum (8), the
current node is the top-right and it must move down-left one space. If the X coordinate is at the
minimum elsewhere, the code must move one space to the right. If the Y coordinate is at the
ENGI-4557
Digital Communications 11
JPEG Image Compression
maximum and the X coordinate is anywhere but the minimum, the code moves down one space.
Any other spaces with even coordinate sums result in the code reading the value and move upright.
For cases where the modulo 2 sum is not zero, the diagonals have odd numbered
coordinate sums. When the X coordinate is at the maximum and the Y coordinate is not at the
maximum, the entry is at the lower left corner and the code moves one space to the right. If the Y
coordinate is at the minimum and the X coordinate is at any value less than the maximum, the
code moves down one space. Any other spaces with odd coordinate sums result in the code
reading the value and move down-left.
Lastly, then the code reads the value with X and Y at the maximum and copies that entry
into the output array, the Zig Zag transformation is complete. At this point, the output array
should contain most of the non-zero values at the beginning of the array followed by the zero
values in large sequential group, or several smaller sequential groups divided by non-zero values.
It is at this point that the output array will be Run-Length Encoded to reduce the number of bytes
and subsequently increase the transmission speed.
Run-Length Encoding
The Run-Length Encoding receives a one-dimensional array of any size and scans
through each entry. It first reads the entry and adds that value to the one-dimensional output
array. Next, it checks ahead in the input array until the value is not equal to the first one scanned.
The number of entries checked is then added as the next value in the output array. As shown in
figure 2.2, the code reads a value of 8 which is repeated 3 times in a row; value 0 which only
occurs once in a row; value 4 which is repeated twice; and value 0 again, which is repeated twice
this time.
Input Array
Output Array
[8, 8, 8, 0, 4, 4, 0, 0]
[8, 3, 0, 1, 4, 2, 0, 2]
Figure 2.2
In this particular case, Run-Length Encoding has not saved any space these values can
be assumed to use 4 bits per entry and both the input and output arrays have 8 entries each, 32
bits total. However, this method is incredibly useful with JPEG compression since the quantized
rounded matrix contains a short list of non-zero values in the upper right corner and is heavily
populated by zeros elsewhere. After the Zig Zag Transformation, this results in an array
containing 64 entries, mostly sequential zeros. This case can be seen in figure 2.3.
ENGI-4557
Digital Communications 12
JPEG Image Compression
Input Array
Output Array
[1, 2, 3, 4, 0, 0, 0, 0, , 0]
[1, 1, 2, 1, 3, 1, 4, 1, 0, n]
Figure 2.3
The Run-Length Encoding clearly loses efficiency when encoding values that are not
repeated since the output contains the value and the number of sequential entries (in this case, 1),
effectively doubling the number of bits required. However, each time a value is sequentially
repeated, we have saved bits equal to one entry. When we apply this to the Zig Zag transformed
quantized rounded matrix, we receive an output array that is significantly shorter than the 64entry matrix that we started with.
At this point, it would be possible to use Huffman encoding to further reduce the size of
the transmitted message but for the scope of this project, this step is complex and unnecessary.
The image has been compressed and can now be transmitted to another device.
See Figure 2.4 below for the complete process:
Message Decoding
Following the transmission of the message from the source device to the receiver, the
message must be converted back into a readable format in order for the receiver to perform
transform operations to covert the data into the compressed JPEG image.
ENGI-4557
Digital Communications 13
JPEG Image Compression
Firstly, the encoded array must be inflated back into a one-dimensional array containing
64 entries. To do this, the Run-Length Encoding must be reversed by reading the first entry in the
input array, adding that value to the output array N times, where N is the number following the
value. This process is repeated until the input array has been completely read, which should be a
total of 64 entries.
Following the reverse Run-Length Encoding, the receiver must perform reverse Zig Zag
encoding to return values to their correct locations. This is done in a very similar way to the
regular Zig Zag encoding process. An 8x8 output matrix is initialized with all zeros and the
decoder moves through it in the exact same way as the encoder did before transmitting the
message. The only difference is that in this case, the values from the reverse Run-Length
Encoded message are read and entered into the current entry of the output matrix. The result of
this process is identical to the Quantized Rounded Matrix that the source device had created
before the transmission and encoding steps.
From this point, the receiving device may continue to use inverse transform methods to
recreate the viewable compressed JPEG image.
ENGI-4557
Digital Communications 14
JPEG Image Compression
which will gives us the quantized coefficients of the DCT values. An example is shown below of
this process using the standard luminance quantization matrix:
.
Digital Communications 15
JPEG Image Compression
ENGI-4557
qf <50 qf >50
Scale=5000/qf Scale=2002 qf
The standard JPEG luminance and chrominance quantization matrices are already set to a
quality factor of 50, this is why two conditions exists. By determining which quality factor is
used, the standard tables can scaled up or down. The equation below shows the formula to
calculate the new table values based on the desired quality factor used.
Qnew=
Q50Scale+50
100
The higher the quality factor used, the more the image will retain its integrity, i.e., less
compression will occur.
ENGI-4557
Digital Communications 16
JPEG Image Compression
The two quantization tables shown above have a quality factor of 10 and 90. What is
noticed is that a quality factor (qf) of 10 will cause the most compression to occur because it will
eliminate a large portion of the high frequency components. With a qf=90, the image will end up
retaining a large portion of the original values so less compression is achieved.
With the quality factor determined, the process of finding the normalized quantized
coefficient table is the same as described previously. Once this table has been calculated,
encoding can begin.
ENGI-4557
Digital Communications 17
JPEG Image Compression
[139
144
150
159
159
161
162
162
144
151
155
161
160
161
162
162
149
153
160
162
161
161
161
161
153
156
163
160
162
161
163
161
155
159
158
160
162
160
162
163
155
156
156
159
155
157
157
158
155
156
156
159
155
157
157
158
155;
156;
156;
159;
155;
157;
157;
158];
%The line below subtracts 128 from each element in the above matrix to
%make an signed integer to work with for the DCT algorithm.
P = P0 - 128;
%The two lines below are to produce the C(omega) term in Eq. 1.5.
S = eye(8)/2;
S(1,1) = 1/2/sqrt(2);
%Below, the DCT vectors are arranged and then multiplied by S to complete
%equation Eq 1.5.
D = zeros(8,8);
for t = [0:7]
for w = [0:7] %where w is u and v
D(w+1, t+1) = cos((2*t+1)*w*pi/16);
end
end
%Equation Eq. 1.5 therefore is:
D = S * D;
%Perform forward DCT to obtain the 8x8 matrix of DCT coefficients.
F = D * P * D';
ENGI-4557
Digital Communications 18
JPEG Image Compression
[139
144
150
159
159
161
162
162
144
151
155
161
160
161
162
162
149
153
160
162
161
161
161
161
153
156
163
160
162
161
163
161
155
159
158
160
162
160
162
163
155
156
156
159
155
157
157
158
155
156
156
159
155
157
157
158
155;
156;
156;
159;
155;
157;
157;
158]; %Original 8x8 Matrix.
P0 = P0original - 128; %Subtracts 128 from original 8x8 Matrix (for a signed
integer rather than unsigned).
P0fDCT = roundn(dct2(P0), 0); %Calculates forward DCT of P0.
Q0 =
[16
11
10
16
24
40
12
12
14
19
26
58
14
13
16
24
40
57
14
17
22
29
51
87
18
22
37
56
68
109
24
35
55
64
81
104
49
64
78
87
103
121
72
92
95
98
112
100
quantization matrix (for 50% compression)
51
60
69
80
103
113
120
103
61;
55;
56;
62;
77;
92;
101;
99]; %This is the
ENGI-4557
Digital Communications 19
JPEG Image Compression
%Now that the denormalized quantized matrix is obtained, the image needs to
%be reconstructed to its compressed 8x8 matrix.
Q0Reconstructed = idct2(Q0DeNorm); %Inverse Discrete Cosine Transform to
reconstruct image.
Q0Reconstructed128 = Q0Reconstructed + 128; %Adds 128 to the signed integer to
make it once again an unsigned 0-255 integer.
Q0ReconstructedRounded = roundn (Q0Reconstructed128, 0); %Rounds the matrix
values to integers.
%Now compressed image is reconstructed
ENGI-4557
Digital Communications 20
JPEG Image Compression
[16
12
14
14
18
24
49
72
11
12
13
17
22
35
64
92
10
14
16
22
37
55
78
95
16
19
24
29
56
64
87
98
24
26
40
51
68
81
103
112
40
58
57
87
109
104
121
100
51
60
69
80
103
113
120
103
61;
55;
56;
62;
77;
92;
101;
99];
%Q0 above is the standard quantization table which uses 50% compression.
Q0Norm{m,n} = ldivide(Q0, P0fDCT{m,n}); %Normalization of the matrix
using element-by-element division (left-array-division)
Q0NormRounded{m,n} = roundn(Q0Norm{m,n}, 0); %Simply rounds the
normalized matrix
%At this point, the zig-zag algorithm would be run and process each 8x8
%block into a row vector so that the run-length algorithm can be applied to
%compress the image file for transmission
Q0DeNorm{m,n} = times(Q0NormRounded{m,n},Q0); %Denormalization of the
matrix via element-by-element multiplication
ENGI-4557
Digital Communications 21
JPEG Image Compression
ENGI-4557
Digital Communications 22
JPEG Image Compression
60
60
65
85
110
175
255
255
50
70
80
110
185
255
255
255
80
95
120
145
255
255
255
255
120
130
200
255
255
255
255
255
200
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255;
255;
255;
255;
255;
255;
255;
255];
ENGI-4557
Digital Communications 23
JPEG Image Compression
ENGI-4557
Digital Communications 24
JPEG Image Compression
2
2
3
3
4
7
13
18
2
3
3
4
7
11
16
19
3
4
5
6
11
13
17
20
5
5
8
10
14
16
21
22
8
12
11
17
22
12
24
20
10
12
14
16
21
23
24
20
12;
11;
11;
12;
15;
18;
21;
20];
ENGI-4557
Digital Communications 25
JPEG Image Compression
Homepage (Homepage.m)
function varargout = Homepage(varargin)
ENGI-4557
Digital Communications 26
JPEG Image Compression
ENGI-4557
Digital Communications 27
JPEG Image Compression
% --- Outputs from this function are returned to the command line.
function varargout = Homepage_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject
handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in DCTExample.
function DCTExample_Callback(hObject, eventdata, handles)
run DCTFig
% hObject
handle to DCTExample (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
ENGI-4557
P0 =
[139
144
150
159
159
161
162
162
144
151
155
161
160
161
162
162
149
153
160
162
161
161
161
161
153
156
163
160
162
161
163
161
155
159
158
160
162
160
162
163
155
156
156
159
155
157
157
158
155
156
156
159
155
157
157
158
Digital Communications 28
JPEG Image Compression
155;
156;
156;
159;
155;
157;
157;
158];
%The line below subtracts 128 from each element in the above matrix to
%make an signed integer to work with for the DCT algorithm.
P = P0 - 128;
%The two lines below are to produce the C(omega) term in Eq. 1.5.
S = eye(8)/2;
S(1,1) = 1/2/sqrt(2);
%Below, the DCT vectors are arranged and then multiplied by S to complete
%equation Eq 1.5.
D = zeros(8,8);
for t = [0:7]
for w = [0:7] %where w is u and v
D(w+1, t+1) = cos((2*t+1)*w*pi/16);
end
end
%Equation Eq. 1.5 therefore is:
D = S * D;
%Perform forward DCT to obtain the 8x8 matrix of DCT coefficients.
F = D * P * D';
ENGI-4557
Digital Communications 29
JPEG Image Compression
%
%
H = DCTFIG returns the handle to a new DCTFIG or the handle to
%
the existing singleton*.
%
%
DCTFIG('CALLBACK',hObject,eventData,handles,...) calls the local
%
function named CALLBACK in DCTFIG.M with the given input arguments.
%
%
DCTFIG('Property','Value',...) creates a new DCTFIG or raises the
%
existing singleton*. Starting from the left, property value pairs are
%
applied to the GUI before DCTFig_OpeningFcn gets called. An
%
unrecognized property name or invalid value makes property application
%
stop. All inputs are passed to DCTFig_OpeningFcn via varargin.
%
%
*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%
instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help DCTFig
% Last Modified by GUIDE v2.5 22-Nov-2013 18:57:02
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DCTFig_OpeningFcn, ...
'gui_OutputFcn', @DCTFig_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback',
[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
ENGI-4557
Digital Communications 30
JPEG Image Compression
% --- Outputs from this function are returned to the command line.
function varargout = DCTFig_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject
handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
ENGI-4557
Digital Communications 31
JPEG Image Compression
ENGI-4557
Digital Communications 32
JPEG Image Compression
ENGI-4557
Digital Communications 33
JPEG Image Compression
ENGI-4557
Digital Communications 34
JPEG Image Compression
%
function named CALLBACK in DCTFIG2.M with the given input arguments.
%
%
DCTFIG2('Property','Value',...) creates a new DCTFIG2 or raises the
%
existing singleton*. Starting from the left, property value pairs are
%
applied to the GUI before DCTFig2_OpeningFcn gets called. An
%
unrecognized property name or invalid value makes property application
%
stop. All inputs are passed to DCTFig2_OpeningFcn via varargin.
%
%
*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%
instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help DCTFig2
% Last Modified by GUIDE v2.5 22-Nov-2013 19:05:03
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DCTFig2_OpeningFcn, ...
'gui_OutputFcn', @DCTFig2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback',
[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
ENGI-4557
Digital Communications 35
JPEG Image Compression
% --- Outputs from this function are returned to the command line.
function varargout = DCTFig2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject
handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
ENGI-4557
Digital Communications 36
JPEG Image Compression
ENGI-4557
Digital Communications 37
JPEG Image Compression
%
%
DCTLENA('Property','Value',...) creates a new DCTLENA or raises the
%
existing singleton*. Starting from the left, property value pairs are
%
applied to the GUI before DCTLena_OpeningFcn gets called. An
%
unrecognized property name or invalid value makes property application
%
stop. All inputs are passed to DCTLena_OpeningFcn via varargin.
%
%
*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%
instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help DCTLena
% Last Modified by GUIDE v2.5 22-Nov-2013 23:07:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DCTLena_OpeningFcn, ...
'gui_OutputFcn', @DCTLena_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback',
[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
ENGI-4557
Digital Communications 38
JPEG Image Compression
ENGI-4557
Digital Communications 39
JPEG Image Compression
%
DCTLENA2('Property','Value',...) creates a new DCTLENA2 or raises the
%
existing singleton*. Starting from the left, property value pairs are
%
applied to the GUI before DCTLena2_OpeningFcn gets called. An
%
unrecognized property name or invalid value makes property application
%
stop. All inputs are passed to DCTLena2_OpeningFcn via varargin.
%
%
*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%
instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help DCTLena2
% Last Modified by GUIDE v2.5 24-Nov-2013 12:49:16
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DCTLena2_OpeningFcn, ...
'gui_OutputFcn', @DCTLena2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback',
[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
function varargout = DCTLena2_OutputFcn(hObject, eventdata, handles)
ENGI-4557
%
%
%
%
varargout
hObject
eventdata
handles
Digital Communications 40
JPEG Image Compression
ENGI-4557
%
double
Digital Communications 41
JPEG Image Compression
ENGI-4557
Digital Communications 42
JPEG Image Compression
ENGI-4557
Digital Communications 43
JPEG Image Compression
ENGI-4557
Digital Communications 44
JPEG Image Compression
ENGI-4557
Digital Communications 45
JPEG Image Compression
of X axis (dimension 1)
1);
of Y axis (dimension 2)
2);
i = 1;
%initializes a 1-dimensional matrix (vector) with length = the size of our
%imput matrix
output = zeros(1, xmax * ymax);
%---------------------------------%
%
%
%
%
%
%
+-----> +Y
|
|
|
V
+X
ENGI-4557
Digital Communications 46
JPEG Image Compression
y = y + 1;
end;
%CASE C: last column but not max row => move down
elseif ((y == ymax) && (x < xmax))
x = x + 1;
%CASE D: all other cases => move up-right
elseif ((x > xmin) && (y < ymax))
x = x - 1;
y = y + 1;
end;
%===Downwards Direction===
else %points where x+y != a multiple of 2
%CASE E: bottom row => move right
if (x == xmax)
y = y + 1;
%CASE F: first column, any row but first
elseif (y == ymin && x < xmax)
x = x + 1;
%CASE G: all other cases => move down-left
elseif ((x < xmax) && (y > ymin))
% all other cases
x = x + 1;
y = y - 1;
end;
end;
%increment i to add the next element to output array
i = i + 1;
%Output element i = the current point in the matrix in(x,y)
output(i) = in(x, y);
%CASE I: bottom-right corner => break loop
if ((x == xmax) && (y == ymax))
output(i) = in(x, y);
break
end;
end;
ENGI-4557
Digital Communications 47
JPEG Image Compression
ENGI-4557
Digital Communications 48
JPEG Image Compression
ENGI-4557
Digital Communications 49
JPEG Image Compression
ENGI-4557
Digital Communications 50
JPEG Image Compression
cur_index=cur_index+1;
elseif cur_col~=1 && cur_row~=num_rows && mod(cur_row+cur_col,2)~=0
out(cur_row,cur_col)=in(cur_index);
cur_row=cur_row+1;
cur_col=cur_col-1; %move diagonally left down
cur_index=cur_index+1;
elseif cur_row~=1 && cur_col~=num_cols && mod(cur_row+cur_col,2)==0
out(cur_row,cur_col)=in(cur_index);
cur_row=cur_row-1;
cur_col=cur_col+1; %move diagonally right up
cur_index=cur_index+1;
elseif cur_index==tot_elem
element
out(end)=in(end);
break
end
end
ENGI-4557
Tag: showspatial
Digital Communications 51
JPEG Image Compression
Tag:
showdctcoefficients
Tag: showquant
Tag: transmit
Tag: zigzag
Tag: normalized
Tag: zigzag
Tag: reconstructed
ENGI-4557
Digital Communications 52
JPEG Image Compression
Tag: OriginalValues
Tag: tenpercent
Tag:
ShowCompressed
Tag: blockvalues
Tag: ninetypercent
Tag: dctcoefficients
Tag: ShowOriginal
Tag: chrominance50
Tag: normalized
Tag: save
Tag: reconstructed
Tag:
showcompressedsize
ENGI-4557
Digital Communications 53
JPEG Image Compression
Works Cited
[1] MathWorks. [Online]. www.mathworks.com
[2] Gregory K. Wallace, "The JPEG Still Picture Compression Standard," Maynard,
Massechusetts, 1992.
[3] Andrew B. Watson, "Image Compression Using the Discrete Cosine Transform," NASA
Ames Research Centre, 1994.
[4] "JPEG Baseline Sequential Codec," XIL Programmer's Guide, 1994.
[5] "Terminal Equipment and Protocols for Telematic Services: Information Technology Digital Compression and Coding of Continuous-Tone Still Images-Requirements and
Guidelines," International Telecommunication Union, 1992.
[6] Peter Gent Ken Cabeen, "Image Compression and the Discrete Cosine Transform," College
of the Redwoods,.
[7] Stanislav Hanus Tomas Fryza, "Image Compression Algorithms Optimized for MATLAB,"
Brno University of Technology, Purkynova, Czech Republic,.