Can I store another images pixel into a different image's pixel buffer?

edited February 2016 in Programming Questions

Say for example I did pic = loadImage("pic.jpg") and I did pic2 = createImage(width,height,RGB). Would I be able to store pixels of pic into pic2?
If I found the right indices could I just do pic2.pixels[location] = color(r , g ,b ), where r g and b are found from the first image like: r = red(pic.pixels[location])?


thank you

Answers

  • edited February 2016

    If 2 PImage objects got the same width & height dimensions, a simple arrayCopy() would suffice:
    arrayCopy(pic1.pixels, pic2.pixels);: https://Processing.org/reference/arrayCopy_.html

    An example using arrayCopy(): https://forum.Processing.org/two/discussion/14835/how-to-change-main-sketch-display#latest

    If they're different dimensions, you're gonna need copy() instead:
    https://Processing.org/reference/PImage_copy_.html

    An example using copy(): https://forum.Processing.org/two/discussion/comment/61996/#Comment_61996

  • Would arrayCopy() or copy be the only way to do it? I'm not sure if I am allowed to use it unless that is the only way. Would using a loop and doing as I described above work? I tried it, but it seems to not be working. I'm not sure if it is just not possible or I have a bug somewhere.

  • edited February 2016

    Both arrayCopy() & copy() are for full copy transfer.
    If you need to modify the transferred values, you're gonna need a regular loop.
    If you've got an attempt, why haven't you posted it already?
    Don't forget to format it for the forum though, please: https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

  • edited February 2016

    I was thinking:

    win = creatImage(w, h, RGB);
    for(int i = 0; i < w; i++){
        for(int j = 0; j < h; j++){
            int location = j * w + i;
            float r = red(img.pixels[i]);
            //same for g and b;
            win.pixels[location] = color(r,g,b);
        }
    }
    
  • edited February 2016

    Ir seems you're not altering the values. Therefore arrayCopy() or copy() are the best options.
    And don't forget to learn how to post formatted code in this forum:
    https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

  • I can't seem to get the tabs correct for the formatted code.

  • Just highlight posted code and hit CTRL+O.

  • edited February 2016

    if I have an image, if i want to loop through its pixels would it be like this:

    for(int i = 0; i < img.height; i++){
        for(int j = 0; j < img.width; j++)
        {
            img.pixels[ i + j * img.width] = something;
        }
    }
    
  • edited February 2016 Answer ✓

    Slight mistake in line 4

    for(int i = 0; i < img.height; i++){
        for(int j = 0; j < img.width; j++)
        {
            img.pixels[ i * img.width + j] = something;
        }
     }
    

    You can make it clearer by using different variable names

    for(int y = 0; y < img.height; y++){
        for(int x = 0; x < img.width; x++)
        {
            img.pixels[ y * img.width + x] = something;
        }
     }
    
  • Thanks quark! So the variable that is used to compare against the height will always be the one that gets multiplied to the width? Do you think you can explain the logic behind this to me. Thanks!

  • edited February 2016 Answer ✓

    Assume you have an image 8 pixels wide and 4 high. That is 32 pixels so the pixel array elements go from [0] to [31] and their position in the image is like this

     x =      0    1    2    3    4    5    6    7
    y =  ------------------------------------------
     0   |   0    1    2    3    4    5    6    7
     1   |   8    9   10   11   12   13   14   15
     2   |  16   17   18   19   20   21   22   23
     3   |  24   25   26   27   28   29   30   31
    

    Now if we take the image pixel at [x=3, y=2] the position in the pixel array is 19 which is calculated by 2 * 8 + 3 in other words

    y * image_width + x

  • Answer ✓

    BTW you can reverse the process.

    Given an index position in the pixel array idx you can calculate the pixel coordinates with

    y = idx / image_width;
    x = idx % image_width;
    

    where % is the modulo operator i.e. gives the remainder after a division.

  • edited February 2016 Answer ✓

    Just an addendum: For the / operator to work correctly, neither of its operands can be of any fractional type, such as float & double for example. :-SS

    That's especially true for JavaScript for example, given the fact its number type is fractional.
    Therefore, we need to truncate the result by using | 0: y = idx / image_width | 0;

Sign In or Register to comment.