Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# C++ Function

Posted on 2007-11-26
Medium Priority
1,149 Views
I'm trying to convert an "interpolation" function into C++ code.   Unfortunately, I'm not really sure how to do this.

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

Could anyone pls provide me some pointers as to what's the best approach?

Thanks,
EEH
0
Question by:ExpExchHelp
• 7
• 4

LVL 55

Expert Comment

ID: 20355616
how do you store the data? do you have an array? some function to fill in?
0

Author Comment

ID: 20355654
jaime_olivares:

so far, I only have the input/output of reading in the image.   I've posted the file at:
https://filedb.experts-exchange.com/incoming/ee-stuff/5834-Interpolation.zip

In the body, I've marked where the function should go.   Any help would be greatly appreciated.

Thanks,
EEH
0

Author Comment

ID: 20355689
Also, beyond the sample C++ posted above, the following JPG may provide more insights (I hope so).

http://img263.imageshack.us/img263/7990/interpolation2tp1.jpg
0

LVL 55

Accepted Solution

Jaime Olivares earned 2000 total points
ID: 20355738
Ok, I have post your code below.
I see where you create the image:
// Allocate this many number of bytes to the image.
totalPixels = numberOfRows*numberOfColumns*numberOfBands;

// Reads from the specified pointer; 1 byte, # of pixels, and file pointer
image = (unsigned char *) malloc(totalPixels);

and where you ask for the factors:
// User must enter interpolation factor Fm.
cout << "\n\nEnter interpolation factor Fm.  The value must be an integer:  ";
cin >> Fm;
// User must enter interpolation factor Fn.
cout << "\n\nEnter interpolation factor Fn.  The value must be an integer:  ";
cin >> Fn;

So, you need to reserve memory for the new image:
// Allocate this many number of bytes to the image.
int newTotalPixels = (int)(numberOfRows*fm)*(int)(numberOfColumns*fn)*numberOfBands;
unsigned char *newImage = (unsigned char *) malloc(newTotalPixels);

and  create a function like:
void Interpolate(unsigned char *image, int width, int height, int bands, double fm, double fn, char *newImage);

So, inside this function, scan for all fixels in 'image' in the x and y axis with a nested for() loop.
for each pixel in the original image, save a pixel in the new image, as explained in the formula.
I don't see the utility of p and q, assume to be 0.

``````// Standard libraries
#include <stdio.h>                          // This header file defines the I/O functions
#include <iostream>                         // This header file defines the IO-stream functions
#include <stdlib.h>                         // This header file includes standard definitions like exit()
#include <math.h>                           // This header file includes math functions
#include <conio.h>                          // This header file include console input/output

// These #define statements allow to convert numerical values into string values.
#define FALSE 0
#define TRUE 1
#define DBL_SIZE sizeof(double)
#define CMPLX_SIZE 16

// argc = counter; argv = array of strings.
int main(int argc, char **argv){

// Initialize variables:
// Existing variables (previous assignment)

FILE *fpIn;
FILE *fpOut;
FILE *fresults;

char *string;
char c;

int highVal;
int numberOfRows, numberOfColumns, numberOfBands;
int totalPixels;
int maximum=-1;
int i, j, l;

// New variables (midterm)
int halfColumns;
int bnd;
int iOff, ipOff;

double invMrowMcol;
double iv;
double *imaged;
double *filter;
double v, w;
double *shiftedFilter;

// Declares a pointer of this type.  The data will be stored in "*image".
unsigned char *image;

// Program requires at least 3 arguments:
// 1. "call program"
// 2. "input pgm"
// 3. "output pgm"; the modified image will be written to this outfile file

if (argc < 3) {
exit(0);
}
fpIn = fopen(argv[1],"rb");				// argv[1] contains the filename
if (fpIn == NULL) {
fprintf(stderr,"%s either cannot be read or does not exist\n", argv[1]);
exit(-1);
}
string = (char *) calloc(256,1);		// initialized dynamic memory allocation
while (!doneReading && (c = (char) fgetc(fpIn)) != '\0')
{
switch(c) {
case 'P':
c = (char) getc(fpIn);
switch(c) {
case '2':
numberOfBands = 1;
// ppmType = PPMASCII;
break;
case '3':
numberOfBands = 3;
// ppmType = PPMASCII;
break;
case '5':
numberOfBands = 1;
// ppmType = PPMGRAY;
break;
case '6':
numberOfBands = 3;
// ppmType = PPMRAW;
break;
}

c = (char) getc(fpIn);
if (c != 0x0A) {
ungetc(c,fpIn);
}
else {
ungetc(c,fpIn);
fgets(string,256,fpIn);
}
break;

case '#':
fgets(string,256,fpIn);
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
ungetc(c,fpIn);
fscanf(fpIn,"%d %d %d", &numberOfColumns, &numberOfRows, &highVal);
fgetc(fpIn);
break;

}    // End Switch
}      // End While

// De-allocate memory
free(string);

// Passes the supplied output-filename (2nd argument) into the feedback (command window)
char* arg2 = argv[2];

// Allocate this many number of bytes to the image.
totalPixels = numberOfRows*numberOfColumns*numberOfBands;

// Reads from the specified pointer; 1 byte, # of pixels, and file pointer
image = (unsigned char *) malloc(totalPixels);

// ******** Interpolation (begin) ********

// Define variables
using namespace std;
double Fm;
double Fn;

// User must enter interpolation factor Fm.
cout << "\n\nEnter interpolation factor Fm.  The value must be an integer:  ";
cin >> Fm;

// User must enter interpolation factor Fn.
cout << "\n\nEnter interpolation factor Fn.  The value must be an integer:  ";
cin >> Fn;

// Interpolation function should go here...

// ******** Interpolation (end) ********

// ******** Image Output Section ********

//Generate the midterm1.pgm (modified) image
fpOut = fopen(argv[2],"wb");

fprintf(fpOut,"P%d\n%d %d\n255\n",numberOfBands>1?6:5,numberOfColumns,numberOfRows);
fwrite(image,1,totalPixels,fpOut);

// Command Window Feedback
fprintf(stdout,"\n\n\n\n\nAn output image has been created!\n\nThe filename is: %s\n\n\n\n\n", argv[2]);

// Exit routine
exit(0);
}
``````
0

