<p>What you're trying to do is <em>online 2D bin packing</em>. It's online because you don't have all your small pictures in hand before you attempt to pack them into the big picture. Furthermore some small pictures will be "deallocated" and their space will be freed up. On the other hand, an offline algorithm allows you to do things like sort your small pictures from largest to smallest before packing them.</p>

<p>Here's an article that surveys the state of the art in 2D packing: <em><a href="http://www.csc.liv.ac.uk/~epa/surveyhtml.html" rel="nofollow noreferrer">Survey on two-dimensional packing</a></em>. It's quite theoretical.</p>

<p>This article <em><a href="http://arxiv.org/abs/0906.0409" rel="nofollow noreferrer">A New Upper Bound on 2D Online Bin Packing</a></em> cites other articles that describe online 2D packing algorithms.</p>

<p>People in the gaming world have a similar problem as you do; they call it <em>texture packing</em> or <em><a href="http://en.wikipedia.org/wiki/Texture_atlas" rel="nofollow noreferrer">texture atlas</a></em>. However, they use offline algorithms.</p>

<p>John Ratcliff posted a <a href="http://codesuppository.blogspot.com/2009/04/texture-packing-code-snippet-to-compute.html" rel="nofollow noreferrer">blog article</a> on texture packing.</p>

<p>See also this related question on gamedev: <a href="https://gamedev.stackexchange.com/questions/2829/texture-packing-algorithm">https://gamedev.stackexchange.com/questions/2829/texture-packing-algorithm</a></p>
Partitioning big rectangle to small ones (2D Packing)
<p>I need algorithm which splits big static sized rectangle to small ones. A perfect implementation for me look like this:</p>

<pre><code>struct RECT
{
  int l,t,r,b;
};

class BigRect
{
public:
  // width and height of big rect
  BigRect( unsigned width, unsigned height );

  // returns -1 if rect cannot be allocated, otherwise returns id of found rect
  int GetRect( unsigned width, unsigned height, RECT &amp;out );

  // returns allocated rect to big rectangle
  void FreeRect( int id );
};

void test()
{
  BigRect r( 10, 10 );

  RECT out;
  r.GetRect( 4, 4, out ); // rect found ({0,0,4,4} for example), returns 1
  r.GetRect( 5, 5, out ); // rect found ({4,0,9,5} for example), returns 2

  r.GetRect( 6, 6, out ); // no place found for rect, returns -1
  r.FreeRect( 2 );        // add {4,0,9,5} back to rect

  r.GetRect( 6, 6, out ); // rect found (4,0,10,6)
}
</code></pre>

<p>So I need algorithm for <code>GetRect</code> and <code>FreeRect</code> methods. Any ideas and links would be appreciated.</p>