merge two memory (char*) buffers to one

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhav001
    New Member
    • Dec 2009
    • 4

    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(imagel eft.width * image1.length); // for left image
    char * buf2 = (char *)malloc(imager ight.width * image2.length); // for right image

    // for final image
    char * buf3 = (char *)malloc(imagel eft.width + imageright.widt h * imageleft.lengt h+imageright.le ngth);

    buf1 = getImage("image left");// left image
    buf1 = getImage("image right");// 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=im ageleft.half;

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

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

    madhavan
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    malloc(imagelef t.width + imageright.widt h * imageleft.lengt h+imageright.le ngth);

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

    imageleft.width + imageright.widt h * imageleft.lengt h + imageright.leng th

    is interpreted as

    imageleft.width + (imageright.wid th * imageleft.lengt h) + imageright.leng th

    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.wid th );
    memcpy(buf3+ y * imageleft.width + imageright.widt h , buf2, y*imageleft.wid th + imageright.widt h );

    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.

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      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.

      Comment

      • madhav001
        New Member
        • Dec 2009
        • 4

        #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.wid th );
        memcpy(buf3+ y * imageleft.width + imageright.widt h , buf2, y*imageleft.wid th + imageright.widt h );

        Is it in right form or not?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          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.

          Comment

          • madhav001
            New Member
            • Dec 2009
            • 4

            #6
            Hi Banfa,

            Thanks for your reply. While considering your suggesions, I am working on it as I am a c++ biginner.

            Comment

            Working...