2
0

stb_rect_pack.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // stb_rect_pack.h - v1.00 - 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. // github:IntellectualKitty
  31. //
  32. // Bugfixes / warning fixes
  33. // Jeremy Jaussaud
  34. // Fabian Giesen
  35. //
  36. // Version history:
  37. //
  38. // 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles
  39. // 0.99 (2019-02-07) warning fixes
  40. // 0.11 (2017-03-03) return packing success/fail result
  41. // 0.10 (2016-10-25) remove cast-away-const to avoid warnings
  42. // 0.09 (2016-08-27) fix compiler warnings
  43. // 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0)
  44. // 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0)
  45. // 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort
  46. // 0.05: added STBRP_ASSERT to allow replacing assert
  47. // 0.04: fixed minor bug in STBRP_LARGE_RECTS support
  48. // 0.01: initial release
  49. //
  50. // LICENSE
  51. //
  52. // See end of file for license information.
  53. //////////////////////////////////////////////////////////////////////////////
  54. //
  55. // INCLUDE SECTION
  56. //
  57. #ifndef STB_INCLUDE_STB_RECT_PACK_H
  58. #define STB_INCLUDE_STB_RECT_PACK_H
  59. #define STB_RECT_PACK_VERSION 1
  60. #ifdef STBRP_STATIC
  61. #define STBRP_DEF static
  62. #else
  63. #define STBRP_DEF extern
  64. #endif
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. typedef struct stbrp_context stbrp_context;
  69. typedef struct stbrp_node stbrp_node;
  70. typedef struct stbrp_rect stbrp_rect;
  71. #ifdef STBRP_LARGE_RECTS
  72. typedef int stbrp_coord;
  73. #else
  74. typedef unsigned short stbrp_coord;
  75. #endif
  76. STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
  77. // Assign packed locations to rectangles. The rectangles are of type
  78. // 'stbrp_rect' defined below, stored in the array 'rects', and there
  79. // are 'num_rects' many of them.
  80. //
  81. // Rectangles which are successfully packed have the 'was_packed' flag
  82. // set to a non-zero value and 'x' and 'y' store the minimum location
  83. // on each axis (i.e. bottom-left in cartesian coordinates, top-left
  84. // if you imagine y increasing downwards). Rectangles which do not fit
  85. // have the 'was_packed' flag set to 0.
  86. //
  87. // You should not try to access the 'rects' array from another thread
  88. // while this function is running, as the function temporarily reorders
  89. // the array while it executes.
  90. //
  91. // To pack into another rectangle, you need to call stbrp_init_target
  92. // again. To continue packing into the same rectangle, you can call
  93. // this function again. Calling this multiple times with multiple rect
  94. // arrays will probably produce worse packing results than calling it
  95. // a single time with the full rectangle array, but the option is
  96. // available.
  97. //
  98. // The function returns 1 if all of the rectangles were successfully
  99. // packed and 0 otherwise.
  100. struct stbrp_rect
  101. {
  102. // reserved for your use:
  103. int id;
  104. // input:
  105. stbrp_coord w, h;
  106. // output:
  107. stbrp_coord x, y;
  108. int was_packed; // non-zero if valid packing
  109. }; // 16 bytes, nominally
  110. STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
  111. // Initialize a rectangle packer to:
  112. // pack a rectangle that is 'width' by 'height' in dimensions
  113. // using temporary storage provided by the array 'nodes', which is 'num_nodes' long
  114. //
  115. // You must call this function every time you start packing into a new target.
  116. //
  117. // There is no "shutdown" function. The 'nodes' memory must stay valid for
  118. // the following stbrp_pack_rects() call (or calls), but can be freed after
  119. // the call (or calls) finish.
  120. //
  121. // Note: to guarantee best results, either:
  122. // 1. make sure 'num_nodes' >= 'width'
  123. // or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'
  124. //
  125. // If you don't do either of the above things, widths will be quantized to multiples
  126. // of small integers to guarantee the algorithm doesn't run out of temporary storage.
  127. //
  128. // If you do #2, then the non-quantized algorithm will be used, but the algorithm
  129. // may run out of temporary storage and be unable to pack some rectangles.
  130. STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
  131. // Optionally call this function after init but before doing any packing to
  132. // change the handling of the out-of-temp-memory scenario, described above.
  133. // If you call init again, this will be reset to the default (false).
  134. STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
  135. // Optionally select which packing heuristic the library should use. Different
  136. // heuristics will produce better/worse results for different data sets.
  137. // If you call init again, this will be reset to the default.
  138. enum
  139. {
  140. STBRP_HEURISTIC_Skyline_default=0,
  141. STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
  142. STBRP_HEURISTIC_Skyline_BF_sortHeight
  143. };
  144. //////////////////////////////////////////////////////////////////////////////
  145. //
  146. // the details of the following structures don't matter to you, but they must
  147. // be visible so you can handle the memory allocations for them
  148. struct stbrp_node
  149. {
  150. stbrp_coord x,y;
  151. stbrp_node *next;
  152. };
  153. struct stbrp_context
  154. {
  155. int width;
  156. int height;
  157. int align;
  158. int init_mode;
  159. int heuristic;
  160. int num_nodes;
  161. stbrp_node *active_head;
  162. stbrp_node *free_head;
  163. stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
  164. };
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif
  169. //////////////////////////////////////////////////////////////////////////////
  170. //
  171. // IMPLEMENTATION SECTION
  172. //
  173. #ifdef STB_RECT_PACK_IMPLEMENTATION
  174. #ifndef STBRP_SORT
  175. #include <stdlib.h>
  176. #define STBRP_SORT qsort
  177. #endif
  178. #ifndef STBRP_ASSERT
  179. #include <assert.h>
  180. #define STBRP_ASSERT assert
  181. #endif
  182. #ifdef _MSC_VER
  183. #define STBRP__NOTUSED(v) (void)(v)
  184. #else
  185. #define STBRP__NOTUSED(v) (void)sizeof(v)
  186. #endif
  187. enum
  188. {
  189. STBRP__INIT_skyline = 1
  190. };
  191. STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
  192. {
  193. switch (context->init_mode) {
  194. case STBRP__INIT_skyline:
  195. STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
  196. context->heuristic = heuristic;
  197. break;
  198. default:
  199. STBRP_ASSERT(0);
  200. }
  201. }
  202. STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
  203. {
  204. if (allow_out_of_mem)
  205. // if it's ok to run out of memory, then don't bother aligning them;
  206. // this gives better packing, but may fail due to OOM (even though
  207. // the rectangles easily fit). @TODO a smarter approach would be to only
  208. // quantize once we've hit OOM, then we could get rid of this parameter.
  209. context->align = 1;
  210. else {
  211. // if it's not ok to run out of memory, then quantize the widths
  212. // so that num_nodes is always enough nodes.
  213. //
  214. // I.e. num_nodes * align >= width
  215. // align >= width / num_nodes
  216. // align = ceil(width/num_nodes)
  217. context->align = (context->width + context->num_nodes-1) / context->num_nodes;
  218. }
  219. }
  220. STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
  221. {
  222. int i;
  223. #ifndef STBRP_LARGE_RECTS
  224. STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
  225. #endif
  226. for (i=0; i < num_nodes-1; ++i)
  227. nodes[i].next = &nodes[i+1];
  228. nodes[i].next = NULL;
  229. context->init_mode = STBRP__INIT_skyline;
  230. context->heuristic = STBRP_HEURISTIC_Skyline_default;
  231. context->free_head = &nodes[0];
  232. context->active_head = &context->extra[0];
  233. context->width = width;
  234. context->height = height;
  235. context->num_nodes = num_nodes;
  236. stbrp_setup_allow_out_of_mem(context, 0);
  237. // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)
  238. context->extra[0].x = 0;
  239. context->extra[0].y = 0;
  240. context->extra[0].next = &context->extra[1];
  241. context->extra[1].x = (stbrp_coord) width;
  242. #ifdef STBRP_LARGE_RECTS
  243. context->extra[1].y = (1<<30);
  244. #else
  245. context->extra[1].y = 65535;
  246. #endif
  247. context->extra[1].next = NULL;
  248. }
  249. // find minimum y position if it starts at x1
  250. static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
  251. {
  252. stbrp_node *node = first;
  253. int x1 = x0 + width;
  254. int min_y, visited_width, waste_area;
  255. STBRP__NOTUSED(c);
  256. STBRP_ASSERT(first->x <= x0);
  257. #if 0
  258. // skip in case we're past the node
  259. while (node->next->x <= x0)
  260. ++node;
  261. #else
  262. STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency
  263. #endif
  264. STBRP_ASSERT(node->x <= x0);
  265. min_y = 0;
  266. waste_area = 0;
  267. visited_width = 0;
  268. while (node->x < x1) {
  269. if (node->y > min_y) {
  270. // raise min_y higher.
  271. // we've accounted for all waste up to min_y,
  272. // but we'll now add more waste for everything we've visted
  273. waste_area += visited_width * (node->y - min_y);
  274. min_y = node->y;
  275. // the first time through, visited_width might be reduced
  276. if (node->x < x0)
  277. visited_width += node->next->x - x0;
  278. else
  279. visited_width += node->next->x - node->x;
  280. } else {
  281. // add waste area
  282. int under_width = node->next->x - node->x;
  283. if (under_width + visited_width > width)
  284. under_width = width - visited_width;
  285. waste_area += under_width * (min_y - node->y);
  286. visited_width += under_width;
  287. }
  288. node = node->next;
  289. }
  290. *pwaste = waste_area;
  291. return min_y;
  292. }
  293. typedef struct
  294. {
  295. int x,y;
  296. stbrp_node **prev_link;
  297. } stbrp__findresult;
  298. static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
  299. {
  300. int best_waste = (1<<30), best_x, best_y = (1 << 30);
  301. stbrp__findresult fr;
  302. stbrp_node **prev, *node, *tail, **best = NULL;
  303. // align to multiple of c->align
  304. width = (width + c->align - 1);
  305. width -= width % c->align;
  306. STBRP_ASSERT(width % c->align == 0);
  307. // if it can't possibly fit, bail immediately
  308. if (width > c->width || height > c->height) {
  309. fr.prev_link = NULL;
  310. fr.x = fr.y = 0;
  311. return fr;
  312. }
  313. node = c->active_head;
  314. prev = &c->active_head;
  315. while (node->x + width <= c->width) {
  316. int y,waste;
  317. y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
  318. if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
  319. // bottom left
  320. if (y < best_y) {
  321. best_y = y;
  322. best = prev;
  323. }
  324. } else {
  325. // best-fit
  326. if (y + height <= c->height) {
  327. // can only use it if it first vertically
  328. if (y < best_y || (y == best_y && waste < best_waste)) {
  329. best_y = y;
  330. best_waste = waste;
  331. best = prev;
  332. }
  333. }
  334. }
  335. prev = &node->next;
  336. node = node->next;
  337. }
  338. best_x = (best == NULL) ? 0 : (*best)->x;
  339. // if doing best-fit (BF), we also have to try aligning right edge to each node position
  340. //
  341. // e.g, if fitting
  342. //
  343. // ____________________
  344. // |____________________|
  345. //
  346. // into
  347. //
  348. // | |
  349. // | ____________|
  350. // |____________|
  351. //
  352. // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
  353. //
  354. // This makes BF take about 2x the time
  355. if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
  356. tail = c->active_head;
  357. node = c->active_head;
  358. prev = &c->active_head;
  359. // find first node that's admissible
  360. while (tail->x < width)
  361. tail = tail->next;
  362. while (tail) {
  363. int xpos = tail->x - width;
  364. int y,waste;
  365. STBRP_ASSERT(xpos >= 0);
  366. // find the left position that matches this
  367. while (node->next->x <= xpos) {
  368. prev = &node->next;
  369. node = node->next;
  370. }
  371. STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
  372. y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
  373. if (y + height <= c->height) {
  374. if (y <= best_y) {
  375. if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
  376. best_x = xpos;
  377. STBRP_ASSERT(y <= best_y);
  378. best_y = y;
  379. best_waste = waste;
  380. best = prev;
  381. }
  382. }
  383. }
  384. tail = tail->next;
  385. }
  386. }
  387. fr.prev_link = best;
  388. fr.x = best_x;
  389. fr.y = best_y;
  390. return fr;
  391. }
  392. static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
  393. {
  394. // find best position according to heuristic
  395. stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
  396. stbrp_node *node, *cur;
  397. // bail if:
  398. // 1. it failed
  399. // 2. the best node doesn't fit (we don't always check this)
  400. // 3. we're out of memory
  401. if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
  402. res.prev_link = NULL;
  403. return res;
  404. }
  405. // on success, create new node
  406. node = context->free_head;
  407. node->x = (stbrp_coord) res.x;
  408. node->y = (stbrp_coord) (res.y + height);
  409. context->free_head = node->next;
  410. // insert the new node into the right starting point, and
  411. // let 'cur' point to the remaining nodes needing to be
  412. // stiched back in
  413. cur = *res.prev_link;
  414. if (cur->x < res.x) {
  415. // preserve the existing one, so start testing with the next one
  416. stbrp_node *next = cur->next;
  417. cur->next = node;
  418. cur = next;
  419. } else {
  420. *res.prev_link = node;
  421. }
  422. // from here, traverse cur and free the nodes, until we get to one
  423. // that shouldn't be freed
  424. while (cur->next && cur->next->x <= res.x + width) {
  425. stbrp_node *next = cur->next;
  426. // move the current node to the free list
  427. cur->next = context->free_head;
  428. context->free_head = cur;
  429. cur = next;
  430. }
  431. // stitch the list back in
  432. node->next = cur;
  433. if (cur->x < res.x + width)
  434. cur->x = (stbrp_coord) (res.x + width);
  435. #ifdef _DEBUG
  436. cur = context->active_head;
  437. while (cur->x < context->width) {
  438. STBRP_ASSERT(cur->x < cur->next->x);
  439. cur = cur->next;
  440. }
  441. STBRP_ASSERT(cur->next == NULL);
  442. {
  443. int count=0;
  444. cur = context->active_head;
  445. while (cur) {
  446. cur = cur->next;
  447. ++count;
  448. }
  449. cur = context->free_head;
  450. while (cur) {
  451. cur = cur->next;
  452. ++count;
  453. }
  454. STBRP_ASSERT(count == context->num_nodes+2);
  455. }
  456. #endif
  457. return res;
  458. }
  459. static int rect_height_compare(const void *a, const void *b)
  460. {
  461. const stbrp_rect *p = (const stbrp_rect *) a;
  462. const stbrp_rect *q = (const stbrp_rect *) b;
  463. if (p->h > q->h)
  464. return -1;
  465. if (p->h < q->h)
  466. return 1;
  467. return (p->w > q->w) ? -1 : (p->w < q->w);
  468. }
  469. static int rect_original_order(const void *a, const void *b)
  470. {
  471. const stbrp_rect *p = (const stbrp_rect *) a;
  472. const stbrp_rect *q = (const stbrp_rect *) b;
  473. return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
  474. }
  475. #ifdef STBRP_LARGE_RECTS
  476. #define STBRP__MAXVAL 0xffffffff
  477. #else
  478. #define STBRP__MAXVAL 0xffff
  479. #endif
  480. STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
  481. {
  482. int i, all_rects_packed = 1;
  483. // we use the 'was_packed' field internally to allow sorting/unsorting
  484. for (i=0; i < num_rects; ++i) {
  485. rects[i].was_packed = i;
  486. }
  487. // sort according to heuristic
  488. STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
  489. for (i=0; i < num_rects; ++i) {
  490. if (rects[i].w == 0 || rects[i].h == 0) {
  491. rects[i].x = rects[i].y = 0; // empty rect needs no space
  492. } else {
  493. stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
  494. if (fr.prev_link) {
  495. rects[i].x = (stbrp_coord) fr.x;
  496. rects[i].y = (stbrp_coord) fr.y;
  497. } else {
  498. rects[i].x = rects[i].y = STBRP__MAXVAL;
  499. }
  500. }
  501. }
  502. // unsort
  503. STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
  504. // set was_packed flags and all_rects_packed status
  505. for (i=0; i < num_rects; ++i) {
  506. rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
  507. if (!rects[i].was_packed)
  508. all_rects_packed = 0;
  509. }
  510. // return the all_rects_packed status
  511. return all_rects_packed;
  512. }
  513. #endif
  514. /*
  515. ------------------------------------------------------------------------------
  516. This software is available under 2 licenses -- choose whichever you prefer.
  517. ------------------------------------------------------------------------------
  518. ALTERNATIVE A - MIT License
  519. Copyright (c) 2017 Sean Barrett
  520. Permission is hereby granted, free of charge, to any person obtaining a copy of
  521. this software and associated documentation files (the "Software"), to deal in
  522. the Software without restriction, including without limitation the rights to
  523. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  524. of the Software, and to permit persons to whom the Software is furnished to do
  525. so, subject to the following conditions:
  526. The above copyright notice and this permission notice shall be included in all
  527. copies or substantial portions of the Software.
  528. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  529. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  530. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  531. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  532. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  533. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  534. SOFTWARE.
  535. ------------------------------------------------------------------------------
  536. ALTERNATIVE B - Public Domain (www.unlicense.org)
  537. This is free and unencumbered software released into the public domain.
  538. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  539. software, either in source code form or as a compiled binary, for any purpose,
  540. commercial or non-commercial, and by any means.
  541. In jurisdictions that recognize copyright laws, the author or authors of this
  542. software dedicate any and all copyright interest in the software to the public
  543. domain. We make this dedication for the benefit of the public at large and to
  544. the detriment of our heirs and successors. We intend this dedication to be an
  545. overt act of relinquishment in perpetuity of all present and future rights to
  546. this software under copyright law.
  547. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  548. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  549. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  550. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  551. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  552. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  553. ------------------------------------------------------------------------------
  554. */