By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

merge two memory (char*) buffers to one

P: 4
Hi,
I am biginner in C++ and I tried to combine two memory buffers of TIFF image data to one so that to save it as one image. ie: left half image + right half image = a full image. That was not successfull. The code part is:

char * buf1 = (char *)malloc(imageleft.width * image1.length); // for left image
char * buf2 = (char *)malloc(imageright.width * image2.length); // for right image

// for final image
char * buf3 = (char *)malloc(imageleft.width + imageright.width * imageleft.length+imageright.length);

buf1 = getImage("imageleft");// left image
buf1 = getImage("imageright");// right image

// now need to merge the two buffers on widthwise
// ie: buf1 (left half image) + buf2 (right half image) = buf3 (final image)

//image_height is same for all images

int image_height=imageleft.half;

for (int y = 0; y < image_height; y++) {
memcpy(buf3+ y * imageleft.width , buf1, y*imageleft.width );
memcpy(buf3+ y * imageleft.width+imageright.width , buf2, y*imageleft.width+imageright.width );
}

//.... Now this gives unexpected result. What is wrong with this pls?

madhavan
Dec 17 '09 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
malloc(imageleft.width + imageright.width * imageleft.length+imageright.length);

This is almost certainly not allocating the correct amount of data for you

imageleft.width + imageright.width * imageleft.length + imageright.length

is interpreted as

imageleft.width + (imageright.width * imageleft.length) + imageright.length

because the operator precedence of * is higher than +.

Anyway if you have 2 halves of an image, A and B such that A is AWidth x AHeight and B is BWidth x BHeight and AHeight == BHeight what site side by side in the final image then the final image is (AWidth + BWidth) x AHeight in size.

Which brings us onto

memcpy(buf3+ y * imageleft.width , buf1, y*imageleft.width );
memcpy(buf3+ y * imageleft.width + imageright.width , buf2, y*imageleft.width + imageright.width );

Neight of these memcpy calls is correct. For staters you always copy from the same place in the source buffer. Secondly although each copy should be copying a row from the source buffer and a row in an image has constant size the number of bytes copied is a function of y and therefore increasing.
Dec 17 '09 #2

RRick
Expert 100+
P: 463
Another assumption you are making is that the color is exactly 1 byte long. This might be true for greyscale, but not for color.

I believe Tiff uses RGB triplets for each color pixel which means you are dealing with 3 bytes, not 1.
Dec 18 '09 #3

P: 4
Hi Banfa thanks your reponse to my query.
I admit the points what you have mentioned here. Exactly the source buffer is used here to cobine to a third one. Also the row in an image has constant size/ the number of bytes copied as the y increment. But again I doubt the

memcpy(buf3+ y * imageleft.width , buf1, y*imageleft.width );
memcpy(buf3+ y * imageleft.width + imageright.width , buf2, y*imageleft.width + imageright.width );

Is it in right form or not?
Dec 19 '09 #4

Banfa
Expert Mod 5K+
P: 8,916
Those memcpy calls definitely use the wrong pointers and buffer sizes.

Perhaps you should try writing down in English (or your native language on a piece of paper) what you want to copy, from where to where and how much.

Once you have the linguistic descriptions of what you want to do then you can try transferring them into algorithms in code.

In both memcpys

The first parameter is wrong because it fails to properly account for the length of a buf3 row.

The second parameter is wrong because it is constant.

The third parameter is wrong because it is not constant.
Dec 19 '09 #5

P: 4
Hi Banfa,

Thanks for your reply. While considering your suggesions, I am working on it as I am a c++ biginner.
Dec 30 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.