combing two images together

Hi guys, I need assistance in this. I am supposed to make a function:combineImage(const Image & im, int I, int j): that combines two images together, The position of the image to insert is specified such that the upper left pixel of im is located on row i and column j. Note that i and j could be outside of the image we are combing with. Use a blending operation where the combined images overlap. A blending operation averages the overlapping pixel values. Note that each color for each pixel must be averaged separately. Because the combined image must be rectangular we may have empty portions in the final image. The pixels in the empty portions should be black. For example suppose we have an image of size 200 x 300 pixels and we combine this with an image of size 200 x 100 at location i=10 and j=1. The size of the combined image will be 210 x 300 pixels and the combined image will contain empty black regions.


Here is my code if it helps:( I do really appreciate it if u managed to help!)




// Functions for reading and writing a binary ppm image file
// The image is stored in an array of pixels using integers for the colors

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct pixel {
int red;
int green;
int blue;
};

class Picture {
private:
pixel *image;
int width, height;

public:
Picture(int h, int w);
Picture( void );
~Picture( void );
void save(string filename);
void load(string filename);
void set(int row, int col, pixel color);
pixel get(int row, int col);
int Width();
int Height();
pixel* pixels();
void flipimage();
void adjustcolors(pixel color);
void reducesize(void);
};


Picture::Picture( void ) {

width = 0;
height = 0;
image = NULL; }

Picture::Picture(int h, int w) {
width = w; height = h;
image = new pixel[height*width];
for (int i=0; i<height; i++)
for (int j=0; j<width; j++) {
image[i*width+j].red = 0;
image[i*width+j].green = 0;
image[i*width+j].blue = 0; }}

Picture::~Picture(void)
{
delete image;
}


void Picture::set(int row, int col, pixel color) {
image[row*width+col] = color; }

pixel Picture::get(int row, int col) {
return image[row*width+col]; }

int Picture::Height(void) {
return height; }

int Picture::Width(void) {
return width; }
pixel* Picture::pixels(void)
{
return image;}



void Picture::load(string imageName){
// read an image from a .ppm file
// returns the image in an array of pixels
// returns the size of the image in width and height

ifstream imageFile;
string s;
char *temp;
int numLevels;

// ios::binary because it is a data file
imageFile.open(imageName.c_str(), ios::binary);

// read in the ppm header
imageFile >> s;
if (s != "P6") {
cout << "Not a valid PPM image" << endl;
}

imageFile >> width >> height >> numLevels;
imageFile.get();

temp = new char[width*height*3];

// read in the image data to temp
imageFile.read(temp,width*height*3);

// copy temp to an array of pixels
image = new pixel[width*height];
for (int i=0; i<width*height; i++) {
unsigned char red = temp[i*3];
unsigned char green = temp[i*3+1];
unsigned char blue = temp[i*3+2];
image[i].red = red;
image[i].green = green;
image[i].blue = blue;
}

delete temp; // return the memory
imageFile.close();
}


void Picture::save(string imageName) {
// store image (array of pixels) in a .ppm file
// widht and height are the size of the input image
ofstream imageFile;
// ios::binary because it is a data file
imageFile.open(imageName.c_str(), ios::binary);

// write the ppm header
imageFile << "P6" << endl << width << endl << height
<< endl << 255 << endl;
char* temp = new char[width*height*3];
// copy the array of pixels in image to temp
for (int i=0; i<width*height; i++) {
char red = image[i].red;
char green = image[i].green;
char blue = image[i].blue;
temp[i*3] = red;
temp[i*3+1] = green;
temp[i*3+2] = blue;
}
// write temp to the file
imageFile.write(temp,width*height*3);

delete temp; // return the memory

imageFile.close();
}


void main (void)
{

string s;
Picture pic1;
pixel color;
cout << "Input image: " ;
cin >> s;
pic1.load(s);


cout << "Output image: " ;
cin >> s;
pic1.save(s);
}


//Then I am supposed to ask the user to input another image to combine it with the existing image
// I really do appreciate it if u managed to help me :)
Last edited on
anybody please I know it's a bit confusing but plz try to help
Thanks.
one suggestion:

dont post duplicate topics and give some time to the members here.. someone will help surely.
ok thank u, I was just worried cus i really nead this done
sorry
dont worry.. members here are very active and love to answer questions as soon they see one.


I have no idea regarding ppm images actually.. :(
Thanks anyways, but if u have any knowledge abotu any kinds of images it will work,, but thanks anyways
Topic archived. No new replies allowed.