stb_rect_pack.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // stb_rect_pack.h - v0.08 - public domain - rectangle packing
  2. // Sean Barrett 2014
  3. //
  4. // Useful for e.g. packing rectangular textures into an atlas.
  5. // Does not do rotation.
  6. //
  7. // Not necessarily the awesomest packing method, but better than
  8. // the totally naive one in stb_truetype (which is primarily what
  9. // this is meant to replace).
  10. //
  11. // Has only had a few tests run, may have issues.
  12. //
  13. // More docs to come.
  14. //
  15. // No memory allocations; uses qsort() and assert() from stdlib.
  16. // Can override those by defining STBRP_SORT and STBRP_ASSERT.
  17. //
  18. // This library currently uses the Skyline Bottom-Left algorithm.
  19. //
  20. // Please note: better rectangle packers are welcome! Please
  21. // implement them to the same API, but with a different init
  22. // function.
  23. //
  24. // Credits
  25. //
  26. // Library
  27. // Sean Barrett
  28. // Minor features
  29. // Martins Mozeiko
  30. // Bugfixes / warning fixes
  31. // Jeremy Jaussaud
  32. //
  33. // Version history:
  34. //
  35. // 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0)
  36. // 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0)
  37. // 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort
  38. // 0.05: added STBRP_ASSERT to allow replacing assert
  39. // 0.04: fixed minor bug in STBRP_LARGE_RECTS support
  40. // 0.01: initial release
  41. //
  42. // LICENSE
  43. //
  44. // This software is dual-licensed to the public domain and under the following
  45. // license: you are granted a perpetual, irrevocable license to copy, modify,
  46. // publish, and distribute this file as you see fit.
  47. //////////////////////////////////////////////////////////////////////////////
  48. //
  49. // INCLUDE SECTION
  50. //
  51. #ifndef STB_INCLUDE_STB_RECT_PACK_H
  52. #define STB_INCLUDE_STB_RECT_PACK_H
  53. #define STB_RECT_PACK_VERSION 1
  54. #ifdef STBRP_STATIC
  55. #define STBRP_DEF static
  56. #else
  57. #define STBRP_DEF extern
  58. #endif
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. typedef struct stbrp_context stbrp_context;
  63. typedef struct stbrp_node stbrp_node;
  64. typedef struct stbrp_rect stbrp_rect;
  65. #ifdef STBRP_LARGE_RECTS
  66. typedef int stbrp_coord;
  67. #else
  68. typedef unsigned short stbrp_coord;
  69. #endif
  70. STBRP_DEF void stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
  71. // Assign packed locations to rectangles. The rectangles are of type
  72. // 'stbrp_rect' defined below, stored in the array 'rects', and there
  73. // are 'num_rects' many of them.
  74. //
  75. // Rectangles which are successfully packed have the 'was_packed' flag
  76. // set to a non-zero value and 'x' and 'y' store the minimum location
  77. // on each axis (i.e. bottom-left in cartesian coordinates, top-left
  78. // if you imagine y increasing downwards). Rectangles which do not fit
  79. // have the 'was_packed' flag set to 0.
  80. //
  81. // You should not try to access the 'rects' array from another thread
  82. // while this function is running, as the function temporarily reorders
  83. // the array while it executes.
  84. //
  85. // To pack into another rectangle, you need to call stbrp_init_target
  86. // again. To continue packing into the same rectangle, you can call
  87. // this function again. Calling this multiple times with multiple rect
  88. // arrays will probably produce worse packing results than calling it
  89. // a single time with the full rectangle array, but the option is
  90. // available.
  91. struct stbrp_rect
  92. {
  93. // reserved for your use:
  94. int id;
  95. // input:
  96. stbrp_coord w, h;
  97. // output:
  98. stbrp_coord x, y;
  99. int was_packed; // non-zero if valid packing
  100. }; // 16 bytes, nominally
  101. STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
  102. // Initialize a rectangle packer to:
  103. // pack a rectangle that is 'width' by 'height' in dimensions
  104. // using temporary storage provided by the array 'nodes', which is 'num_nodes' long
  105. //
  106. // You must call this function every time you start packing into a new target.
  107. //
  108. // There is no "shutdown" function. The 'nodes' memory must stay valid for
  109. // the following stbrp_pack_rects() call (or calls), but can be freed after
  110. // the call (or calls) finish.
  111. //
  112. // Note: to guarantee best results, either:
  113. // 1. make sure 'num_nodes' >= 'width'
  114. // or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'
  115. //
  116. // If you don't do either of the above things, widths will be quantized to multiples
  117. // of small integers to guarantee the algorithm doesn't run out of temporary storage.
  118. //
  119. // If you do #2, then the non-quantized algorithm will be used, but the algorithm
  120. // may run out of temporary storage and be unable to pack some rectangles.
  121. STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
  122. // Optionally call this function after init but before doing any packing to
  123. // change the handling of the out-of-temp-memory scenario, described above.
  124. // If you call init again, this will be reset to the default (false).
  125. STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
  126. // Optionally select which packing heuristic the library should use. Different
  127. // heuristics will produce better/worse results for different data sets.
  128. // If you call init again, this will be reset to the default.
  129. enum
  130. {
  131. STBRP_HEURISTIC_Skyline_default=0,
  132. STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
  133. STBRP_HEURISTIC_Skyline_BF_sortHeight,
  134. };
  135. //////////////////////////////////////////////////////////////////////////////
  136. //
  137. // the details of the following structures don't matter to you, but they must
  138. // be visible so you can handle the memory allocations for them
  139. struct stbrp_node
  140. {
  141. stbrp_coord x,y;
  142. stbrp_node *next;
  143. };
  144. struct stbrp_context
  145. {
  146. int width;
  147. int height;
  148. int align;
  149. int init_mode;
  150. int heuristic;
  151. int num_nodes;
  152. stbrp_node *active_head;
  153. stbrp_node *free_head;
  154. stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
  155. };
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif