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.

#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) {
                 //putc(a,fp2);
                 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;
            /*if(t>50&&t<200)
                           t=0;
            else
                           t=255;*/
            putc(t,fp2);
		    putc(t,fp2);
		    putc(t,fp2);
        }
        cout<<endl;
    }
    
    fclose(fp1);
    fclose(fp2);
	getch();
	return 0;
}

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

A better indented version of my 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) {
                 //putc(a,fp2);
                 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;
}

Figured it out. Was making an index mistake. It's a pain to initialize 3d arrays dynamically, so i use a 1d array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.