Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > having trouble with Discrete Cosine Transform program

Reply
Thread Tools

having trouble with Discrete Cosine Transform program

 
 
khaleel.alyasini@gmail.com
Guest
Posts: n/a
 
      03-24-2006
anyone could point me out where did i do wrong? it seems that i can't
get back the original Lena image after the IDCT(inverse discrete cosine
transform) process. the output raw image is nothing more than a grey
colored canvas ...



//Header files declarations
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <stdlib.h>

//Global declarations
const int pixel=256;
unsigned char impix[pixel][pixel];
double dctblock[pixel][pixel];
double idctblock[pixel][pixel];
static const double pi = 3.141593;

//Functions declarations
void copypixel();
void openfile();
void dct();
void idct();
void printdct();
void imageblock();
void printidct();
void printdctimage();
void printidctimage();
double c(int);


void main()
{
openfile();
imageblock();
dct();
printdct();
printdctimage();
idct();
printidct();
printidctimage();
}

void openfile()
{

ifstream rawimage("lena.raw", ios::in|ios::binary);
if(!rawimage)
{
cerr<<"Error in reading file\n";
exit(1);
}

for(int i=0; i<pixel; i++)
{
for(int j=0; j<pixel; j++)
{
rawimage>>impix[i][j];
}
}
}

void imageblock()
{

ofstream rawimagetxt("rawimage.txt", ios:ut|ios::binary);
if(!rawimagetxt)
{
cerr<<"Error in reading file\n";
exit(1);
}

for(int i=0; i<pixel; i++)
{
for(int j=0; j<pixel; j++)
{
rawimagetxt<<int(impix[i][j])<<" ";
}
}
}

void dct()
{
for(int u=0;u<pixel;u++) {
for(int v=0;v<pixel;v++) {
double temp = 0.0;

for(int x=0;x<8;x++) {
for(int y=0;y<8;y++) {
temp += impix[x][y] * cos(pi *
(2 * x + 1) * u / 16) *
cos(pi * (2 * y + 1) * v / 16);
}
}

dctblock[u][v] = temp * (c(u) / 2) * (c(v) /
2);
}
}
}

void printdct()
{

ofstream dct("dct.txt", ios:ut|ios::binary);
if(!dct)
{
cerr<<"Error in reading file\n";
exit(1);
}

for(int i=0; i<pixel; i++)
{
for(int j=0; j<pixel; j++)
{
dct<<dctblock[i][j]<<" ";
}
}
}

void printidct()
{

ofstream idct("idct.txt", ios:ut|ios::binary);
if(!idct)
{
cerr<<"Error in reading file\n";
exit(1);
}

for(int i=0; i<pixel; i++)
{
for(int j=0; j<pixel; j++)
{
idct<<idctblock[i][j]<<" ";
}
}
}

void printdctimage()
{

ofstream dctimage("dct.raw", ios:ut|ios::binary);
if(!dctimage)
{
cerr<<"Error in reading file\n";
exit(1);
}

for(int i=0; i<pixel; i++)
{
for(int j=0; j<pixel; j++)
{
dctimage<<dctblock[i][j];
}
}
}

void idct()
{
for(int x=0;x<pixel;x++) {
for(int y=0;y<pixel;y++) {
double temp = 0.0;

for(int u=0;u<8;u++) {
for(int v=0;v<8;v++) {
temp += (c(u) / 2) * (c(v) / 2)
* dctblock[u][v] * cos(pi * (2 *
x + 1) * u / 16) * cos(pi * (2 * y + 1) * v / 16);
}
}

idctblock[x][y] = temp;
}
}
}

void printidctimage()
{

ofstream idctimage("idct.raw", ios:ut|ios::binary);
if(!idctimage)
{
cerr<<"Error in reading file\n";
exit(1);
}

for(int i=0; i<pixel; i++)
{
for(int j=0; j<pixel; j++)
{
idctimage<<idctblock[i][j];
}
}
}
double c(int number)
{
if(number == 0)
{
return 1/sqrt(2);
}
else
{
return 1;
}
}

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      03-24-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> anyone could point me out where did i do wrong? it seems that i can't
> get back the original Lena image after the IDCT(inverse discrete cosine
> transform) process. the output raw image is nothing more than a grey
> colored canvas ...


The DCT is off-topic here. Try in comp.dsp or comp.compression or
similar. If you have C++ language questions, feel free to ask them
here. See below for some source code problems that I spotted on a
cursory reading.

> //Header files declarations
> #include <iostream.h>
> #include <fstream.h>


Deprecated. Use <iostream> and <fstream>. You'll also need to add:

using namespace std;

> #include <math.h>
> #include <stdlib.h>


Prefer <cmath> and <cstdlib>.

>
> //Global declarations
> const int pixel=256;
> unsigned char impix[pixel][pixel];
> double dctblock[pixel][pixel];
> double idctblock[pixel][pixel];


Prefer to declare things as locally as possible, and pass parameters
into functions that need them. Global variable make for unreadable and
hard-to-debug code.

> static const double pi = 3.141593;


