K
khaleel.alyasini
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[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[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[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[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[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[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[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[j];
}
}
}
double c(int number)
{
if(number == 0)
{
return 1/sqrt(2);
}
else
{
return 1;
}
}
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[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[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[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[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[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[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[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[j];
}
}
}
double c(int number)
{
if(number == 0)
{
return 1/sqrt(2);
}
else
{
return 1;
}
}