Arrays getting disfigured automatically!

R

rghai6

Hi, i've been trying to create a program for implementing high/low
pass filters on images. I get the data from a 24 bit .bmp file using
the old file handling techniques. The file handling portion of my
program works like a breeze, it is the arrays that are giving me the
problem.

My I/P file:4*4 pixel 24 bit bmp

I've added the output as well at the end to show the difference in
array contents in the start an at the beginning.

I'm really confused as to why the contents of the array are changing ?

PS:I am not doing any processing on the image yet, just trying to get
the pixel color intensities into the array c and use this to output
the pixel intensities to the new file.

code:

#include <conio.h>
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

int main() {

FILE *fp1,*fp2;
int a,h=0,w=0,t=0;
int j=0,i=0;
//clrscr();

fp1=fopen("rd.bmp","rb");
fp2=fopen("rd2.bmp","wb");


for (i=0;i<54;i++) {
a=getc(fp1);
putc(a,fp2);

if(i>=18&&i<22)
w+=int(a*(pow(256,(i-18))));
if(i>=22&&i<26)
h+=int(a*(pow(256,(i-22))));

}


cout<<endl<<"Pixels to process: "<<h<<" "<<w<<endl;
cout<<"Original image"<<endl;
int *c = new int[w*h*3];
//int c[48]={0};


for (i=0;i<h;i++) {
for (j=0;j<w;j++) {
t=0;
a=getc(fp1);
t+=a;
c[i*w+j]=(int)a;
cout<<c[i*w+j]<<" ";
a=getc(fp1);
t+=a;
c[i*w+j+1]=(int)a;
cout<<c[i*w+j+1]<<" ";
a=getc(fp1);
t=(t+a)/3;
c[i*w+j+2]=(int)a;
cout<<c[i*w+j+2]<<" ";
if(a==-1) break;
}

cout<<endl;
if(a==-1) {
cout<<endl<<"Breaking..."<<i<<" "<<j; break ;
}
}

cout<<endl<<"Processed pixels : "<<i<<" "<<j<<endl;
cout<<endl;
int cn=0;

while((a=getc(fp1))!=-1) {
cout<<" "<<a;
cn++;
}
cout<<endl<<"Pixels not proc : "<<cn/3<<endl;

for (i=0;i<h;i++) {
for (j=0;j<w;j++) {

cout<<c[i*w+j]<<" "<<c[i*w+j+1]<<" "<<c[i*w+j
+2]<<" ";
t=(c[i*w+j]+c[i*w+j+1]+c[i*w+j+2])/3;

putc(t,fp2);
putc(t,fp2);
putc(t,fp2);
}
cout<<endl;
}





Output :

Pixels to process: 4
4
Original
image
0 0 0 255 255 255 0 0 255 255 255
255
255 255 255 255 255 255 255 255 255 255 255
255
255 255 255 255 255 255 255 255 255 255 255
255
255 255 255 255 255 255 255 255 255 255 255
255

Processed pixels : 4
4


Pixels not proc :
0
0 255 0 255 0 255 0 255 255 255 255
255
255 255 255 255 255 255 255 255 255 255 255
255
255 255 255 255 255 255 255 255 255 255 255
255
255 255 255 255 255 255 255 255 255 255 255 255

Thanks
fclose(fp1);
fclose(fp2);
getch();
return 0;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi, i've been trying to create a program for implementing high/low
pass filters on images. I get the data from a 24 bit .bmp file using
the old file handling techniques. The file handling portion of my
program works like a breeze, it is the arrays that are giving me the
problem.

My I/P file:4*4 pixel 24 bit bmp

I've added the output as well at the end to show the difference in
array contents in the start an at the beginning.

I'm really confused as to why the contents of the array are changing ?

You have chosen to represent a 3D array [H][W][3] as a 1D
(single-dimensional) array.

Generally given 3D indices y, x, z the 1D index i is then i = y*W*3 +
x*3 + z = 3*(W*y + x) + z, while you're using i = W*y + x + z.

That means you're accessing the same 1D array element with more than one
3D (y, x, z).

By the way, one reason that you needed to ask is probably that the
program logic is expressed at the lowest possible level of abstraction,
C style coding.

Try to introduce e.g. a 3D array class.

Cheers, & hth.,

- Alf
 
R

rghai6

* (e-mail address removed):
Hi, i've been trying to create a program for implementing high/low
pass filters on images. I get the data from a 24 bit .bmp file using
the old file handling techniques. The file handling portion of my
program works like a breeze, it is the arrays that are giving me the
problem.
My I/P file:4*4 pixel 24 bit bmp
I've added the output as well at the end to show the difference in
array contents in the start an at the beginning.
I'm really confused as to why the contents of the array are changing ?

You have chosen to represent a 3D array [H][W][3] as a 1D
(single-dimensional) array.

Generally given 3D indices y, x, z the 1D index i is then i = y*W*3 +
x*3 + z = 3*(W*y + x) + z, while you're using i = W*y + x + z.

That means you're accessing the same 1D array element with more than one
3D (y, x, z).

By the way, one reason that you needed to ask is probably that the
program logic is expressed at the lowest possible level of abstraction,
C style coding.

Try to introduce e.g. a 3D array class.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Oh yeah. Thanks a lot!
 
R

rghai6

* (e-mail address removed):
Hi, i've been trying to create a program for implementing high/low
pass filters on images. I get the data from a 24 bit .bmp file using
the old file handling techniques. The file handling portion of my
program works like a breeze, it is the arrays that are giving me the
problem.
My I/P file:4*4 pixel 24 bit bmp
I've added the output as well at the end to show the difference in
array contents in the start an at the beginning.
I'm really confused as to why the contents of the array are changing ?

You have chosen to represent a 3D array [H][W][3] as a 1D
(single-dimensional) array.

Generally given 3D indices y, x, z the 1D index i is then i = y*W*3 +
x*3 + z = 3*(W*y + x) + z, while you're using i = W*y + x + z.

That means you're accessing the same 1D array element with more than one
3D (y, x, z).

By the way, one reason that you needed to ask is probably that the
program logic is expressed at the lowest possible level of abstraction,
C style coding.

Try to introduce e.g. a 3D array class.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Oh and i did try using a 2d array but dynamic initialization was
proving to be a big headache. I'm now planning on trying to use
Boost.MultiArray. Let's see.
 
T

tragomaskhalos

if(i>=18&&i<22)
w+=int(a*(pow(256,(i-18))));
if(i>=22&&i<26)
h+=int(a*(pow(256,(i-22))));

You really don't want to be using pow here; use <<
instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top