473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

merge two memory (char*) buffers to one

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
5 9743
Banfa
9,065 Expert Mod 8TB
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
463 Expert 256MB
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
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
9,065 Expert Mod 8TB
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
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

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

Similar topics

20
by: iouswuoibev | last post by:
When writing a function that manipulates data (for example, a string), is it in principle a good or bad idea to allocate/deallocate memory within that function? For example, I have the following...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
1
by: Rick Gigger | last post by:
I want to know how much memory I've got free on my system. The free command gives me something like this: total used free shared buffers cached Mem: 2064832 ...
3
by: wakun | last post by:
Hi there, I am seeking a fastest way to load a BIG string and parse it as a given format. I have a extern function which return a (char *)string in BIG size. Now, I am going to parse it with a...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
9
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void...
13
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure....
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.