Leptonica  1.83.1
Image processing and image analysis suite
boxbasic.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
132 #ifdef HAVE_CONFIG_H
133 #include <config_auto.h>
134 #endif /* HAVE_CONFIG_H */
135 
136 #include <string.h>
137 #include "allheaders.h"
138 #include "pix_internal.h"
139 
140  /* Bounds on array sizes */
141 static const size_t MaxBoxaPtrArraySize = 10000000;
142 static const size_t MaxBoxaaPtrArraySize = 1000000;
143 static const size_t InitialPtrArraySize = 20;
145 /*---------------------------------------------------------------------*
146  * Box creation, destruction and copy *
147  *---------------------------------------------------------------------*/
170 BOX *
171 boxCreate(l_int32 x,
172  l_int32 y,
173  l_int32 w,
174  l_int32 h)
175 {
176 BOX *box;
177 
178  if (w < 0 || h < 0)
179  return (BOX *)ERROR_PTR("w and h not both >= 0", __func__, NULL);
180  if (x < 0) { /* take part in +quad */
181  w = w + x;
182  x = 0;
183  if (w <= 0)
184  return (BOX *)ERROR_PTR("x < 0 and box off +quad", __func__, NULL);
185  }
186  if (y < 0) { /* take part in +quad */
187  h = h + y;
188  y = 0;
189  if (h <= 0)
190  return (BOX *)ERROR_PTR("y < 0 and box off +quad", __func__, NULL);
191  }
192 
193  box = (BOX *)LEPT_CALLOC(1, sizeof(BOX));
194  boxSetGeometry(box, x, y, w, h);
195  box->refcount = 1;
196  return box;
197 }
198 
199 
211 BOX *
212 boxCreateValid(l_int32 x,
213  l_int32 y,
214  l_int32 w,
215  l_int32 h)
216 {
217  if (w <= 0 || h <= 0)
218  return (BOX *)ERROR_PTR("w and h not both > 0", __func__, NULL);
219  return boxCreate(x, y, w, h);
220 }
221 
222 
229 BOX *
231 {
232 BOX *boxc;
233 
234  if (!box)
235  return (BOX *)ERROR_PTR("box not defined", __func__, NULL);
236 
237  boxc = boxCreate(box->x, box->y, box->w, box->h);
238  return boxc;
239 }
240 
241 
248 BOX *
250 {
251 
252  if (!box)
253  return (BOX *)ERROR_PTR("box not defined", __func__, NULL);
254 
255  ++box->refcount;
256  return box;
257 }
258 
259 
272 void
274 {
275 BOX *box;
276 
277  if (pbox == NULL) {
278  L_WARNING("ptr address is null!\n", __func__);
279  return;
280  }
281  if ((box = *pbox) == NULL)
282  return;
283 
284  if (--box->refcount == 0)
285  LEPT_FREE(box);
286  *pbox = NULL;
287 }
288 
289 
290 /*---------------------------------------------------------------------*
291  * Box accessors *
292  *---------------------------------------------------------------------*/
300 l_ok
301 boxGetGeometry(const BOX *box,
302  l_int32 *px,
303  l_int32 *py,
304  l_int32 *pw,
305  l_int32 *ph)
306 {
307  if (px) *px = 0;
308  if (py) *py = 0;
309  if (pw) *pw = 0;
310  if (ph) *ph = 0;
311  if (!box)
312  return ERROR_INT("box not defined", __func__, 1);
313  if (px) *px = box->x;
314  if (py) *py = box->y;
315  if (pw) *pw = box->w;
316  if (ph) *ph = box->h;
317  return 0;
318 }
319 
320 
328 l_ok
330  l_int32 x,
331  l_int32 y,
332  l_int32 w,
333  l_int32 h)
334 {
335  if (!box)
336  return ERROR_INT("box not defined", __func__, 1);
337  if (x != -1) box->x = x;
338  if (y != -1) box->y = y;
339  if (w != -1) box->w = w;
340  if (h != -1) box->h = h;
341  return 0;
342 }
343 
344 
357 l_ok
359  l_int32 *pl,
360  l_int32 *pr,
361  l_int32 *pt,
362  l_int32 *pb)
363 {
364 l_int32 x, y, w, h;
365 
366  if (pl) *pl = 0;
367  if (pr) *pr = 0;
368  if (pt) *pt = 0;
369  if (pb) *pb = 0;
370  if (!box)
371  return ERROR_INT("box not defined", __func__, 1);
372 
373  boxGetGeometry(box, &x, &y, &w, &h);
374  if (pl) *pl = x;
375  if (pr) *pr = x + w - 1;
376  if (pt) *pt = y;
377  if (pb) *pb = y + h - 1;
378  return 0;
379 }
380 
381 
389 l_ok
391  l_int32 l,
392  l_int32 r,
393  l_int32 t,
394  l_int32 b)
395 {
396 l_int32 x, y, w, h;
397 
398  if (!box)
399  return ERROR_INT("box not defined", __func__, 1);
400  x = (l != -1) ? l : box->x;
401  w = (r != -1) ? r - x + 1 : box->x + box->w - x;
402  y = (t != -1) ? t : box->y;
403  h = (b != -1) ? b - y + 1 : box->y + box->h - y;
404  boxSetGeometry(box, x, y, w, h);
405  return 0;
406 }
407 
408 
416 l_ok
418  l_int32 *pvalid)
419 {
420  if (!pvalid)
421  return ERROR_INT("&valid not defined", __func__, 1);
422  *pvalid = 0;
423  if (!box)
424  return ERROR_INT("box not defined", __func__, 1);
425 
426  if (box->w > 0 && box->h > 0)
427  *pvalid = 1;
428  return 0;
429 }
430 
431 
432 /*---------------------------------------------------------------------*
433  * Boxa creation, destruction, copy, extension *
434  *---------------------------------------------------------------------*/
441 BOXA *
442 boxaCreate(l_int32 n)
443 {
444 BOXA *boxa;
445 
446  if (n <= 0 || n > MaxBoxaPtrArraySize)
448 
449  boxa = (BOXA *)LEPT_CALLOC(1, sizeof(BOXA));
450  boxa->n = 0;
451  boxa->nalloc = n;
452  boxa->refcount = 1;
453  if ((boxa->box = (BOX **)LEPT_CALLOC(n, sizeof(BOX *))) == NULL) {
454  boxaDestroy(&boxa);
455  return (BOXA *)ERROR_PTR("boxa ptrs not made", __func__, NULL);
456  }
457  return boxa;
458 }
459 
460 
474 BOXA *
476  l_int32 copyflag)
477 {
478 l_int32 i;
479 BOX *boxc;
480 BOXA *boxac;
481 
482  if (!boxa)
483  return (BOXA *)ERROR_PTR("boxa not defined", __func__, NULL);
484 
485  if (copyflag == L_CLONE) {
486  boxa->refcount++;
487  return boxa;
488  }
489 
490  if (copyflag != L_COPY && copyflag != L_COPY_CLONE)
491  return (BOXA *)ERROR_PTR("invalid copyflag", __func__, NULL);
492 
493  if ((boxac = boxaCreate(boxa->nalloc)) == NULL)
494  return (BOXA *)ERROR_PTR("boxac not made", __func__, NULL);
495  for (i = 0; i < boxa->n; i++) {
496  if (copyflag == L_COPY)
497  boxc = boxaGetBox(boxa, i, L_COPY);
498  else /* copy-clone */
499  boxc = boxaGetBox(boxa, i, L_CLONE);
500  boxaAddBox(boxac, boxc, L_INSERT);
501  }
502  return boxac;
503 }
504 
505 
518 void
520 {
521 l_int32 i;
522 BOXA *boxa;
523 
524  if (pboxa == NULL) {
525  L_WARNING("ptr address is null!\n", __func__);
526  return;
527  }
528 
529  if ((boxa = *pboxa) == NULL)
530  return;
531 
532  /* Decrement the ref count. If it is 0, destroy the boxa. */
533  if (--boxa->refcount == 0) {
534  for (i = 0; i < boxa->n; i++)
535  boxDestroy(&boxa->box[i]);
536  LEPT_FREE(boxa->box);
537  LEPT_FREE(boxa);
538  }
539 
540  *pboxa = NULL;
541 }
542 
543 
552 l_ok
554  BOX *box,
555  l_int32 copyflag)
556 {
557 l_int32 n;
558 BOX *boxc;
559 
560  if (!boxa)
561  return ERROR_INT("boxa not defined", __func__, 1);
562  if (!box)
563  return ERROR_INT("box not defined", __func__, 1);
564 
565  if (copyflag == L_INSERT)
566  boxc = box;
567  else if (copyflag == L_COPY)
568  boxc = boxCopy(box);
569  else if (copyflag == L_CLONE)
570  boxc = boxClone(box);
571  else
572  return ERROR_INT("invalid copyflag", __func__, 1);
573  if (!boxc)
574  return ERROR_INT("boxc not made", __func__, 1);
575 
576  n = boxaGetCount(boxa);
577  if (n >= boxa->nalloc) {
578  if (boxaExtendArray(boxa)) {
579  if (copyflag != L_INSERT)
580  boxDestroy(&boxc);
581  return ERROR_INT("extension failed", __func__, 1);
582  }
583  }
584  boxa->box[n] = boxc;
585  boxa->n++;
586  return 0;
587 }
588 
589 
601 l_ok
603 {
604  if (!boxa)
605  return ERROR_INT("boxa not defined", __func__, 1);
606 
607  return boxaExtendArrayToSize(boxa, 2 * boxa->nalloc);
608 }
609 
610 
624 l_ok
626  size_t size)
627 {
628 size_t oldsize, newsize;
629 
630  if (!boxa)
631  return ERROR_INT("boxa not defined", __func__, 1);
632  if (boxa->nalloc > MaxBoxaPtrArraySize) /* belt & suspenders */
633  return ERROR_INT("boxa has too many ptrs", __func__, 1);
634  if (size > MaxBoxaPtrArraySize)
635  return ERROR_INT("size > 10M box ptrs; too large", __func__, 1);
636  if (size <= boxa->nalloc) {
637  L_INFO("size too small; no extension\n", __func__);
638  return 0;
639  }
640 
641  oldsize = boxa->nalloc * sizeof(BOX *);
642  newsize = size * sizeof(BOX *);
643  if ((boxa->box = (BOX **)reallocNew((void **)&boxa->box,
644  oldsize, newsize)) == NULL)
645  return ERROR_INT("new ptr array not returned", __func__, 1);
646  boxa->nalloc = size;
647  return 0;
648 }
649 
650 
651 /*---------------------------------------------------------------------*
652  * Boxa accessors *
653  *---------------------------------------------------------------------*/
660 l_int32
661 boxaGetCount(const BOXA *boxa)
662 {
663  if (!boxa)
664  return ERROR_INT("boxa not defined", __func__, 0);
665  return boxa->n;
666 }
667 
668 
675 l_int32
677 {
678 l_int32 n, i, w, h, count;
679 
680  if (!boxa)
681  return ERROR_INT("boxa not defined", __func__, 0);
682 
683  n = boxaGetCount(boxa);
684  for (i = 0, count = 0; i < n; i++) {
685  boxaGetBoxGeometry(boxa, i, NULL, NULL, &w, &h);
686  if (w > 0 && h > 0)
687  count++;
688  }
689  return count;
690 }
691 
692 
701 BOX *
703  l_int32 index,
704  l_int32 accessflag)
705 {
706  if (!boxa)
707  return (BOX *)ERROR_PTR("boxa not defined", __func__, NULL);
708  if (index < 0 || index >= boxa->n)
709  return (BOX *)ERROR_PTR("index not valid", __func__, NULL);
710 
711  if (accessflag == L_COPY)
712  return boxCopy(boxa->box[index]);
713  else if (accessflag == L_CLONE)
714  return boxClone(boxa->box[index]);
715  else
716  return (BOX *)ERROR_PTR("invalid accessflag", __func__, NULL);
717 }
718 
719 
738 BOX *
740  l_int32 index,
741  l_int32 accessflag)
742 {
743 l_int32 w, h;
744 BOX *box;
745 
746  if (!boxa)
747  return (BOX *)ERROR_PTR("boxa not defined", __func__, NULL);
748 
749  if ((box = boxaGetBox(boxa, index, accessflag)) == NULL)
750  return (BOX *)ERROR_PTR("box not returned", __func__, NULL);
751  boxGetGeometry(box, NULL, NULL, &w, &h);
752  if (w <= 0 || h <= 0) /* not valid, but not necessarily an error */
753  boxDestroy(&box);
754  return box;
755 }
756 
757 
764 NUMA *
766 {
767 l_int32 i, n, w, h;
768 NUMA *na;
769 
770  if (!boxa)
771  return (NUMA *)ERROR_PTR("boxa not defined", __func__, NULL);
772 
773  n = boxaGetCount(boxa);
774  if (boxaGetValidCount(boxa) == n)
775  return NULL;
776 
777  na = numaMakeConstant(0, n);
778  for (i = 0; i < n; i++) {
779  boxaGetBoxGeometry(boxa, i, NULL, NULL, &w, &h);
780  if (w == 0 || h == 0)
781  numaSetValue(na, i, 1);
782  }
783  return na;
784 }
785 
786 
795 l_ok
797  l_int32 index,
798  l_int32 *px,
799  l_int32 *py,
800  l_int32 *pw,
801  l_int32 *ph)
802 {
803 BOX *box;
804 
805  if (px) *px = 0;
806  if (py) *py = 0;
807  if (pw) *pw = 0;
808  if (ph) *ph = 0;
809  if (!boxa)
810  return ERROR_INT("boxa not defined", __func__, 1);
811  if (index < 0 || index >= boxa->n)
812  return ERROR_INT("index not valid", __func__, 1);
813 
814  if ((box = boxaGetBox(boxa, index, L_CLONE)) == NULL)
815  return ERROR_INT("box not found!", __func__, 1);
816  boxGetGeometry(box, px, py, pw, ph);
817  boxDestroy(&box);
818  return 0;
819 }
820 
821 
829 l_ok
831  l_int32 *pfull)
832 {
833 l_int32 i, n, full;
834 BOX *box;
835 
836  if (!pfull)
837  return ERROR_INT("&full not defined", __func__, 1);
838  *pfull = 0;
839  if (!boxa)
840  return ERROR_INT("boxa not defined", __func__, 1);
841 
842  n = boxaGetCount(boxa);
843  full = 1;
844  for (i = 0; i < n; i++) {
845  if ((box = boxaGetBox(boxa, i, L_CLONE)) == NULL) {
846  full = 0;
847  break;
848  }
849  boxDestroy(&box);
850  }
851  *pfull = full;
852  return 0;
853 }
854 
855 
856 /*---------------------------------------------------------------------*
857  * Boxa array modifiers *
858  *---------------------------------------------------------------------*/
874 l_ok
876  l_int32 index,
877  BOX *box)
878 {
879  if (!boxa)
880  return ERROR_INT("boxa not defined", __func__, 1);
881  if (index < 0 || index >= boxa->n)
882  return ERROR_INT("index not valid", __func__, 1);
883  if (!box)
884  return ERROR_INT("box not defined", __func__, 1);
885 
886  boxDestroy(&(boxa->box[index]));
887  boxa->box[index] = box;
888  return 0;
889 }
890 
891 
910 l_ok
912  l_int32 index,
913  BOX *box)
914 {
915 l_int32 i, n;
916 BOX **array;
917 
918  if (!boxa)
919  return ERROR_INT("boxa not defined", __func__, 1);
920  n = boxaGetCount(boxa);
921  if (index < 0 || index > n) {
922  L_ERROR("index %d not in [0,...,%d]\n", __func__, index, n);
923  return 1;
924  }
925  if (!box)
926  return ERROR_INT("box not defined", __func__, 1);
927 
928  if (n >= boxa->nalloc) {
929  if (boxaExtendArray(boxa))
930  return ERROR_INT("extension failed", __func__, 1);
931  }
932  array = boxa->box;
933  boxa->n++;
934  for (i = n; i > index; i--)
935  array[i] = array[i - 1];
936  array[index] = box;
937  return 0;
938 }
939 
940 
956 l_ok
958  l_int32 index)
959 {
960  return boxaRemoveBoxAndSave(boxa, index, NULL);
961 }
962 
963 
980 l_ok
982  l_int32 index,
983  BOX **pbox)
984 {
985 l_int32 i, n;
986 BOX **array;
987 
988  if (pbox) *pbox = NULL;
989  if (!boxa)
990  return ERROR_INT("boxa not defined", __func__, 1);
991  n = boxaGetCount(boxa);
992  if (index < 0 || index >= n) {
993  L_ERROR("index %d not in [0,...,%d]\n", __func__, index, n - 1);
994  return 1;
995  }
996 
997  if (pbox)
998  *pbox = boxaGetBox(boxa, index, L_CLONE);
999  array = boxa->box;
1000  boxDestroy(&array[index]);
1001  for (i = index + 1; i < n; i++)
1002  array[i - 1] = array[i];
1003  array[n - 1] = NULL;
1004  boxa->n--;
1005 
1006  return 0;
1007 }
1008 
1009 
1022 BOXA *
1024  l_int32 copyflag)
1025 {
1026 l_int32 i, n;
1027 BOX *box;
1028 BOXA *boxad;
1029 
1030  if (!boxas)
1031  return (BOXA *)ERROR_PTR("boxas not defined", __func__, NULL);
1032  if (copyflag != L_COPY && copyflag != L_CLONE)
1033  return (BOXA *)ERROR_PTR("invalid copyflag", __func__, NULL);
1034 
1035  n = boxaGetCount(boxas);
1036  boxad = boxaCreate(n);
1037  for (i = 0; i < n; i++) {
1038  if ((box = boxaGetValidBox(boxas, i, copyflag)) != NULL)
1039  boxaAddBox(boxad, box, L_INSERT);
1040  }
1041 
1042  return boxad;
1043 }
1044 
1045 
1084 l_ok
1086  BOX *box)
1087 {
1088 l_int32 i, n;
1089 BOX *boxt;
1090 
1091  if (!boxa)
1092  return ERROR_INT("boxa not defined", __func__, 1);
1093 
1094  n = boxa->nalloc;
1095  boxa->n = n;
1096  for (i = 0; i < n; i++) {
1097  if (box)
1098  boxt = boxCopy(box);
1099  else
1100  boxt = boxCreate(0, 0, 0, 0);
1101  boxaReplaceBox(boxa, i, boxt);
1102  }
1103  return 0;
1104 }
1105 
1106 
1119 l_ok
1121 {
1122 l_int32 i, n;
1123 
1124  if (!boxa)
1125  return ERROR_INT("boxa not defined", __func__, 1);
1126 
1127  n = boxaGetCount(boxa);
1128  for (i = 0; i < n; i++)
1129  boxDestroy(&boxa->box[i]);
1130  boxa->n = 0;
1131  return 0;
1132 }
1133 
1134 
1135 /*--------------------------------------------------------------------------*
1136  * Boxaa creation, destruction *
1137  *--------------------------------------------------------------------------*/
1144 BOXAA *
1145 boxaaCreate(l_int32 n)
1146 {
1147 BOXAA *baa;
1148 
1149  if (n <= 0 || n > MaxBoxaaPtrArraySize)
1150  n = InitialPtrArraySize;
1151 
1152  baa = (BOXAA *)LEPT_CALLOC(1, sizeof(BOXAA));
1153  if ((baa->boxa = (BOXA **)LEPT_CALLOC(n, sizeof(BOXA *))) == NULL) {
1154  boxaaDestroy(&baa);
1155  return (BOXAA *)ERROR_PTR("boxa ptr array not made", __func__, NULL);
1156  }
1157  baa->nalloc = n;
1158  baa->n = 0;
1159  return baa;
1160 }
1161 
1162 
1177 BOXAA *
1179  l_int32 copyflag)
1180 {
1181 l_int32 i, n;
1182 BOXA *boxa;
1183 BOXAA *baad;
1184 
1185  if (!baas)
1186  return (BOXAA *)ERROR_PTR("baas not defined", __func__, NULL);
1187  if (copyflag != L_COPY && copyflag != L_CLONE)
1188  return (BOXAA *)ERROR_PTR("invalid copyflag", __func__, NULL);
1189 
1190  n = boxaaGetCount(baas);
1191  baad = boxaaCreate(n);
1192  for (i = 0; i < n; i++) {
1193  boxa = boxaaGetBoxa(baas, i, copyflag);
1194  boxaaAddBoxa(baad, boxa, L_INSERT);
1195  }
1196 
1197  return baad;
1198 }
1199 
1200 
1206 void
1208 {
1209 l_int32 i;
1210 BOXAA *baa;
1211 
1212  if (pbaa == NULL) {
1213  L_WARNING("ptr address is NULL!\n", __func__);
1214  return;
1215  }
1216 
1217  if ((baa = *pbaa) == NULL)
1218  return;
1219 
1220  for (i = 0; i < baa->n; i++)
1221  boxaDestroy(&baa->boxa[i]);
1222  LEPT_FREE(baa->boxa);
1223  LEPT_FREE(baa);
1224  *pbaa = NULL;
1225 }
1226 
1227 
1228 
1229 /*--------------------------------------------------------------------------*
1230  * Add Boxa to Boxaa *
1231  *--------------------------------------------------------------------------*/
1240 l_ok
1242  BOXA *ba,
1243  l_int32 copyflag)
1244 {
1245 l_int32 n;
1246 BOXA *bac;
1247 
1248  if (!baa)
1249  return ERROR_INT("baa not defined", __func__, 1);
1250  if (!ba)
1251  return ERROR_INT("ba not defined", __func__, 1);
1252  if (copyflag != L_INSERT && copyflag != L_COPY && copyflag != L_CLONE)
1253  return ERROR_INT("invalid copyflag", __func__, 1);
1254 
1255  if (copyflag == L_INSERT)
1256  bac = ba;
1257  else
1258  bac = boxaCopy(ba, copyflag);
1259 
1260  n = boxaaGetCount(baa);
1261  if (n >= baa->nalloc) {
1262  if (boxaaExtendArray(baa))
1263  return ERROR_INT("extension failed", __func__, 1);
1264  }
1265  baa->boxa[n] = bac;
1266  baa->n++;
1267  return 0;
1268 }
1269 
1270 
1283 l_ok
1285 {
1286  if (!baa)
1287  return ERROR_INT("baa not defined", __func__, 1);
1288 
1289  return boxaaExtendArrayToSize(baa, 2 * baa->nalloc);
1290 }
1291 
1292 
1306 l_ok
1308  l_int32 size)
1309 {
1310 size_t oldsize, newsize;
1311 
1312  if (!baa)
1313  return ERROR_INT("baa not defined", __func__, 1);
1314  if (baa->nalloc > MaxBoxaaPtrArraySize) /* belt & suspenders */
1315  return ERROR_INT("baa has too many ptrs", __func__, 1);
1316  if (size > MaxBoxaaPtrArraySize)
1317  return ERROR_INT("size > 1M boxa ptrs; too large", __func__, 1);
1318  if (size <= baa->nalloc) {
1319  L_INFO("size too small; no extension\n", __func__);
1320  return 0;
1321  }
1322 
1323  oldsize = baa->nalloc * sizeof(BOXA *);
1324  newsize = size * sizeof(BOXA *);
1325  if ((baa->boxa = (BOXA **)reallocNew((void **)&baa->boxa,
1326  oldsize, newsize)) == NULL)
1327  return ERROR_INT("new ptr array not returned", __func__, 1);
1328  baa->nalloc = size;
1329  return 0;
1330 }
1331 
1332 
1333 /*----------------------------------------------------------------------*
1334  * Boxaa accessors *
1335  *----------------------------------------------------------------------*/
1342 l_int32
1344 {
1345  if (!baa)
1346  return ERROR_INT("baa not defined", __func__, 0);
1347  return baa->n;
1348 }
1349 
1350 
1357 l_int32
1359 {
1360 BOXA *boxa;
1361 l_int32 n, sum, i;
1362 
1363  if (!baa)
1364  return ERROR_INT("baa not defined", __func__, 0);
1365 
1366  n = boxaaGetCount(baa);
1367  for (sum = 0, i = 0; i < n; i++) {
1368  boxa = boxaaGetBoxa(baa, i, L_CLONE);
1369  sum += boxaGetCount(boxa);
1370  boxaDestroy(&boxa);
1371  }
1372 
1373  return sum;
1374 }
1375 
1376 
1385 BOXA *
1387  l_int32 index,
1388  l_int32 accessflag)
1389 {
1390 l_int32 n;
1391 
1392  if (!baa)
1393  return (BOXA *)ERROR_PTR("baa not defined", __func__, NULL);
1394  n = boxaaGetCount(baa);
1395  if (index < 0 || index >= n)
1396  return (BOXA *)ERROR_PTR("index not valid", __func__, NULL);
1397  if (accessflag != L_COPY && accessflag != L_CLONE)
1398  return (BOXA *)ERROR_PTR("invalid accessflag", __func__, NULL);
1399 
1400  return boxaCopy(baa->boxa[index], accessflag);
1401 }
1402 
1403 
1413 BOX *
1415  l_int32 iboxa,
1416  l_int32 ibox,
1417  l_int32 accessflag)
1418 {
1419 BOX *box;
1420 BOXA *boxa;
1421 
1422  if ((boxa = boxaaGetBoxa(baa, iboxa, L_CLONE)) == NULL)
1423  return (BOX *)ERROR_PTR("boxa not retrieved", __func__, NULL);
1424  if ((box = boxaGetBox(boxa, ibox, accessflag)) == NULL)
1425  L_ERROR("box not retrieved\n", __func__);
1426  boxaDestroy(&boxa);
1427  return box;
1428 }
1429 
1430 
1431 /*----------------------------------------------------------------------*
1432  * Boxaa array modifiers *
1433  *----------------------------------------------------------------------*/
1463 l_ok
1465  BOXA *boxa)
1466 {
1467 l_int32 i, n;
1468 BOXA *boxat;
1469 
1470  if (!baa)
1471  return ERROR_INT("baa not defined", __func__, 1);
1472  if (!boxa)
1473  return ERROR_INT("boxa not defined", __func__, 1);
1474 
1475  n = baa->nalloc;
1476  baa->n = n;
1477  for (i = 0; i < n; i++) {
1478  boxat = boxaCopy(boxa, L_COPY);
1479  boxaaReplaceBoxa(baa, i, boxat);
1480  }
1481  return 0;
1482 }
1483 
1484 
1501 l_ok
1503  l_int32 maxindex,
1504  BOXA *boxa)
1505 {
1506 l_int32 i, n;
1507 
1508  if (!baa)
1509  return ERROR_INT("baa not defined", __func__, 1);
1510  if (!boxa)
1511  return ERROR_INT("boxa not defined", __func__, 1);
1512 
1513  /* Extend the ptr array if necessary */
1514  n = boxaaGetCount(baa);
1515  if (maxindex < n) return 0;
1516  if (boxaaExtendArrayToSize(baa, maxindex + 1))
1517  return ERROR_INT("extension failed", __func__, 1);
1518 
1519  /* Fill the new entries with copies of boxa */
1520  for (i = n; i <= maxindex; i++)
1521  boxaaAddBoxa(baa, boxa, L_COPY);
1522  return 0;
1523 }
1524 
1525 
1541 l_ok
1543  l_int32 index,
1544  BOXA *boxa)
1545 {
1546 l_int32 n;
1547 
1548  if (!baa)
1549  return ERROR_INT("baa not defined", __func__, 1);
1550  if (!boxa)
1551  return ERROR_INT("boxa not defined", __func__, 1);
1552  n = boxaaGetCount(baa);
1553  if (index < 0 || index >= n)
1554  return ERROR_INT("index not valid", __func__, 1);
1555 
1556  boxaDestroy(&baa->boxa[index]);
1557  baa->boxa[index] = boxa;
1558  return 0;
1559 }
1560 
1561 
1581 l_ok
1583  l_int32 index,
1584  BOXA *boxa)
1585 {
1586 l_int32 i, n;
1587 BOXA **array;
1588 
1589  if (!baa)
1590  return ERROR_INT("baa not defined", __func__, 1);
1591  n = boxaaGetCount(baa);
1592  if (index < 0 || index > n) {
1593  L_ERROR("index %d not in [0,...,%d]\n", __func__, index, n);
1594  return 1;
1595  }
1596  if (!boxa)
1597  return ERROR_INT("boxa not defined", __func__, 1);
1598 
1599  if (n >= baa->nalloc) {
1600  if (boxaaExtendArray(baa))
1601  return ERROR_INT("extension failed", __func__, 1);
1602  }
1603  array = baa->boxa;
1604  baa->n++;
1605  for (i = n; i > index; i--)
1606  array[i] = array[i - 1];
1607  array[index] = boxa;
1608  return 0;
1609 }
1610 
1611 
1628 l_ok
1630  l_int32 index)
1631 {
1632 l_int32 i, n;
1633 BOXA **array;
1634 
1635  if (!baa)
1636  return ERROR_INT("baa not defined", __func__, 1);
1637  n = boxaaGetCount(baa);
1638  if (index < 0 || index >= n)
1639  return ERROR_INT("index not valid", __func__, 1);
1640 
1641  array = baa->boxa;
1642  boxaDestroy(&array[index]);
1643  for (i = index + 1; i < n; i++)
1644  array[i - 1] = array[i];
1645  array[n - 1] = NULL;
1646  baa->n--;
1647 
1648  return 0;
1649 }
1650 
1651 
1666 l_ok
1668  l_int32 index,
1669  BOX *box,
1670  l_int32 accessflag)
1671 {
1672 l_int32 n;
1673 BOXA *boxa;
1674  if (!baa)
1675  return ERROR_INT("baa not defined", __func__, 1);
1676  n = boxaaGetCount(baa);
1677  if (index < 0 || index >= n)
1678  return ERROR_INT("index not valid", __func__, 1);
1679  if (accessflag != L_INSERT && accessflag != L_COPY && accessflag != L_CLONE)
1680  return ERROR_INT("invalid accessflag", __func__, 1);
1681 
1682  boxa = boxaaGetBoxa(baa, index, L_CLONE);
1683  boxaAddBox(boxa, box, accessflag);
1684  boxaDestroy(&boxa);
1685  return 0;
1686 }
1687 
1688 
1689 /*---------------------------------------------------------------------*
1690  * Boxaa serialized I/O *
1691  *---------------------------------------------------------------------*/
1712 BOXAA *
1713 boxaaReadFromFiles(const char *dirname,
1714  const char *substr,
1715  l_int32 first,
1716  l_int32 nfiles)
1717 {
1718 char *fname;
1719 l_int32 i, n;
1720 BOXA *boxa;
1721 BOXAA *baa;
1722 SARRAY *sa;
1723 
1724  if (!dirname)
1725  return (BOXAA *)ERROR_PTR("dirname not defined", __func__, NULL);
1726 
1727  sa = getSortedPathnamesInDirectory(dirname, substr, first, nfiles);
1728  if (!sa || ((n = sarrayGetCount(sa)) == 0)) {
1729  sarrayDestroy(&sa);
1730  return (BOXAA *)ERROR_PTR("no pixa files found", __func__, NULL);
1731  }
1732 
1733  baa = boxaaCreate(n);
1734  for (i = 0; i < n; i++) {
1735  fname = sarrayGetString(sa, i, L_NOCOPY);
1736  if ((boxa = boxaRead(fname)) == NULL) {
1737  L_ERROR("boxa not read for %d-th file", __func__, i);
1738  continue;
1739  }
1740  boxaaAddBoxa(baa, boxa, L_INSERT);
1741  }
1742 
1743  sarrayDestroy(&sa);
1744  return baa;
1745 }
1746 
1747 
1754 BOXAA *
1755 boxaaRead(const char *filename)
1756 {
1757 FILE *fp;
1758 BOXAA *baa;
1759 
1760  if (!filename)
1761  return (BOXAA *)ERROR_PTR("filename not defined", __func__, NULL);
1762 
1763  if ((fp = fopenReadStream(filename)) == NULL)
1764  return (BOXAA *)ERROR_PTR("stream not opened", __func__, NULL);
1765  baa = boxaaReadStream(fp);
1766  fclose(fp);
1767  if (!baa)
1768  return (BOXAA *)ERROR_PTR("boxaa not read", __func__, NULL);
1769  return baa;
1770 }
1771 
1772 
1784 BOXAA *
1786 {
1787 l_int32 n, i, x, y, w, h, version;
1788 l_int32 ignore;
1789 BOXA *boxa;
1790 BOXAA *baa;
1791 
1792  if (!fp)
1793  return (BOXAA *)ERROR_PTR("stream not defined", __func__, NULL);
1794 
1795  if (fscanf(fp, "\nBoxaa Version %d\n", &version) != 1)
1796  return (BOXAA *)ERROR_PTR("not a boxaa file", __func__, NULL);
1797  if (version != BOXAA_VERSION_NUMBER)
1798  return (BOXAA *)ERROR_PTR("invalid boxa version", __func__, NULL);
1799  if (fscanf(fp, "Number of boxa = %d\n", &n) != 1)
1800  return (BOXAA *)ERROR_PTR("not a boxaa file", __func__, NULL);
1801  if (n < 0)
1802  return (BOXAA *)ERROR_PTR("num boxa ptrs < 0", __func__, NULL);
1803  if (n > MaxBoxaaPtrArraySize)
1804  return (BOXAA *)ERROR_PTR("too many boxa ptrs", __func__, NULL);
1805  if (n == 0) L_INFO("the boxaa is empty\n", __func__);
1806 
1807  if ((baa = boxaaCreate(n)) == NULL)
1808  return (BOXAA *)ERROR_PTR("boxaa not made", __func__, NULL);
1809  for (i = 0; i < n; i++) {
1810  if (fscanf(fp, "\nBoxa[%d] extent: x = %d, y = %d, w = %d, h = %d",
1811  &ignore, &x, &y, &w, &h) != 5) {
1812  boxaaDestroy(&baa);
1813  return (BOXAA *)ERROR_PTR("boxa descr not valid", __func__, NULL);
1814  }
1815  if ((boxa = boxaReadStream(fp)) == NULL) {
1816  boxaaDestroy(&baa);
1817  return (BOXAA *)ERROR_PTR("boxa not made", __func__, NULL);
1818  }
1819  boxaaAddBoxa(baa, boxa, L_INSERT);
1820  }
1821  return baa;
1822 }
1823 
1824 
1832 BOXAA *
1833 boxaaReadMem(const l_uint8 *data,
1834  size_t size)
1835 {
1836 FILE *fp;
1837 BOXAA *baa;
1838 
1839  if (!data)
1840  return (BOXAA *)ERROR_PTR("data not defined", __func__, NULL);
1841  if ((fp = fopenReadFromMemory(data, size)) == NULL)
1842  return (BOXAA *)ERROR_PTR("stream not opened", __func__, NULL);
1843 
1844  baa = boxaaReadStream(fp);
1845  fclose(fp);
1846  if (!baa) L_ERROR("baa not read\n", __func__);
1847  return baa;
1848 }
1849 
1850 
1858 l_ok
1859 boxaaWrite(const char *filename,
1860  BOXAA *baa)
1861 {
1862 l_int32 ret;
1863 FILE *fp;
1864 
1865  if (!filename)
1866  return ERROR_INT("filename not defined", __func__, 1);
1867  if (!baa)
1868  return ERROR_INT("baa not defined", __func__, 1);
1869 
1870  if ((fp = fopenWriteStream(filename, "w")) == NULL)
1871  return ERROR_INT("stream not opened", __func__, 1);
1872  ret = boxaaWriteStream(fp, baa);
1873  fclose(fp);
1874  if (ret)
1875  return ERROR_INT("baa not written to stream", __func__, 1);
1876  return 0;
1877 }
1878 
1879 
1887 l_ok
1889  BOXAA *baa)
1890 {
1891 l_int32 n, i, x, y, w, h;
1892 BOX *box;
1893 BOXA *boxa;
1894 
1895  if (!fp)
1896  return ERROR_INT("stream not defined", __func__, 1);
1897  if (!baa)
1898  return ERROR_INT("baa not defined", __func__, 1);
1899 
1900  n = boxaaGetCount(baa);
1901  fprintf(fp, "\nBoxaa Version %d\n", BOXAA_VERSION_NUMBER);
1902  fprintf(fp, "Number of boxa = %d\n", n);
1903 
1904  for (i = 0; i < n; i++) {
1905  if ((boxa = boxaaGetBoxa(baa, i, L_CLONE)) == NULL)
1906  return ERROR_INT("boxa not found", __func__, 1);
1907  boxaGetExtent(boxa, NULL, NULL, &box);
1908  boxGetGeometry(box, &x, &y, &w, &h);
1909  fprintf(fp, "\nBoxa[%d] extent: x = %d, y = %d, w = %d, h = %d",
1910  i, x, y, w, h);
1911  boxaWriteStream(fp, boxa);
1912  boxDestroy(&box);
1913  boxaDestroy(&boxa);
1914  }
1915  return 0;
1916 }
1917 
1918 
1932 l_ok
1933 boxaaWriteMem(l_uint8 **pdata,
1934  size_t *psize,
1935  BOXAA *baa)
1936 {
1937 l_int32 ret;
1938 FILE *fp;
1939 
1940  if (pdata) *pdata = NULL;
1941  if (psize) *psize = 0;
1942  if (!pdata)
1943  return ERROR_INT("&data not defined", __func__, 1);
1944  if (!psize)
1945  return ERROR_INT("&size not defined", __func__, 1);
1946  if (!baa)
1947  return ERROR_INT("baa not defined", __func__, 1);
1948 
1949 #if HAVE_FMEMOPEN
1950  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
1951  return ERROR_INT("stream not opened", __func__, 1);
1952  ret = boxaaWriteStream(fp, baa);
1953  fputc('\0', fp);
1954  fclose(fp);
1955  *psize = *psize - 1;
1956 #else
1957  L_INFO("work-around: writing to a temp file\n", __func__);
1958  #ifdef _WIN32
1959  if ((fp = fopenWriteWinTempfile()) == NULL)
1960  return ERROR_INT("tmpfile stream not opened", __func__, 1);
1961  #else
1962  if ((fp = tmpfile()) == NULL)
1963  return ERROR_INT("tmpfile stream not opened", __func__, 1);
1964  #endif /* _WIN32 */
1965  ret = boxaaWriteStream(fp, baa);
1966  rewind(fp);
1967  *pdata = l_binaryReadStream(fp, psize);
1968  fclose(fp);
1969 #endif /* HAVE_FMEMOPEN */
1970  return ret;
1971 }
1972 
1973 
1974 /*---------------------------------------------------------------------*
1975  * Boxa serialized I/O *
1976  *---------------------------------------------------------------------*/
1983 BOXA *
1984 boxaRead(const char *filename)
1985 {
1986 FILE *fp;
1987 BOXA *boxa;
1988 
1989  if (!filename)
1990  return (BOXA *)ERROR_PTR("filename not defined", __func__, NULL);
1991 
1992  if ((fp = fopenReadStream(filename)) == NULL)
1993  return (BOXA *)ERROR_PTR("stream not opened", __func__, NULL);
1994  boxa = boxaReadStream(fp);
1995  fclose(fp);
1996  if (!boxa)
1997  return (BOXA *)ERROR_PTR("boxa not read", __func__, NULL);
1998  return boxa;
1999 }
2000 
2001 
2013 BOXA *
2015 {
2016 l_int32 n, i, x, y, w, h, version;
2017 l_int32 ignore;
2018 BOX *box;
2019 BOXA *boxa;
2020 
2021  if (!fp)
2022  return (BOXA *)ERROR_PTR("stream not defined", __func__, NULL);
2023 
2024  if (fscanf(fp, "\nBoxa Version %d\n", &version) != 1)
2025  return (BOXA *)ERROR_PTR("not a boxa file", __func__, NULL);
2026  if (version != BOXA_VERSION_NUMBER)
2027  return (BOXA *)ERROR_PTR("invalid boxa version", __func__, NULL);
2028  if (fscanf(fp, "Number of boxes = %d\n", &n) != 1)
2029  return (BOXA *)ERROR_PTR("not a boxa file", __func__, NULL);
2030  if (n < 0)
2031  return (BOXA *)ERROR_PTR("num box ptrs < 0", __func__, NULL);
2032  if (n > MaxBoxaPtrArraySize)
2033  return (BOXA *)ERROR_PTR("too many box ptrs", __func__, NULL);
2034  if (n == 0) L_INFO("the boxa is empty\n", __func__);
2035 
2036  if ((boxa = boxaCreate(n)) == NULL)
2037  return (BOXA *)ERROR_PTR("boxa not made", __func__, NULL);
2038  for (i = 0; i < n; i++) {
2039  if (fscanf(fp, " Box[%d]: x = %d, y = %d, w = %d, h = %d\n",
2040  &ignore, &x, &y, &w, &h) != 5) {
2041  boxaDestroy(&boxa);
2042  return (BOXA *)ERROR_PTR("box descr not valid", __func__, NULL);
2043  }
2044  box = boxCreate(x, y, w, h);
2045  boxaAddBox(boxa, box, L_INSERT);
2046  }
2047  return boxa;
2048 }
2049 
2050 
2058 BOXA *
2059 boxaReadMem(const l_uint8 *data,
2060  size_t size)
2061 {
2062 FILE *fp;
2063 BOXA *boxa;
2064 
2065  if (!data)
2066  return (BOXA *)ERROR_PTR("data not defined", __func__, NULL);
2067  if ((fp = fopenReadFromMemory(data, size)) == NULL)
2068  return (BOXA *)ERROR_PTR("stream not opened", __func__, NULL);
2069 
2070  boxa = boxaReadStream(fp);
2071  fclose(fp);
2072  if (!boxa) L_ERROR("boxa not read\n", __func__);
2073  return boxa;
2074 }
2075 
2076 
2093 l_ok
2094 boxaWriteDebug(const char *filename,
2095  BOXA *boxa)
2096 {
2097  if (LeptDebugOK) {
2098  return boxaWrite(filename, boxa);
2099  } else {
2100  L_INFO("write to named temp file %s is disabled\n", __func__, filename);
2101  return 0;
2102  }
2103 }
2104 
2105 
2113 l_ok
2114 boxaWrite(const char *filename,
2115  BOXA *boxa)
2116 {
2117 l_int32 ret;
2118 FILE *fp;
2119 
2120  if (!filename)
2121  return ERROR_INT("filename not defined", __func__, 1);
2122  if (!boxa)
2123  return ERROR_INT("boxa not defined", __func__, 1);
2124 
2125  if ((fp = fopenWriteStream(filename, "w")) == NULL)
2126  return ERROR_INT("stream not opened", __func__, 1);
2127  ret = boxaWriteStream(fp, boxa);
2128  fclose(fp);
2129  if (ret)
2130  return ERROR_INT("boxa not written to stream", __func__, 1);
2131 
2132  return 0;
2133 }
2134 
2135 
2143 l_ok
2145  BOXA *boxa)
2146 {
2147 l_int32 n, i;
2148 BOX *box;
2149 
2150  if (!boxa)
2151  return ERROR_INT("boxa not defined", __func__, 1);
2152  if (!fp)
2153  return boxaWriteStderr(boxa);
2154 
2155  n = boxaGetCount(boxa);
2156  fprintf(fp, "\nBoxa Version %d\n", BOXA_VERSION_NUMBER);
2157  fprintf(fp, "Number of boxes = %d\n", n);
2158  for (i = 0; i < n; i++) {
2159  if ((box = boxaGetBox(boxa, i, L_CLONE)) == NULL)
2160  return ERROR_INT("box not found", __func__, 1);
2161  fprintf(fp, " Box[%d]: x = %d, y = %d, w = %d, h = %d\n",
2162  i, box->x, box->y, box->w, box->h);
2163  boxDestroy(&box);
2164  }
2165  return 0;
2166 }
2167 
2168 
2175 l_ok
2177 {
2178 l_int32 n, i;
2179 BOX *box;
2180 
2181  if (!boxa)
2182  return ERROR_INT("boxa not defined", __func__, 1);
2183 
2184  n = boxaGetCount(boxa);
2185  lept_stderr("\nBoxa Version %d\n", BOXA_VERSION_NUMBER);
2186  lept_stderr("Number of boxes = %d\n", n);
2187  for (i = 0; i < n; i++) {
2188  if ((box = boxaGetBox(boxa, i, L_CLONE)) == NULL)
2189  return ERROR_INT("box not found", __func__, 1);
2190  lept_stderr(" Box[%d]: x = %d, y = %d, w = %d, h = %d\n",
2191  i, box->x, box->y, box->w, box->h);
2192  boxDestroy(&box);
2193  }
2194  return 0;
2195 }
2196 
2197 
2211 l_ok
2212 boxaWriteMem(l_uint8 **pdata,
2213  size_t *psize,
2214  BOXA *boxa)
2215 {
2216 l_int32 ret;
2217 FILE *fp;
2218 
2219  if (pdata) *pdata = NULL;
2220  if (psize) *psize = 0;
2221  if (!pdata)
2222  return ERROR_INT("&data not defined", __func__, 1);
2223  if (!psize)
2224  return ERROR_INT("&size not defined", __func__, 1);
2225  if (!boxa)
2226  return ERROR_INT("boxa not defined", __func__, 1);
2227 
2228 #if HAVE_FMEMOPEN
2229  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
2230  return ERROR_INT("stream not opened", __func__, 1);
2231  ret = boxaWriteStream(fp, boxa);
2232  fputc('\0', fp);
2233  fclose(fp);
2234  *psize = *psize - 1;
2235 #else
2236  L_INFO("work-around: writing to a temp file\n", __func__);
2237  #ifdef _WIN32
2238  if ((fp = fopenWriteWinTempfile()) == NULL)
2239  return ERROR_INT("tmpfile stream not opened", __func__, 1);
2240  #else
2241  if ((fp = tmpfile()) == NULL)
2242  return ERROR_INT("tmpfile stream not opened", __func__, 1);
2243  #endif /* _WIN32 */
2244  ret = boxaWriteStream(fp, boxa);
2245  rewind(fp);
2246  *pdata = l_binaryReadStream(fp, psize);
2247  fclose(fp);
2248 #endif /* HAVE_FMEMOPEN */
2249  return ret;
2250 }
2251 
2252 
2253 /*---------------------------------------------------------------------*
2254  * Debug printing *
2255  *---------------------------------------------------------------------*/
2269 l_ok
2271  BOX *box)
2272 {
2273  if (!box)
2274  return ERROR_INT("box not defined", __func__, 1);
2275 
2276  if (!fp) { /* output to stderr */
2277  lept_stderr(" Box: x = %d, y = %d, w = %d, h = %d\n",
2278  box->x, box->y, box->w, box->h);
2279  } else {
2280  fprintf(fp, " Box: x = %d, y = %d, w = %d, h = %d\n",
2281  box->x, box->y, box->w, box->h);
2282  }
2283  return 0;
2284 }
l_ok boxaWriteMem(l_uint8 **pdata, size_t *psize, BOXA *boxa)
boxaWriteMem()
Definition: boxbasic.c:2212
l_ok boxaaInitFull(BOXAA *baa, BOXA *boxa)
boxaaInitFull()
Definition: boxbasic.c:1464
l_int32 boxaGetValidCount(BOXA *boxa)
boxaGetValidCount()
Definition: boxbasic.c:676
BOXA * boxaSaveValid(BOXA *boxas, l_int32 copyflag)
boxaSaveValid()
Definition: boxbasic.c:1023
l_ok boxGetGeometry(const BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
Definition: boxbasic.c:301
l_ok boxaaReplaceBoxa(BOXAA *baa, l_int32 index, BOXA *boxa)
boxaaReplaceBoxa()
Definition: boxbasic.c:1542
l_ok boxaClear(BOXA *boxa)
boxaClear()
Definition: boxbasic.c:1120
l_ok boxaaWrite(const char *filename, BOXAA *baa)
boxaaWrite()
Definition: boxbasic.c:1859
BOXAA * boxaaReadFromFiles(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
boxaaReadFromFiles()
Definition: boxbasic.c:1713
BOXA * boxaRead(const char *filename)
boxaRead()
Definition: boxbasic.c:1984
l_ok boxaaExtendArray(BOXAA *baa)
boxaaExtendArray()
Definition: boxbasic.c:1284
BOXA * boxaReadStream(FILE *fp)
boxaReadStream()
Definition: boxbasic.c:2014
l_ok boxaInitFull(BOXA *boxa, BOX *box)
boxaInitFull()
Definition: boxbasic.c:1085
l_ok boxaIsFull(BOXA *boxa, l_int32 *pfull)
boxaIsFull()
Definition: boxbasic.c:830
BOXA * boxaCopy(BOXA *boxa, l_int32 copyflag)
boxaCopy()
Definition: boxbasic.c:475
l_ok boxaInsertBox(BOXA *boxa, l_int32 index, BOX *box)
boxaInsertBox()
Definition: boxbasic.c:911
BOX * boxCreateValid(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreateValid()
Definition: boxbasic.c:212
void boxDestroy(BOX **pbox)
boxDestroy()
Definition: boxbasic.c:273
l_ok boxaaRemoveBoxa(BOXAA *baa, l_int32 index)
boxaaRemoveBoxa()
Definition: boxbasic.c:1629
l_ok boxaRemoveBoxAndSave(BOXA *boxa, l_int32 index, BOX **pbox)
boxaRemoveBoxAndSave()
Definition: boxbasic.c:981
l_ok boxaRemoveBox(BOXA *boxa, l_int32 index)
boxaRemoveBox()
Definition: boxbasic.c:957
BOXAA * boxaaReadMem(const l_uint8 *data, size_t size)
boxaaReadMem()
Definition: boxbasic.c:1833
BOX * boxClone(BOX *box)
boxClone()
Definition: boxbasic.c:249
BOX * boxaaGetBox(BOXAA *baa, l_int32 iboxa, l_int32 ibox, l_int32 accessflag)
boxaaGetBox()
Definition: boxbasic.c:1414
BOXAA * boxaaCreate(l_int32 n)
boxaaCreate()
Definition: boxbasic.c:1145
l_ok boxaReplaceBox(BOXA *boxa, l_int32 index, BOX *box)
boxaReplaceBox()
Definition: boxbasic.c:875
l_ok boxGetSideLocations(const BOX *box, l_int32 *pl, l_int32 *pr, l_int32 *pt, l_int32 *pb)
boxGetSideLocations()
Definition: boxbasic.c:358
l_int32 boxaaGetCount(BOXAA *baa)
boxaaGetCount()
Definition: boxbasic.c:1343
l_ok boxaGetBoxGeometry(BOXA *boxa, l_int32 index, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxaGetBoxGeometry()
Definition: boxbasic.c:796
BOXAA * boxaaCopy(BOXAA *baas, l_int32 copyflag)
boxaaCopy()
Definition: boxbasic.c:1178
l_ok boxaWriteDebug(const char *filename, BOXA *boxa)
boxaWriteDebug()
Definition: boxbasic.c:2094
l_ok boxaaExtendWithInit(BOXAA *baa, l_int32 maxindex, BOXA *boxa)
boxaaExtendWithInit()
Definition: boxbasic.c:1502
static const size_t InitialPtrArraySize
Definition: boxbasic.c:143
l_ok boxaWriteStderr(BOXA *boxa)
boxaWriteStderr()
Definition: boxbasic.c:2176
l_ok boxaaAddBoxa(BOXAA *baa, BOXA *ba, l_int32 copyflag)
boxaaAddBoxa()
Definition: boxbasic.c:1241
l_ok boxaaWriteStream(FILE *fp, BOXAA *baa)
boxaaWriteStream()
Definition: boxbasic.c:1888
l_ok boxaExtendArrayToSize(BOXA *boxa, size_t size)
boxaExtendArrayToSize()
Definition: boxbasic.c:625
l_ok boxaWrite(const char *filename, BOXA *boxa)
boxaWrite()
Definition: boxbasic.c:2114
l_ok boxaAddBox(BOXA *boxa, BOX *box, l_int32 copyflag)
boxaAddBox()
Definition: boxbasic.c:553
l_ok boxaExtendArray(BOXA *boxa)
boxaExtendArray()
Definition: boxbasic.c:602
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
Definition: boxbasic.c:519
l_int32 boxaGetCount(const BOXA *boxa)
boxaGetCount()
Definition: boxbasic.c:661
l_ok boxIsValid(BOX *box, l_int32 *pvalid)
boxIsValid()
Definition: boxbasic.c:417
l_ok boxSetGeometry(BOX *box, l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxSetGeometry()
Definition: boxbasic.c:329
BOX * boxaGetValidBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetValidBox()
Definition: boxbasic.c:739
BOXA * boxaaGetBoxa(BOXAA *baa, l_int32 index, l_int32 accessflag)
boxaaGetBoxa()
Definition: boxbasic.c:1386
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
Definition: boxbasic.c:702
BOXA * boxaReadMem(const l_uint8 *data, size_t size)
boxaReadMem()
Definition: boxbasic.c:2059
BOX * boxCopy(BOX *box)
boxCopy()
Definition: boxbasic.c:230
void boxaaDestroy(BOXAA **pbaa)
boxaaDestroy()
Definition: boxbasic.c:1207
NUMA * boxaFindInvalidBoxes(BOXA *boxa)
boxaFindInvalidBoxes()
Definition: boxbasic.c:765
l_int32 boxaaGetBoxCount(BOXAA *baa)
boxaaGetBoxCount()
Definition: boxbasic.c:1358
l_ok boxaaExtendArrayToSize(BOXAA *baa, l_int32 size)
boxaaExtendArrayToSize()
Definition: boxbasic.c:1307
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
Definition: boxbasic.c:171
l_ok boxaaInsertBoxa(BOXAA *baa, l_int32 index, BOXA *boxa)
boxaaInsertBoxa()
Definition: boxbasic.c:1582
BOXAA * boxaaReadStream(FILE *fp)
boxaaReadStream()
Definition: boxbasic.c:1785
l_ok boxSetSideLocations(BOX *box, l_int32 l, l_int32 r, l_int32 t, l_int32 b)
boxSetSideLocations()
Definition: boxbasic.c:390
BOXA * boxaCreate(l_int32 n)
boxaCreate()
Definition: boxbasic.c:442
l_ok boxaWriteStream(FILE *fp, BOXA *boxa)
boxaWriteStream()
Definition: boxbasic.c:2144
l_ok boxPrintStreamInfo(FILE *fp, BOX *box)
boxPrintStreamInfo()
Definition: boxbasic.c:2270
BOXAA * boxaaRead(const char *filename)
boxaaRead()
Definition: boxbasic.c:1755
l_ok boxaaAddBox(BOXAA *baa, l_int32 index, BOX *box, l_int32 accessflag)
boxaaAddBox()
Definition: boxbasic.c:1667
l_ok boxaaWriteMem(l_uint8 **pdata, size_t *psize, BOXAA *baa)
boxaaWriteMem()
Definition: boxbasic.c:1933
l_ok boxaGetExtent(BOXA *boxa, l_int32 *pw, l_int32 *ph, BOX **pbox)
boxaGetExtent()
Definition: boxfunc4.c:922
l_ok numaSetValue(NUMA *na, l_int32 index, l_float32 val)
numaSetValue()
Definition: numabasic.c:750
NUMA * numaMakeConstant(l_float32 val, l_int32 size)
numaMakeConstant()
Definition: numafunc1.c:820
@ L_COPY
Definition: pix.h:505
@ L_CLONE
Definition: pix.h:506
@ L_COPY_CLONE
Definition: pix.h:507
@ L_NOCOPY
Definition: pix.h:503
@ L_INSERT
Definition: pix.h:504
#define BOXAA_VERSION_NUMBER
Definition: pix_internal.h:229
#define BOXA_VERSION_NUMBER
Definition: pix_internal.h:228
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:673
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:617
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:353
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
Definition: sarray1.c:1739
l_int32 y
Definition: pix_internal.h:258
l_int32 x
Definition: pix_internal.h:257
l_int32 w
Definition: pix_internal.h:259
l_int32 h
Definition: pix_internal.h:260
l_atomic refcount
Definition: pix_internal.h:261
l_int32 nalloc
Definition: pix_internal.h:268
l_int32 n
Definition: pix_internal.h:267
l_atomic refcount
Definition: pix_internal.h:269
struct Box ** box
Definition: pix_internal.h:270
l_int32 nalloc
Definition: pix_internal.h:277
struct Boxa ** boxa
Definition: pix_internal.h:278
l_int32 n
Definition: pix_internal.h:276
void lept_stderr(const char *fmt,...)
lept_stderr()
Definition: utils1.c:306
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
Definition: utils2.c:1358
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
Definition: utils2.c:1937
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1905
void * reallocNew(void **pindata, size_t oldsize, size_t newsize)
reallocNew()
Definition: utils2.c:1262
FILE * fopenWriteWinTempfile(void)
fopenWriteWinTempfile()
Definition: utils2.c:1981
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1864