This is ok as a global, but static to indicate the object is restricted
to file scope is also deprecated. Use an anonymous namespace instead.

>
> //Functions declarations
> void copypixel();
> void openfile();
> void dct();
> void idct();
> void printdct();
> void imageblock();
> void printidct();
> void printdctimage();
> void printidctimage();
> double c(int);


Since you only have one file, you could just put main() at the end and
avoid the need for these prototypes altogether.

>
>
> void main()


int main()

See http://www.parashift.com/c++-faq-lit....html#faq-29.3

> {
> openfile();
> imageblock();
> dct();
> printdct();
> printdctimage();
> idct();
> printidct();
> printidctimage();
> }
>
> void openfile()
> {
>
> ifstream rawimage("lena.raw", ios::in|ios::binary);
> if(!rawimage)
> {
> cerr<<"Error in reading file\n";
> exit(1);
> }
>
> for(int i=0; i<pixel; i++)
> {
> for(int j=0; j<pixel; j++)
> {
> rawimage>>impix[i][j];


Stream inserters and extractors (i.e. the >> and << operators) are
intended only for formatted text I/O, not binary I/O. You need to use
istream::read() and ostream::write().

> }
> }
> }
>
> void imageblock()
> {
>
> ofstream rawimagetxt("rawimage.txt", ios:ut|ios::binary);


I suspect you didn't want the ios::binary here.

> if(!rawimagetxt)
> {
> cerr<<"Error in reading file\n";
> exit(1);
> }
>
> for(int i=0; i<pixel; i++)
> {
> for(int j=0; j<pixel; j++)
> {
> rawimagetxt<<int(impix[i][j])<<" ";
> }
> }
> }
>
> void dct()
> {
> for(int u=0;u<pixel;u++) {
> for(int v=0;v<pixel;v++) {
> double temp = 0.0;
>
> for(int x=0;x<8;x++) {
> for(int y=0;y<8;y++) {
> temp += impix[x][y] * cos(pi *
> (2 * x + 1) * u / 16) *
> cos(pi * (2 * y + 1) * v / 16);
> }
> }
>
> dctblock[u][v] = temp * (c(u) / 2) * (c(v) /
> 2);
> }
> }
> }
>
> void printdct()
> {
>
> ofstream dct("dct.txt", ios:ut|ios::binary);


Not binary.

> if(!dct)
> {
> cerr<<"Error in reading file\n";
> exit(1);
> }
>
> for(int i=0; i<pixel; i++)
> {
> for(int j=0; j<pixel; j++)
> {
> dct<<dctblock[i][j]<<" ";
> }
> }
> }
>
> void printidct()
> {
>
> ofstream idct("idct.txt", ios:ut|ios::binary);


Not binary.

> if(!idct)
> {
> cerr<<"Error in reading file\n";
> exit(1);
> }
>
> for(int i=0; i<pixel; i++)
> {
> for(int j=0; j<pixel; j++)
> {
> idct<<idctblock[i][j]<<" ";
> }
> }
> }
>
> void printdctimage()


Inaptly named function: you're not printing (which generally implies
text mode).

> {
>
> ofstream dctimage("dct.raw", ios:ut|ios::binary);
> if(!dctimage)
> {
> cerr<<"Error in reading file\n";
> exit(1);
> }
>
> for(int i=0; i<pixel; i++)
> {
> for(int j=0; j<pixel; j++)
> {
> dctimage<<dctblock[i][j];
> }
> }
> }
>
> void idct()
> {
> for(int x=0;x<pixel;x++) {
> for(int y=0;y<pixel;y++) {
> double temp = 0.0;
>
> for(int u=0;u<8;u++) {
> for(int v=0;v<8;v++) {
> temp += (c(u) / 2) * (c(v) / 2)
> * dctblock[u][v] * cos(pi * (2 *
> x + 1) * u / 16) * cos(pi * (2 * y + 1) * v / 16);
> }
> }
>
> idctblock[x][y] = temp;
> }
> }
> }
>
> void printidctimage()


Inaptly named function: you're not printing (which generally implies
text mode).

> {
>
> ofstream idctimage("idct.raw", ios:ut|ios::binary);
> if(!idctimage)
> {
> cerr<<"Error in reading file\n";
> exit(1);
> }
>
> for(int i=0; i<pixel; i++)
> {
> for(int j=0; j<pixel; j++)
> {
> idctimage<<idctblock[i][j];


Don't use the << operator. See comments above.

> }
> }
> }
> double c(int number)
> {
> if(number == 0)
> {
> return 1/sqrt(2);
> }
> else
> {
> return 1;
> }
> }


Cheers! --M

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cosine function returning long double (high resolution) ratcharit@gmail.com C Programming 11 03-06-2008 08:36 PM
function/process to generate sine and cosine wave FPGA VHDL 15 02-09-2008 02:21 PM
sine and cosine wave generation FPGA VHDL 8 01-14-2008 09:11 PM
cosine calcs Niv VHDL 9 08-16-2006 05:25 PM
Cosine algo, was Re: sqrt algo pete C Programming 108 11-05-2005 05:33 AM



Advertisments