Memory management

In the Olena library, all image types behave like image2d:

Exemple with image2d

Images do not actually store the data in the class. Images store a pointer to an allocated space which can be shared with other objects. Once an image is assigned to another one, the two images share the same data so they have the same ID and point to the same memory space. Therefore, assigning an image to another one is NOT a costly operation. The new variable behaves like some mathematical variable. Put differently it is just a name to designate an image:

  image2d<int> ima1(box2d(2, 3));
  image2d<int> ima2;
  point2d p(1,2);

  ima2 = ima1; // ima1.id() == ima2.id()
  // and both point to the same memory area.

  ima2(p) = 2; // ima1 is modified as well.

  // prints "2 - 2"
  std::cout << ima2(p) << " - " << ima1(p) << std::endl;
  // prints "true"
  std::cout << (ima2.id_() == ima1.id_()) << std::endl;

If a deep copy of the image is needed, a duplicate() routine is available:

  image2d<int> ima1(5, 5);
  image2d<int> ima3 = duplicate(ima1); // Makes a deep copy.

  point2d p(2, 2);
  ima3(p) = 3;

  std::cout << ima3(p) << " - " << ima1(p) << std::endl;
  std::cout << (ima3.id_() == ima1.id_()) << std::endl;
Output:
3 - 0
0