Solved

C++ Function (Arithmetic in ForLoop)

Posted on 2007-11-27
22
308 Views
Last Modified: 2010-04-21
I'm trying to convert an "interpolation" function into C++ code.  

Please check out the JPG posted at:
http://img523.imageshack.us/img523/4840/interpolatioinbd5.jpg

Based on the info shown in the JPG above, the basic structure (ForLoops) should be:

for (i = 0; i < numberOfRows; i++)
   for (j = 0; j < numberOfColumns; j++)
        for (p = 0; p < Fm; p++)
             for (q = 0; q < Fn; q++)

How do I need to index arithmetic to get to the proper image values for the original and the interpolated image?

Thanks,
EEH
0
Comment
Question by:ExpExchHelp
  • 12
  • 9
22 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
in theory, what you need is:

for (i = 0; i < numberOfRows; i++)
   for (j = 0; j < numberOfColumns; j++)
        for (p = 0; p < Fm; p++)
             for (q = 0; q < Fn; q++)
                   newImage[i*Fm+p, j*Fn+q] = oldImage[i,j];

0
 

Author Comment

by:ExpExchHelp
Comment Utility
jaime,

thanks -- now seeing it, it makes total sense.

I added the braces and replaced the variable names.   The code is shown below.   Unfortunately, it still throws some errors (listed below as well).

What's still missing/wrong?

EEH

*********************

// Allocate this many number of bytes to the image.
unsigned char *imaged;
imaged = (unsigned char *) malloc(totalPixels);


// Interpolation ForLoop
for (i=0; i<numberOfRows; i++){
      for (j=0; j<numberOfColumns; j++){
            for (p=0; p<Fm; p++){
                  for (q=0; q<Fn; q++){
                  
                        imaged[i*Fm+p, j*Fn+q] = (int) image[i,j];
                  }
            }
      }
}


/* ERRORS:
error C2371: 'imaged' : redefinition; different basic types
error C2440: '=' : cannot convert from 'unsigned char *' to 'double *'
error C2108: subscript is not of integral type
*/
0
 
LVL 20

Expert Comment

by:ikework
Comment Utility
> error C2371: 'imaged' : redefinition; different basic types
"imaged" was declared earlier in the code with another type

> error C2108: subscript is not of integral type
(int) image[i,j] does not work in c/c++ .. it has to be:  image[i][j]

how was "image" declared? if it was "unsigned char" as well keep in mind that one pixel can be more than one byte depending on the bits per plane(bpp).
0
 

Author Comment

by:ExpExchHelp
Comment Utility
ikework:

thanks for chipping in...  I've changed it to:

// Allocate this many number of bytes to the image.
unsigned char *imaged;
imaged = (unsigned char *) malloc(totalPixels);


    // Interpolation ForLoop
      for (i=0; i<numberOfRows; i++){
            for (j=0; j<numberOfColumns; j++){
                  for (p=0; p<Fm; p++){
                        for (q=0; q<Fn; q++){
                        
                              //imaged[i*Fm+p j*Fn+q] = (int) image[i,j];
                              //imaged[i*Fm+p j*Fn+q] = (int) image[i][j];
                              imaged[i*Fm+p][j*Fn+q] = (int) image[i][j];
                        }
                  }
            }
      }

Remaing errors:
error C2108: subscript is not of integral type
error C2109: subscript requires array or pointer type
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
it should be:

 imaged[(int)(i*Fm+p)][(int)(j*Fn+q)] = (int) image[i][j];
                       
but image are defined as unidimensional arrays, so you have to make extra calculations
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
imaged[(int)(i*Fm+p) * (int)(imageWidth*Fm) * bands + (int)(j*Fn+q)] = (int) image[i*imageWidth*bands+j];

this will work for 1 band, so you will need an extra loop

for (int b=0; b<bands; b++)
 imaged[(int)(i*Fm+p) * (int)(imageWidth*Fm) * bands + (int)(j*Fn+q) + b] = (int) image[i*imageWidth*bands+j+b];
0
 

Author Comment

by:ExpExchHelp
Comment Utility
jaime,

I'll try it later on... 'need to go to a meeting now.

If required, I'll post additional questions later on.

EEH
0
 

Author Comment

by:ExpExchHelp
Comment Utility
jaime,

based on previous work, I've been using variables such as numberOfRows and numberOfColumns.

My code now compiles/builds fine, but crashes during execution.   I believe the interpolation arithmetic is correct (at least I hope so).

I think the crashing is caused not not allowing enouogh memory for the arrays.

Do you have any final pointers as to how I need to change that (if that indeed is the underlying cause)?

My latest CPP is posted at:
https://filedb.experts-exchange.com/incoming/ee-stuff/5852-Interpolation.zip

