pointers and ofImage

I’ve been having a bit of trouble and think I can trace it to a misuse of pointers.

I want to copy the data from one pointer (call it pointer1) to another pointer (pointer2) and then adjust the data referenced by pointer2 without adjusting the data referenced by pointer1.

More specifically…
I am assigning two pointers to arrays:

pointer1 = new unsigned char [imageWidth * imageHeight * 3];  
pointer2 = new unsigned char [imageWidth * imageHeight * 3];  

Then I am referencing image data to pointer1:

pointer1 = image.getPixels();  

I then want to assign pointer2’s reference with the data from pointer1. Then I want to alter that data and use pointer2 to display the new image. (the following just shows me altering pointer2’s referenced variables)

    for (int y = 0; y < scnHeight; y++){  
        for (int x = 0; x < scnWidth; x++){  
            //set RGB pixel data count  
            int pixR = (y * scnWidth * 3) + (x*3);  
            int pixG = (y * scnWidth * 3) + (x*3) + 1;  
            int pixB = (y * scnWidth * 3) + (x*3) + 2;  
            tempR = pointer2[pixR] + 10;  
                if(tempR > 255){tempR = 255;}  
                if(tempR < 0){tempR = 0;}  
                pointer2[pixR] = tempR;  
            tempG = pointer2[pixG] + 10;  
                if(tempG > 255){tempG = 255;}  
                if(tempG < 0){tempG = 0;}  
                pointer2[pixG] = tempG;  
            tempB = pointer2[pixB] + 10;  
                if(tempB > 255){tempB = 255;}  
                if(tempB < 0){tempB = 0;}  
                pointer2[pixB] = 10;  
    newImage.loadData(pointer2, imageWidth, imageHeight, GL_RGB);  
    newImage.draw(0, 0, imageWidth, imageHeight,);  

and lastly I want to reset pointer2 to reference the original image data. Any ideas?

From the code you’ve posted, it looks like what you want to do is make a copy of the data and then process that?

So the first thing I can see is that you probably don’t need two pointers. The function ofImage.getPixels() actually returns a pointer to the image data, so you can copy directly from that. Remember, pointers point to a memory location and that’s it.

Second, in order to make a copy of the data, you can’t just write pointer1 = image.getPixels(), because all that does is make pointer1 point to the same memory location as the one returned by ofImage, and you don’t want to modify the image’s data, you want to modify an copy of that data. What you need to do is allocate memory for a new copy of the data, just like you have done, but then you need to either explicitly copy each memory cell:

for(i=0;i<totalPixels;i++) {  
  pointer1[i] = image.getPixels()[i];  

or, the easier and faster way to do it is use memcpy() which will copy an entire block of memory to a new location:

pointer1=new unsigned char[totalPixels];  
memcpy((void*)pointer1,(void*)(image.getPixels()),sizeof(unsigned char)*totalPixels);  

The first parameter is the memory location (pointer) to copy the data to, the second is where to copy hte data from, and the third is the number of bytes of data to copy. The sizeof() function will tell you how many bytes a particular data type has. The only confusing part about memcpy is that that it wants paramaters 1 and 2 to be of type void*, which basically means a generic pointer that has no type, or is type void; so putting (void*) in front of each parameter will force the compiler to think of those parameters as void * instead of unsigned char *.

There you have it, a mini pointers lesson. The other word of caution I would say is to only allocate your memory once, in the setup function, or else you’ll get memory leaks.

Hope this helps!