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
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.
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.
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?
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.
Hi Banfa,
Thanks for your reply. While considering your suggesions, I am working on it as I am a c++ biginner.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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)...
|
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 ...
|
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...
|
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...
|
by: rkk |
last post by:
Hi,
I have written a generic mergesort program which is as below:
---------------------------------------------------------
mergesort.h
-----------------------
void
MergeSort(void...
|
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....
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |