• Status: Solved
• Priority: Medium
• Security: Public
• Views: 354

# C++ Function (Arithmetic in ForLoop)

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
ExpExchHelp
• 12
• 9
1 Solution

Software ArchitectCommented:
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 Commented:
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

Commented:
> 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 Commented:
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

Software ArchitectCommented:
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

Software ArchitectCommented:
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 Commented:
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 Commented:
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

Software ArchitectCommented:
>>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

Software ArchitectCommented:
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 Commented:
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

Software ArchitectCommented:
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];
}
}
}
}
``````
0

Author Commented:
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

Software ArchitectCommented:
ok, post again your code, plus the image file. and try to remove all these blank lines!!!
0

Author Commented:
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

Software ArchitectCommented:
also send the factors you have used
0

Author Commented:
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.

EEH
0

Author Commented:
... I mean to say... "does NOT work at all for helo2.pgm"... ;)
0

Software ArchitectCommented:
ok, that will take a while, I am focused on some hard code right now.
0

Author Commented:
and... OOPS... during test, I temporarily took out the "bands"... they obviously need to go back in.

Sorry for that.

EEH
0

Author Commented:
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 Commented:
Thanks for your help, Jaimie.   This thread is getting very long though.  I'll open up a new one.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.