Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# C++ Function (Arithmetic in ForLoop)

Posted on 2007-11-27
Medium Priority
347 Views
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
Question by:ExpExchHelp
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 12
• 9

LVL 55

Expert Comment

ID: 20360178
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

ID: 20360411
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

ID: 20360519
> 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

ID: 20360763
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

ID: 20360792
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

ID: 20360822
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

ID: 20361325
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

ID: 20362811
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

ID: 20362826
>>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

ID: 20363555
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

ID: 20366802
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

LVL 55

Accepted Solution

Jaime Olivares earned 2000 total points
ID: 20366843
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 Comment

ID: 20367028
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

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

Author Comment

ID: 20367118
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

ID: 20367136
also send the factors you have used
0

Author Comment

ID: 20367159
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

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

LVL 55

Expert Comment

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

Author Comment

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

Sorry for that.

EEH
0

Author Comment

ID: 20368308
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

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more adâ€¦
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more â€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month11 days, 22 hours left to enroll

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

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