Author Comment

ID: 20357222
jaime_olivares:

thank you so much for your kind help.   I truly appreciate it.

Ok, previously, I've used the ForLoop shown below (contains old values such as zeta... pls disregard them).

My questions:
- can this approach be used?
- do I need to change all "double" to "int" for e.g. numberOfRows?
- beginning with the line "rho = sqrt((double(v*v+w*w)));", is this where the new function would go?
- do you have any additional pointers as to how I show write the terms for the function?

EEH

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

//Changed to (replaced double with int):
for (i = 0; i < numberOfRows; i++) {
iOff = i*numberOfColumns;
v = (i - numberOfRows/2)/(double)numberOfRows;
for (j = 0; j < numberOfColumns; j++) {
w = (j - numberOfColumns/2)/(double)numberOfColumns;

rho = sqrt((double(v*v+w*w)));
double rho_quotient = rho / rho_c;
double term1 = exp(-pow(rho_quotient, 2.0));
double term2 = zeta * exp(-pow(1.6 * rho_quotient, 2.0));
filter[iOff+j] = term1 - term2;
}
}
0

LVL 55

Expert Comment

ID: 20358813
I have no idea about the equations you are using in your last post. Them are totally different from your original question.
0

Author Comment

ID: 20358935
jaime,

yes... I understand... they were just an example.    I've given this some more thought and I believe that I need 4 ForLoops.

Does the following seem to be a reasonable approach?

for (p = 0; p < numberOfBands; bnd++) {
for (q = 0; q < numberOfBands; bnd++) {
for (i = 0; i < numberOfRows; i++) {
imaged = image -1 ;

for (j = 0; j < numberOfRows; j++) {
imaged = image -1 ;
}
}
}
}

0

Author Comment

ID: 20359135
Jaime,

given above ForLoop, I'm not sure if I properly reflect the start/end value in the loop.

Therefore, should the ForLoop look like this?

for (p = 0; p < image -1; p++) {
for (q = 0; q < image -1; q++) {
for (i = 0; i < image -1; i++) {
imaged = image -1;

for (j = 0; j < image -1; j++) {
imaged = image -1;
}
}
}
}
0

LVL 55

Expert Comment

ID: 20359146
this looks inconsistent:
for (p = 0; p < numberOfBands; bnd++) {
for (q = 0; q < numberOfBands; bnd++) {
you count p and q, but increment bnd, this will be infinite

for (i = 0; i < numberOfRows; i++) {
imaged = image -1 ;
appears to be is not a realistic example
0

Author Comment

ID: 20359256
Any pointers as to where I should go from here?

EEH
0

Author Comment

ID: 20359557
jaimie,

I think I have new information.   I'll open up a new thread.   Thanks for the help earlier.

New question has been posted under:
http://www.experts-exchange.com/Programming/Languages/CPP/Q_22985448.html

Again, thanks!

EEH
0

## Featured Post

Question has a verified solution.

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

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 â€¦
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 the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anâ€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
###### Suggested Courses
Course of the Month10 days, 20 hours left to enroll