# How do I calculate DCT in Delphi?

I need help writing the coding part to calculate the DCT
(discrete cosine transfer) of an 8x8 matrix. Can some kind
person walk me through this Pascal coding process, please.

I found this page, and I want to use it as a reference for
checking my work, by copying the image in it, and using it
as the reference, as well as the QMx quantization numbers
for each, in my completed and working DCT model. I was
able to screen copy the color image of the birds. Now, to
set up the rest of the process.

http://www.cs.sfu.ca/CC/365/li/interactive-jpeg/Ijpeg.html

And, here is a webpage that helps to describe DCT:

http://www.nationmaster.com/encyclopedia/JPEG

Here's my detail plea.

I've searched the internet, but all I find is c/c++ or java
code pieces, and are enhanced or designed to be fast, but
they are too complex follow and understand.
What I would like, is to have something that is in the
most basic of simple way possible. Speed is not important
here. I want to understand and learn this DCT process.
So, if speed could be overlooked for now, while I learn
this first, I would really appreciate it.

I have tons and tons of info on DCT from all over the web
that I copied to my harddrive. Many are pdf, docs, ppt,
and txt files. They go on to describe the nature or process
of DCT, but it lacks one thing. Simplicity. The proper
expression in Pascal format is missing. Everything is in
c/c++ and too cryptic to follow. And because of this, I
can't go further without actual code to work with.

What I would like, is the proper formula in a Pascal syntax
that caculates DCT for the filled in rgb[8,8] data, and then
with the calculated DCT values, fills another matrix called
dct[8,8]. This matrix will now have the dct[8,8] values.
(My next step will be to Quantize this dct[8,8] with a
quant[8,8] matrix, and further on)
(I do know how to caluclate the Quantization, however)

Lets say I have a 512x512 image, and I read in all the RGB
pixel values into rgb[8,8] with the code:

[code]
var
rgb : array[0..8-1, 0..8-1] of double;
r,c : integer;
.
.
begin

// read in pixles, starting at top, left image
// area, i.e., rgb[0,0]..

for r:=0 to 8-1 do begin
for c:=0 to 8-1 do begin
rgb[r,c] := imabe1.canvas.GetPixels[r,c];
end;
end;

// fills rgb[8,8] matrix with pixels numbers
// looking like this, (but with actual real numbers.
// the 000's are just to show what I'm describing
// below. I guess I am thinking in the range of
// 0-255, but I realize that I defined my rgb[8,8]
// array matrix as Double's.

// 000 000 000 000 000 000 000 000
// 000 000 000 000 000 000 000 000
// 000 000 000 000 000 000 000 000
// 000 000 000 000 000 000 000 000
// 000 000 000 000 000 000 000 000
// 000 000 000 000 000 000 000 000
// 000 000 000 000 000 000 000 000
// 000 000 000 000 000 000 000 000

end;[/code]

Here is some code that I found, and converted from c/c++
to Pascal, but it uses a pre-lookup table which I don't
need at this time. I need the long way of coding it in
one line, for now. In Pascal, the syntax is suppose to be
cos(x) right? But,

[code]
B[k1,k2] := B[k1,k2] + ( A[i,j] * (cosine[k1,i] * cosine[k2,j]) );
if (k1 = 0) then Cu := SQRT2o2 else Cu := 1.0;
if (k2 = 0) then Cv := SQRT2o2 else Cv := 1.0;
B[k1,k2] := B[k1,k2] * (0.25 * Cu * Cv);
[/code]

This line is using a lookup table of precalculated cosine numbers:

[code] B[k1,k2] := B[k1,k2] + ( A[i,j] * (cosine[k1,i] * cosine[k2,j]) );[/code]

But I am really looking for the longer way of doing it, and
without a lookup table, so that I understand the whole process
of calculating DCT.

I did find another reference to calculating DCT, but I don't
understand it here:

[0006]
DCT[v][u] = 1/4xCu.Cv. SIGMA SIGMA F[y][x].cos((2x+1)u pi
/16).cos((2y+1)v pi /16)
Cu, Cv = 1/2(u,v = 0), 1(otherwise)

My goal is to write several functions to do calculations
and things, with the following routines to make things
easier to work with on a procedure/function base, in
the order of DCT process:

1-Procedure fillRGBregion();
2-Procedure rgb_to_YUV();
3-Procedure reduceto128Values(); does a (num[r,c]-128)
4-Procedure calculateDCT();
5-Procedure calculateQuantization();

I believe the above is the right order of obtaining DCT, but
I'm not sure of the -127 to +128 routine. Where exactly is this
suppose to happen. It's use is for shifting by 128 and THEN
doing the DCT part.

Later, I plan on enhancing the speed of the DCT algorithem
process. I just need to get it working for now

-mydelphi