Thanks,
EEH
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
>>I think the crashing is caused not not allowing enouogh memory for the arrays.
I dont't think so, an image usually occupies just some megabytes.
Better you can send the interpolation function here
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
you have to take care when determining new image size:

     // original size calculation
    totalPixels = numberOfRows*numberOfColumns*numberOfBands;
    image = (unsigned char *) malloc(totalpixels);

     // new image allocate
    imaged = (unsigned char *) malloc(totalPixels*Fm*Fn);

the last line should be replace with:
    totalPixels = (int)(numberOfRows*Fm)*(int)(numberOfColumns*Fn)*numberOfBands;
    image = (unsigned char *) malloc(totalpixels);

Then, resuming my previous posts, the main algorithm should be:
      for (i=0; i<numberOfRows; i++){
            for (j=0; j<numberOfColumns; j++){
                  for (p=0; p<Fm; p++){
                        for (q=0; q<Fn; q++){
                              for (int b=0; b<bands; b++)
                                   imaged[(int)(i*Fm+p) + (int)(j*Fn+q)* (int)(imageWidth*Fm) * bands + b] = (int) image[i+j*imageWidth*bands+b];
                        }
                  }
            }
      }



0
 

Author Comment

by:ExpExchHelp
Comment Utility
jaime_olivares:,

thank you for posting the code.   I believe I implemented all of your suggestions; however, the program still crashes during execution.

Could you pls on (on EE) your version of the CPP?    

Thank you,
EEH
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
there are some variables that should have a different name to match your code:


     // original size calculation

    totalPixels = numberOfRows*numberOfColumns*numberOfBands;

    image = (unsigned char *) malloc(totalpixels);

    // new image

    totalPixels = (int)(numberOfRows*Fm)*(int)(numberOfColumns*Fn)*numberOfBands;

    imaged = (unsigned char *) malloc(totalpixels);   // <------ Error: was image, not imaged
 

      for (i=0; i<numberOfRows; i++){

            for (j=0; j<numberOfColumns; j++){

                  for (p=0; p<Fm; p++){

                        for (q=0; q<Fn; q++){

                              for (int b=0; b<bands; b++)

                                   imaged[(int)(i*Fm+p) + (int)(j*Fn+q)* (int)(numberOfColumns*Fm) * bands + b] = (int) image[i+j*numberOfColumns*bands+b];

                        }

                  }

            }

      }

Open in new window

0
 

Author Comment

by:ExpExchHelp
Comment Utility
jaime_olivares:,

thanks again -- the program now executes w/o errors.   However, when opening the new image in GIMP (PGM, PPM viewer), I get and "Premature end of file" error.   Also, new image size is only 4kb vs. the original image size.

Any ideas what might be causing this?

EEH
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
ok, post again your code, plus the image file. and try to remove all these blank lines!!!
0
 

Author Comment

by:ExpExchHelp
Comment Utility
I'll post my code momentarily.

Meanwhile, I had some partial success.   I believe I had seen this "premature end of file" error before in my output section.

Sure enough, when changing "image" to "imaged" in "fwrite(imaged, 1, totalPixels,fpOut);   the program generates a new image with the proper zooming features.

However, when trying this w/ another PGM file, it blew up.   Obviously, this should work w/ any PGM file.

I'll post the following within 5 minutes:
- CPP
- randpoly.pgm (this works fine)
- helo2.pgm (which does NOT work fine)

EEH
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
also send the factors you have used
0
 

Author Comment

by:ExpExchHelp
Comment Utility
jaimie,

the ZIP file with CPP and two PGM files are located at:
https://filedb.experts-exchange.com/incoming/ee-stuff/5863-interpolation.zip

I entered "2, 2" and "3, 3" for the values.   Again, doubling and tripling the size works fine for randpoly.pgm.   It does work at all for helo2.pgm.

Thousand thanks in advance,
EEH
0
 

Author Comment

by:ExpExchHelp
Comment Utility
... I mean to say... "does NOT work at all for helo2.pgm"... ;)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
ok, that will take a while, I am focused on some hard code right now.
0
 

Author Comment

by:ExpExchHelp
Comment Utility
and... OOPS... during test, I temporarily took out the "bands"... they obviously need to go back in.

Sorry for that.

EEH
0
 

Author Comment

by:ExpExchHelp
Comment Utility
jaimie,

I got some feedback from my instructor.  He indicates the following:

*****************
Your index arithmetic in the following statement is not correct:

imaged[(int)(i*Fm+p) + (int)(j*Fn+q)* (int)(numberOfColumns*Fm) * bands + b] = (int) image[i+j*numberOfColumns*bands+b]

For example, when i = 0, b = 0,  (i+j*numberOfColumns*bands+b) evaluates to 0 (j =0), bands*numberOfColumns (j = 1) and so on.  It should evaluate to 0, 3, 6, and so on.

*****************

Do you have any pointers as to how it should be adjusted?

EEH
0
 

Author Closing Comment

by:ExpExchHelp
Comment Utility
Thanks for your help, Jaimie.   This thread is getting very long though.  I'll open up a new one.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now