473,513 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrays getting disfigured automatically!

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;
}

Oct 8 '07 #1
2 1308
Code got messed up last time. Modified it...
New 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;
}
fclose(fp1);
fclose(fp2);
getch();
return 0;

}
Oct 8 '07 #2
On Oct 8, 9:14 am, rgh...@gmail.com wrote:
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.

Oct 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5075
by: Jason | last post by:
I have a number of arrays that are populated with database values. I need to determine which array has the highest ubound out of all the arrays. The array size will always change based on the...
19
2822
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
46
4640
by: Blue Ocean | last post by:
How do I do it? I'm doing a practice problem which includes me implementing a set of ints as a class. I don't want to use vector because that would be cheating. ;) I don't want to spend...
21
3897
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
79
3333
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
10
3127
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
10
4969
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
16
3460
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
9
1788
by: Oliver Graeser | last post by:
Hi All, I do some ensemble averaging calculation that requires me to accumulate very many distributions which I store in arrays. Basically something like class AdaptiveNW{ public: int**...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.