Arrays getting disfigured automatically!

This is a discussion on Arrays getting disfigured automatically! within the C++ Programming forums, part of the General Programming Boards category; Hi, i've been trying to create a program for implementing high/low pass filters on images. I get the data from ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    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;
             }
        
             fclose(fp1);
             fclose(fp2);
    	 getch();
    	 return 0;
    }
    Output :

    Code:
    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.
    Last edited by rghai6; 10-08-2007 at 02:56 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    With your inconsistent indentation and total lack of whitespace, this code is pretty unreadable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    Sorry. There, i've tried to make it a little better.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Your indexing is wrong. You need to multiply the result of i*w+j with 3 before adding the component offset.

    Look at the first loop.
    i=0,j=0
    -> access 0, 1, 2
    i=0, j=1
    -> access 1, 2, 3
    i=0, j=2
    -> access 2, 3, 4
    ...
    i=1, j=0
    -> access 4, 5, 6
    i=1, j=1
    -> access 5, 6, 7

    and so on.

    My recommendation is to use a three-dimensional Boost.MultiArray.
    http://www.boost.org/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    Oh, yeah. Thanks a lot !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 03:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21