Leptonica  1.77.0
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 
133 #include <string.h>
134 #include "allheaders.h"
135 
136 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20;
139 /*---------------------------------------------------------------------*
140  * Box creation, destruction and copy *
141  *---------------------------------------------------------------------*/
164 BOX *
165 boxCreate(l_int32 x,
166  l_int32 y,
167  l_int32 w,
168  l_int32 h)
169 {
170 BOX *box;
171 
172  PROCNAME("boxCreate");
173 
174  if (w < 0 || h < 0)
175  return (BOX *)ERROR_PTR("w and h not both >= 0", procName, NULL);
176  if (x < 0) { /* take part in +quad */
177  w = w + x;
178  x = 0;
179  if (w <= 0)
180  return (BOX *)ERROR_PTR("x < 0 and box off +quad", procName, NULL);
181  }
182  if (y < 0) { /* take part in +quad */
183  h = h + y;
184  y = 0;
185  if (h <= 0)
186  return (BOX *)ERROR_PTR("y < 0 and box off +quad", procName, NULL);
187  }
188 
189  if ((box = (BOX *)LEPT_CALLOC(1, sizeof(BOX))) == NULL)
190  return (BOX *)ERROR_PTR("box not made", procName, NULL);
191  boxSetGeometry(box, x, y, w, h);
192  box->refcount = 1;
193 
194  return box;
195 }
196 
197 
209 BOX *
210 boxCreateValid(l_int32 x,
211  l_int32 y,
212  l_int32 w,
213  l_int32 h)
214 {
215  PROCNAME("boxCreateValid");
216 
217  if (w <= 0 || h <= 0)
218  return (BOX *)ERROR_PTR("w and h not both > 0", procName, NULL);
219  return boxCreate(x, y, w, h);
220 }
221 
222 
229 BOX *
231 {
232 BOX *boxc;
233 
234  PROCNAME("boxCopy");
235 
236  if (!box)
237  return (BOX *)ERROR_PTR("box not defined", procName, NULL);
238 
239  boxc = boxCreate(box->x, box->y, box->w, box->h);
240 
241  return boxc;
242 }
243 
244 
251 BOX *
253 {
254 
255  PROCNAME("boxClone");
256 
257  if (!box)
258  return (BOX *)ERROR_PTR("box not defined", procName, NULL);
259 
260  boxChangeRefcount(box, 1);
261  return box;
262 }
263 
264 
277 void
279 {
280 BOX *box;
281 
282  PROCNAME("boxDestroy");
283 
284  if (pbox == NULL) {
285  L_WARNING("ptr address is null!\n", procName);
286  return;
287  }
288  if ((box = *pbox) == NULL)
289  return;
290 
291  boxChangeRefcount(box, -1);
292  if (boxGetRefcount(box) <= 0)
293  LEPT_FREE(box);
294  *pbox = NULL;
295  return;
296 }
297 
298 
299 /*---------------------------------------------------------------------*
300  * Box accessors *
301  *---------------------------------------------------------------------*/
309 l_ok
311  l_int32 *px,
312  l_int32 *py,
313  l_int32 *pw,
314  l_int32 *ph)
315 {
316  PROCNAME("boxGetGeometry");
317 
318  if (px) *px = 0;
319  if (py) *py = 0;
320  if (pw) *pw = 0;
321  if (ph) *ph = 0;
322  if (!box)
323  return ERROR_INT("box not defined", procName, 1);
324  if (px) *px = box->x;
325  if (py) *py = box->y;
326  if (pw) *pw = box->w;
327  if (ph) *ph = box->h;
328  return 0;
329 }
330 
331 
339 l_ok
341  l_int32 x,
342  l_int32 y,
343  l_int32 w,
344  l_int32 h)
345 {
346  PROCNAME("boxSetGeometry");
347 
348  if (!box)
349  return ERROR_INT("box not defined", procName, 1);
350  if (x != -1) box->x = x;
351  if (y != -1) box->y = y;
352  if (w != -1) box->w = w;
353  if (h != -1) box->h = h;
354  return 0;
355 }
356 
357 
370 l_ok
372  l_int32 *pl,
373  l_int32 *pr,
374  l_int32 *pt,
375  l_int32 *pb)
376 {
377 l_int32 x, y, w, h;
378 
379  PROCNAME("boxGetSideLocations");
380 
381  if (pl) *pl = 0;
382  if (pr) *pr = 0;
383  if (pt) *pt = 0;
384  if (pb) *pb = 0;
385  if (!box)
386  return ERROR_INT("box not defined", procName, 1);
387 
388  boxGetGeometry(box, &x, &y, &w, &h);
389  if (pl) *pl = x;
390  if (pr) *pr = x + w - 1;
391  if (pt) *pt = y;
392  if (pb) *pb = y + h - 1;
393  return 0;
394 }
395 
396 
404 l_ok
406  l_int32 l,
407  l_int32 r,
408  l_int32 t,
409  l_int32 b)
410 {
411 l_int32 x, y, w, h;
412 
413  PROCNAME("boxSetSideLocations");
414 
415  if (!box)
416  return ERROR_INT("box not defined", procName, 1);
417  x = (l != -1) ? l : box->x;
418  w = (r != -1) ? r - x + 1 : box->x + box->w - x;
419  y = (t != -1) ? t : box->y;
420  h = (b != -1) ? b - y + 1 : box->y + box->h - y;
421  boxSetGeometry(box, x, y, w, h);
422  return 0;
423 }
424 
425 
432 l_int32
434 {
435  PROCNAME("boxGetRefcount");
436 
437  if (!box)
438  return ERROR_INT("box not defined", procName, UNDEF);
439 
440  return box->refcount;
441 }
442 
450 l_ok
452  l_int32 delta)
453 {
454  PROCNAME("boxChangeRefcount");
455 
456  if (!box)
457  return ERROR_INT("box not defined", procName, 1);
458 
459  box->refcount += delta;
460  return 0;
461 }
462 
463 
471 l_ok
473  l_int32 *pvalid)
474 {
475  PROCNAME("boxIsValid");
476 
477  if (!pvalid)
478  return ERROR_INT("&valid not defined", procName, 1);
479  *pvalid = 0;
480  if (!box)
481  return ERROR_INT("box not defined", procName, 1);
482 
483  if (box->w > 0 && box->h > 0)
484  *pvalid = 1;
485  return 0;
486 }
487 
488 
489 /*---------------------------------------------------------------------*
490  * Boxa creation, destruction, copy, extension *
491  *---------------------------------------------------------------------*/
498 BOXA *
499 boxaCreate(l_int32 n)
500 {
501 BOXA *boxa;
502 
503  PROCNAME("boxaCreate");
504 
505  if (n <= 0)
507 
508  boxa = (BOXA *)LEPT_CALLOC(1, sizeof(BOXA));
509  boxa->n = 0;
510  boxa->nalloc = n;
511  boxa->refcount = 1;
512  if ((boxa->box = (BOX **)LEPT_CALLOC(n, sizeof(BOX *))) == NULL) {
513  boxaDestroy(&boxa);
514  return (BOXA *)ERROR_PTR("boxa ptrs not made", procName, NULL);
515  }
516  return boxa;
517 }
518 
519 
533 BOXA *
535  l_int32 copyflag)
536 {
537 l_int32 i;
538 BOX *boxc;
539 BOXA *boxac;
540 
541  PROCNAME("boxaCopy");
542 
543  if (!boxa)
544  return (BOXA *)ERROR_PTR("boxa not defined", procName, NULL);
545 
546  if (copyflag == L_CLONE) {
547  boxa->refcount++;
548  return boxa;
549  }
550 
551  if (copyflag != L_COPY && copyflag != L_COPY_CLONE)
552  return (BOXA *)ERROR_PTR("invalid copyflag", procName, NULL);
553 
554  if ((boxac = boxaCreate(boxa->nalloc)) == NULL)
555  return (BOXA *)ERROR_PTR("boxac not made", procName, NULL);
556  for (i = 0; i < boxa->n; i++) {
557  if (copyflag == L_COPY)
558  boxc = boxaGetBox(boxa, i, L_COPY);
559  else /* copy-clone */
560  boxc = boxaGetBox(boxa, i, L_CLONE);
561  boxaAddBox(boxac, boxc, L_INSERT);
562  }
563  return boxac;
564 }
565 
566 
579 void
581 {
582 l_int32 i;
583 BOXA *boxa;
584 
585  PROCNAME("boxaDestroy");
586 
587  if (pboxa == NULL) {
588  L_WARNING("ptr address is null!\n", procName);
589  return;
590  }
591 
592  if ((boxa = *pboxa) == NULL)
593  return;
594 
595  /* Decrement the ref count. If it is 0, destroy the boxa. */
596  boxa->refcount--;
597  if (boxa->refcount <= 0) {
598  for (i = 0; i < boxa->n; i++)
599  boxDestroy(&boxa->box[i]);
600  LEPT_FREE(boxa->box);
601  LEPT_FREE(boxa);
602  }
603 
604  *pboxa = NULL;
605  return;
606 }
607 
608 
617 l_ok
619  BOX *box,
620  l_int32 copyflag)
621 {
622 l_int32 n;
623 BOX *boxc;
624 
625  PROCNAME("boxaAddBox");
626 
627  if (!boxa)
628  return ERROR_INT("boxa not defined", procName, 1);
629  if (!box)
630  return ERROR_INT("box not defined", procName, 1);
631 
632  if (copyflag == L_INSERT)
633  boxc = box;
634  else if (copyflag == L_COPY)
635  boxc = boxCopy(box);
636  else if (copyflag == L_CLONE)
637  boxc = boxClone(box);
638  else
639  return ERROR_INT("invalid copyflag", procName, 1);
640  if (!boxc)
641  return ERROR_INT("boxc not made", procName, 1);
642 
643  n = boxaGetCount(boxa);
644  if (n >= boxa->nalloc)
645  boxaExtendArray(boxa);
646  boxa->box[n] = boxc;
647  boxa->n++;
648 
649  return 0;
650 }
651 
652 
664 l_ok
666 {
667  PROCNAME("boxaExtendArray");
668 
669  if (!boxa)
670  return ERROR_INT("boxa not defined", procName, 1);
671 
672  return boxaExtendArrayToSize(boxa, 2 * boxa->nalloc);
673 }
674 
675 
688 l_ok
690  l_int32 size)
691 {
692  PROCNAME("boxaExtendArrayToSize");
693 
694  if (!boxa)
695  return ERROR_INT("boxa not defined", procName, 1);
696 
697  if (size > boxa->nalloc) {
698  if ((boxa->box = (BOX **)reallocNew((void **)&boxa->box,
699  sizeof(BOX *) * boxa->nalloc,
700  size * sizeof(BOX *))) == NULL)
701  return ERROR_INT("new ptr array not returned", procName, 1);
702  boxa->nalloc = size;
703  }
704  return 0;
705 }
706 
707 
708 /*---------------------------------------------------------------------*
709  * Boxa accessors *
710  *---------------------------------------------------------------------*/
717 l_int32
719 {
720  PROCNAME("boxaGetCount");
721 
722  if (!boxa)
723  return ERROR_INT("boxa not defined", procName, 0);
724  return boxa->n;
725 }
726 
727 
734 l_int32
736 {
737 l_int32 n, i, w, h, count;
738 
739  PROCNAME("boxaGetValidCount");
740 
741  if (!boxa)
742  return ERROR_INT("boxa not defined", procName, 0);
743 
744  n = boxaGetCount(boxa);
745  for (i = 0, count = 0; i < n; i++) {
746  boxaGetBoxGeometry(boxa, i, NULL, NULL, &w, &h);
747  if (w > 0 && h > 0)
748  count++;
749  }
750  return count;
751 }
752 
753 
762 BOX *
764  l_int32 index,
765  l_int32 accessflag)
766 {
767  PROCNAME("boxaGetBox");
768 
769  if (!boxa)
770  return (BOX *)ERROR_PTR("boxa not defined", procName, NULL);
771  if (index < 0 || index >= boxa->n)
772  return (BOX *)ERROR_PTR("index not valid", procName, NULL);
773 
774  if (accessflag == L_COPY)
775  return boxCopy(boxa->box[index]);
776  else if (accessflag == L_CLONE)
777  return boxClone(boxa->box[index]);
778  else
779  return (BOX *)ERROR_PTR("invalid accessflag", procName, NULL);
780 }
781 
782 
801 BOX *
803  l_int32 index,
804  l_int32 accessflag)
805 {
806 l_int32 w, h;
807 BOX *box;
808 
809  PROCNAME("boxaGetValidBox");
810 
811  if (!boxa)
812  return (BOX *)ERROR_PTR("boxa not defined", procName, NULL);
813 
814  if ((box = boxaGetBox(boxa, index, accessflag)) == NULL)
815  return (BOX *)ERROR_PTR("box not returned", procName, NULL);
816  boxGetGeometry(box, NULL, NULL, &w, &h);
817  if (w <= 0 || h <= 0) /* not valid, but not necessarily an error */
818  boxDestroy(&box);
819  return box;
820 }
821 
822 
829 NUMA *
831 {
832 l_int32 i, n, w, h;
833 NUMA *na;
834 
835  PROCNAME("boxaFindInvalidBoxes");
836 
837  if (!boxa)
838  return (NUMA *)ERROR_PTR("boxa not defined", procName, NULL);
839 
840  n = boxaGetCount(boxa);
841  if (boxaGetValidCount(boxa) == n)
842  return NULL;
843 
844  na = numaMakeConstant(0, n);
845  for (i = 0; i < n; i++) {
846  boxaGetBoxGeometry(boxa, i, NULL, NULL, &w, &h);
847  if (w == 0 || h == 0)
848  numaSetValue(na, i, 1);
849  }
850  return na;
851 }
852 
853 
862 l_ok
864  l_int32 index,
865  l_int32 *px,
866  l_int32 *py,
867  l_int32 *pw,
868  l_int32 *ph)
869 {
870 BOX *box;
871 
872  PROCNAME("boxaGetBoxGeometry");
873 
874  if (px) *px = 0;
875  if (py) *py = 0;
876  if (pw) *pw = 0;
877  if (ph) *ph = 0;
878  if (!boxa)
879  return ERROR_INT("boxa not defined", procName, 1);
880  if (index < 0 || index >= boxa->n)
881  return ERROR_INT("index not valid", procName, 1);
882 
883  if ((box = boxaGetBox(boxa, index, L_CLONE)) == NULL)
884  return ERROR_INT("box not found!", procName, 1);
885  boxGetGeometry(box, px, py, pw, ph);
886  boxDestroy(&box);
887  return 0;
888 }
889 
890 
898 l_ok
900  l_int32 *pfull)
901 {
902 l_int32 i, n, full;
903 BOX *box;
904 
905  PROCNAME("boxaIsFull");
906 
907  if (!pfull)
908  return ERROR_INT("&full not defined", procName, 1);
909  *pfull = 0;
910  if (!boxa)
911  return ERROR_INT("boxa not defined", procName, 1);
912 
913  n = boxaGetCount(boxa);
914  full = 1;
915  for (i = 0; i < n; i++) {
916  if ((box = boxaGetBox(boxa, i, L_CLONE)) == NULL) {
917  full = 0;
918  break;
919  }
920  boxDestroy(&box);
921  }
922  *pfull = full;
923  return 0;
924 }
925 
926 
927 /*---------------------------------------------------------------------*
928  * Boxa array modifiers *
929  *---------------------------------------------------------------------*/
945 l_ok
947  l_int32 index,
948  BOX *box)
949 {
950  PROCNAME("boxaReplaceBox");
951 
952  if (!boxa)
953  return ERROR_INT("boxa not defined", procName, 1);
954  if (index < 0 || index >= boxa->n)
955  return ERROR_INT("index not valid", procName, 1);
956  if (!box)
957  return ERROR_INT("box not defined", procName, 1);
958 
959  boxDestroy(&(boxa->box[index]));
960  boxa->box[index] = box;
961  return 0;
962 }
963 
964 
983 l_ok
985  l_int32 index,
986  BOX *box)
987 {
988 l_int32 i, n;
989 BOX **array;
990 
991  PROCNAME("boxaInsertBox");
992 
993  if (!boxa)
994  return ERROR_INT("boxa not defined", procName, 1);
995  n = boxaGetCount(boxa);
996  if (index < 0 || index > n)
997  return ERROR_INT("index not in {0...n}", procName, 1);
998  if (!box)
999  return ERROR_INT("box not defined", procName, 1);
1000 
1001  if (n >= boxa->nalloc)
1002  boxaExtendArray(boxa);
1003  array = boxa->box;
1004  boxa->n++;
1005  for (i = n; i > index; i--)
1006  array[i] = array[i - 1];
1007  array[index] = box;
1008 
1009  return 0;
1010 }
1011 
1012 
1028 l_ok
1030  l_int32 index)
1031 {
1032  return boxaRemoveBoxAndSave(boxa, index, NULL);
1033 }
1034 
1035 
1052 l_ok
1054  l_int32 index,
1055  BOX **pbox)
1056 {
1057 l_int32 i, n;
1058 BOX **array;
1059 
1060  PROCNAME("boxaRemoveBoxAndSave");
1061 
1062  if (pbox) *pbox = NULL;
1063  if (!boxa)
1064  return ERROR_INT("boxa not defined", procName, 1);
1065  n = boxaGetCount(boxa);
1066  if (index < 0 || index >= n)
1067  return ERROR_INT("index not in {0...n - 1}", procName, 1);
1068 
1069  if (pbox)
1070  *pbox = boxaGetBox(boxa, index, L_CLONE);
1071  array = boxa->box;
1072  boxDestroy(&array[index]);
1073  for (i = index + 1; i < n; i++)
1074  array[i - 1] = array[i];
1075  array[n - 1] = NULL;
1076  boxa->n--;
1077 
1078  return 0;
1079 }
1080 
1081 
1094 BOXA *
1096  l_int32 copyflag)
1097 {
1098 l_int32 i, n;
1099 BOX *box;
1100 BOXA *boxad;
1101 
1102  PROCNAME("boxaSaveValid");
1103 
1104  if (!boxas)
1105  return (BOXA *)ERROR_PTR("boxas not defined", procName, NULL);
1106  if (copyflag != L_COPY && copyflag != L_CLONE)
1107  return (BOXA *)ERROR_PTR("invalid copyflag", procName, NULL);
1108 
1109  n = boxaGetCount(boxas);
1110  boxad = boxaCreate(n);
1111  for (i = 0; i < n; i++) {
1112  if ((box = boxaGetValidBox(boxas, i, copyflag)) != NULL)
1113  boxaAddBox(boxad, box, L_INSERT);
1114  }
1115 
1116  return boxad;
1117 }
1118 
1119 
1158 l_ok
1160  BOX *box)
1161 {
1162 l_int32 i, n;
1163 BOX *boxt;
1164 
1165  PROCNAME("boxaInitFull");
1166 
1167  if (!boxa)
1168  return ERROR_INT("boxa not defined", procName, 1);
1169 
1170  n = boxa->nalloc;
1171  boxa->n = n;
1172  for (i = 0; i < n; i++) {
1173  if (box)
1174  boxt = boxCopy(box);
1175  else
1176  boxt = boxCreate(0, 0, 0, 0);
1177  boxaReplaceBox(boxa, i, boxt);
1178  }
1179  return 0;
1180 }
1181 
1182 
1195 l_ok
1197 {
1198 l_int32 i, n;
1199 
1200  PROCNAME("boxaClear");
1201 
1202  if (!boxa)
1203  return ERROR_INT("boxa not defined", procName, 1);
1204 
1205  n = boxaGetCount(boxa);
1206  for (i = 0; i < n; i++)
1207  boxDestroy(&boxa->box[i]);
1208  boxa->n = 0;
1209  return 0;
1210 }
1211 
1212 
1213 /*--------------------------------------------------------------------------*
1214  * Boxaa creation, destruction *
1215  *--------------------------------------------------------------------------*/
1222 BOXAA *
1223 boxaaCreate(l_int32 n)
1224 {
1225 BOXAA *baa;
1226 
1227  PROCNAME("boxaaCreate");
1228 
1229  if (n <= 0)
1231 
1232  baa = (BOXAA *)LEPT_CALLOC(1, sizeof(BOXAA));
1233  if ((baa->boxa = (BOXA **)LEPT_CALLOC(n, sizeof(BOXA *))) == NULL) {
1234  boxaaDestroy(&baa);
1235  return (BOXAA *)ERROR_PTR("boxa ptr array not made", procName, NULL);
1236  }
1237  baa->nalloc = n;
1238  baa->n = 0;
1239  return baa;
1240 }
1241 
1242 
1257 BOXAA *
1259  l_int32 copyflag)
1260 {
1261 l_int32 i, n;
1262 BOXA *boxa;
1263 BOXAA *baad;
1264 
1265  PROCNAME("boxaaCopy");
1266 
1267  if (!baas)
1268  return (BOXAA *)ERROR_PTR("baas not defined", procName, NULL);
1269  if (copyflag != L_COPY && copyflag != L_CLONE)
1270  return (BOXAA *)ERROR_PTR("invalid copyflag", procName, NULL);
1271 
1272  n = boxaaGetCount(baas);
1273  baad = boxaaCreate(n);
1274  for (i = 0; i < n; i++) {
1275  boxa = boxaaGetBoxa(baas, i, copyflag);
1276  boxaaAddBoxa(baad, boxa, L_INSERT);
1277  }
1278 
1279  return baad;
1280 }
1281 
1282 
1288 void
1290 {
1291 l_int32 i;
1292 BOXAA *baa;
1293 
1294  PROCNAME("boxaaDestroy");
1295 
1296  if (pbaa == NULL) {
1297  L_WARNING("ptr address is NULL!\n", procName);
1298  return;
1299  }
1300 
1301  if ((baa = *pbaa) == NULL)
1302  return;
1303 
1304  for (i = 0; i < baa->n; i++)
1305  boxaDestroy(&baa->boxa[i]);
1306  LEPT_FREE(baa->boxa);
1307  LEPT_FREE(baa);
1308  *pbaa = NULL;
1309 
1310  return;
1311 }
1312 
1313 
1314 
1315 /*--------------------------------------------------------------------------*
1316  * Add Boxa to Boxaa *
1317  *--------------------------------------------------------------------------*/
1326 l_ok
1328  BOXA *ba,
1329  l_int32 copyflag)
1330 {
1331 l_int32 n;
1332 BOXA *bac;
1333 
1334  PROCNAME("boxaaAddBoxa");
1335 
1336  if (!baa)
1337  return ERROR_INT("baa not defined", procName, 1);
1338  if (!ba)
1339  return ERROR_INT("ba not defined", procName, 1);
1340  if (copyflag != L_INSERT && copyflag != L_COPY && copyflag != L_CLONE)
1341  return ERROR_INT("invalid copyflag", procName, 1);
1342 
1343  if (copyflag == L_INSERT)
1344  bac = ba;
1345  else
1346  bac = boxaCopy(ba, copyflag);
1347 
1348  n = boxaaGetCount(baa);
1349  if (n >= baa->nalloc)
1350  boxaaExtendArray(baa);
1351  baa->boxa[n] = bac;
1352  baa->n++;
1353  return 0;
1354 }
1355 
1356 
1363 l_ok
1365 {
1366 
1367  PROCNAME("boxaaExtendArray");
1368 
1369  if (!baa)
1370  return ERROR_INT("baa not defined", procName, 1);
1371 
1372  if ((baa->boxa = (BOXA **)reallocNew((void **)&baa->boxa,
1373  sizeof(BOXA *) * baa->nalloc,
1374  2 * sizeof(BOXA *) * baa->nalloc)) == NULL)
1375  return ERROR_INT("new ptr array not returned", procName, 1);
1376 
1377  baa->nalloc *= 2;
1378  return 0;
1379 }
1380 
1381 
1394 l_ok
1396  l_int32 size)
1397 {
1398  PROCNAME("boxaaExtendArrayToSize");
1399 
1400  if (!baa)
1401  return ERROR_INT("baa not defined", procName, 1);
1402 
1403  if (size > baa->nalloc) {
1404  if ((baa->boxa = (BOXA **)reallocNew((void **)&baa->boxa,
1405  sizeof(BOXA *) * baa->nalloc,
1406  size * sizeof(BOXA *))) == NULL)
1407  return ERROR_INT("new ptr array not returned", procName, 1);
1408  baa->nalloc = size;
1409  }
1410  return 0;
1411 }
1412 
1413 
1414 /*----------------------------------------------------------------------*
1415  * Boxaa accessors *
1416  *----------------------------------------------------------------------*/
1423 l_int32
1425 {
1426  PROCNAME("boxaaGetCount");
1427 
1428  if (!baa)
1429  return ERROR_INT("baa not defined", procName, 0);
1430  return baa->n;
1431 }
1432 
1433 
1440 l_int32
1442 {
1443 BOXA *boxa;
1444 l_int32 n, sum, i;
1445 
1446  PROCNAME("boxaaGetBoxCount");
1447 
1448  if (!baa)
1449  return ERROR_INT("baa not defined", procName, 0);
1450 
1451  n = boxaaGetCount(baa);
1452  for (sum = 0, i = 0; i < n; i++) {
1453  boxa = boxaaGetBoxa(baa, i, L_CLONE);
1454  sum += boxaGetCount(boxa);
1455  boxaDestroy(&boxa);
1456  }
1457 
1458  return sum;
1459 }
1460 
1461 
1470 BOXA *
1472  l_int32 index,
1473  l_int32 accessflag)
1474 {
1475 l_int32 n;
1476 
1477  PROCNAME("boxaaGetBoxa");
1478 
1479  if (!baa)
1480  return (BOXA *)ERROR_PTR("baa not defined", procName, NULL);
1481  n = boxaaGetCount(baa);
1482  if (index < 0 || index >= n)
1483  return (BOXA *)ERROR_PTR("index not valid", procName, NULL);
1484  if (accessflag != L_COPY && accessflag != L_CLONE)
1485  return (BOXA *)ERROR_PTR("invalid accessflag", procName, NULL);
1486 
1487  return boxaCopy(baa->boxa[index], accessflag);
1488 }
1489 
1490 
1500 BOX *
1502  l_int32 iboxa,
1503  l_int32 ibox,
1504  l_int32 accessflag)
1505 {
1506 BOX *box;
1507 BOXA *boxa;
1508 
1509  PROCNAME("boxaaGetBox");
1510 
1511  if ((boxa = boxaaGetBoxa(baa, iboxa, L_CLONE)) == NULL)
1512  return (BOX *)ERROR_PTR("boxa not retrieved", procName, NULL);
1513  if ((box = boxaGetBox(boxa, ibox, accessflag)) == NULL)
1514  L_ERROR("box not retrieved\n", procName);
1515  boxaDestroy(&boxa);
1516  return box;
1517 }
1518 
1519 
1520 /*----------------------------------------------------------------------*
1521  * Boxaa array modifiers *
1522  *----------------------------------------------------------------------*/
1552 l_ok
1554  BOXA *boxa)
1555 {
1556 l_int32 i, n;
1557 BOXA *boxat;
1558 
1559  PROCNAME("boxaaInitFull");
1560 
1561  if (!baa)
1562  return ERROR_INT("baa not defined", procName, 1);
1563  if (!boxa)
1564  return ERROR_INT("boxa not defined", procName, 1);
1565 
1566  n = baa->nalloc;
1567  baa->n = n;
1568  for (i = 0; i < n; i++) {
1569  boxat = boxaCopy(boxa, L_COPY);
1570  boxaaReplaceBoxa(baa, i, boxat);
1571  }
1572  return 0;
1573 }
1574 
1575 
1592 l_ok
1594  l_int32 maxindex,
1595  BOXA *boxa)
1596 {
1597 l_int32 i, n;
1598 
1599  PROCNAME("boxaaExtendWithInit");
1600 
1601  if (!baa)
1602  return ERROR_INT("baa not defined", procName, 1);
1603  if (!boxa)
1604  return ERROR_INT("boxa not defined", procName, 1);
1605 
1606  /* Extend the ptr array if necessary */
1607  n = boxaaGetCount(baa);
1608  if (maxindex < n) return 0;
1609  boxaaExtendArrayToSize(baa, maxindex + 1);
1610 
1611  /* Fill the new entries with copies of boxa */
1612  for (i = n; i <= maxindex; i++)
1613  boxaaAddBoxa(baa, boxa, L_COPY);
1614  return 0;
1615 }
1616 
1617 
1633 l_ok
1635  l_int32 index,
1636  BOXA *boxa)
1637 {
1638 l_int32 n;
1639 
1640  PROCNAME("boxaaReplaceBoxa");
1641 
1642  if (!baa)
1643  return ERROR_INT("baa not defined", procName, 1);
1644  if (!boxa)
1645  return ERROR_INT("boxa not defined", procName, 1);
1646  n = boxaaGetCount(baa);
1647  if (index < 0 || index >= n)
1648  return ERROR_INT("index not valid", procName, 1);
1649 
1650  boxaDestroy(&baa->boxa[index]);
1651  baa->boxa[index] = boxa;
1652  return 0;
1653 }
1654 
1655 
1674 l_ok
1676  l_int32 index,
1677  BOXA *boxa)
1678 {
1679 l_int32 i, n;
1680 BOXA **array;
1681 
1682  PROCNAME("boxaaInsertBoxa");
1683 
1684  if (!baa)
1685  return ERROR_INT("baa not defined", procName, 1);
1686  n = boxaaGetCount(baa);
1687  if (index < 0 || index > n)
1688  return ERROR_INT("index not in {0...n}", procName, 1);
1689  if (!boxa)
1690  return ERROR_INT("boxa not defined", procName, 1);
1691 
1692  if (n >= baa->nalloc)
1693  boxaaExtendArray(baa);
1694  array = baa->boxa;
1695  baa->n++;
1696  for (i = n; i > index; i--)
1697  array[i] = array[i - 1];
1698  array[index] = boxa;
1699 
1700  return 0;
1701 }
1702 
1703 
1720 l_ok
1722  l_int32 index)
1723 {
1724 l_int32 i, n;
1725 BOXA **array;
1726 
1727  PROCNAME("boxaaRemoveBox");
1728 
1729  if (!baa)
1730  return ERROR_INT("baa not defined", procName, 1);
1731  n = boxaaGetCount(baa);
1732  if (index < 0 || index >= n)
1733  return ERROR_INT("index not valid", procName, 1);
1734 
1735  array = baa->boxa;
1736  boxaDestroy(&array[index]);
1737  for (i = index + 1; i < n; i++)
1738  array[i - 1] = array[i];
1739  array[n - 1] = NULL;
1740  baa->n--;
1741 
1742  return 0;
1743 }
1744 
1745 
1760 l_ok
1762  l_int32 index,
1763  BOX *box,
1764  l_int32 accessflag)
1765 {
1766 l_int32 n;
1767 BOXA *boxa;
1768  PROCNAME("boxaaAddBox");
1769 
1770  if (!baa)
1771  return ERROR_INT("baa not defined", procName, 1);
1772  n = boxaaGetCount(baa);
1773  if (index < 0 || index >= n)
1774  return ERROR_INT("index not valid", procName, 1);
1775  if (accessflag != L_INSERT && accessflag != L_COPY && accessflag != L_CLONE)
1776  return ERROR_INT("invalid accessflag", procName, 1);
1777 
1778  boxa = boxaaGetBoxa(baa, index, L_CLONE);
1779  boxaAddBox(boxa, box, accessflag);
1780  boxaDestroy(&boxa);
1781  return 0;
1782 }
1783 
1784 
1785 /*---------------------------------------------------------------------*
1786  * Boxaa serialized I/O *
1787  *---------------------------------------------------------------------*/
1808 BOXAA *
1809 boxaaReadFromFiles(const char *dirname,
1810  const char *substr,
1811  l_int32 first,
1812  l_int32 nfiles)
1813 {
1814 char *fname;
1815 l_int32 i, n;
1816 BOXA *boxa;
1817 BOXAA *baa;
1818 SARRAY *sa;
1819 
1820  PROCNAME("boxaaReadFromFiles");
1821 
1822  if (!dirname)
1823  return (BOXAA *)ERROR_PTR("dirname not defined", procName, NULL);
1824 
1825  sa = getSortedPathnamesInDirectory(dirname, substr, first, nfiles);
1826  if (!sa || ((n = sarrayGetCount(sa)) == 0)) {
1827  sarrayDestroy(&sa);
1828  return (BOXAA *)ERROR_PTR("no pixa files found", procName, NULL);
1829  }
1830 
1831  baa = boxaaCreate(n);
1832  for (i = 0; i < n; i++) {
1833  fname = sarrayGetString(sa, i, L_NOCOPY);
1834  if ((boxa = boxaRead(fname)) == NULL) {
1835  L_ERROR("boxa not read for %d-th file", procName, i);
1836  continue;
1837  }
1838  boxaaAddBoxa(baa, boxa, L_INSERT);
1839  }
1840 
1841  sarrayDestroy(&sa);
1842  return baa;
1843 }
1844 
1845 
1852 BOXAA *
1853 boxaaRead(const char *filename)
1854 {
1855 FILE *fp;
1856 BOXAA *baa;
1857 
1858  PROCNAME("boxaaRead");
1859 
1860  if (!filename)
1861  return (BOXAA *)ERROR_PTR("filename not defined", procName, NULL);
1862 
1863  if ((fp = fopenReadStream(filename)) == NULL)
1864  return (BOXAA *)ERROR_PTR("stream not opened", procName, NULL);
1865  baa = boxaaReadStream(fp);
1866  fclose(fp);
1867  if (!baa)
1868  return (BOXAA *)ERROR_PTR("boxaa not read", procName, NULL);
1869  return baa;
1870 }
1871 
1872 
1879 BOXAA *
1881 {
1882 l_int32 n, i, x, y, w, h, version;
1883 l_int32 ignore;
1884 BOXA *boxa;
1885 BOXAA *baa;
1886 
1887  PROCNAME("boxaaReadStream");
1888 
1889  if (!fp)
1890  return (BOXAA *)ERROR_PTR("stream not defined", procName, NULL);
1891 
1892  if (fscanf(fp, "\nBoxaa Version %d\n", &version) != 1)
1893  return (BOXAA *)ERROR_PTR("not a boxaa file", procName, NULL);
1894  if (version != BOXAA_VERSION_NUMBER)
1895  return (BOXAA *)ERROR_PTR("invalid boxa version", procName, NULL);
1896  if (fscanf(fp, "Number of boxa = %d\n", &n) != 1)
1897  return (BOXAA *)ERROR_PTR("not a boxaa file", procName, NULL);
1898 
1899  if ((baa = boxaaCreate(n)) == NULL)
1900  return (BOXAA *)ERROR_PTR("boxaa not made", procName, NULL);
1901  for (i = 0; i < n; i++) {
1902  if (fscanf(fp, "\nBoxa[%d] extent: x = %d, y = %d, w = %d, h = %d",
1903  &ignore, &x, &y, &w, &h) != 5) {
1904  boxaaDestroy(&baa);
1905  return (BOXAA *)ERROR_PTR("boxa descr not valid", procName, NULL);
1906  }
1907  if ((boxa = boxaReadStream(fp)) == NULL) {
1908  boxaaDestroy(&baa);
1909  return (BOXAA *)ERROR_PTR("boxa not made", procName, NULL);
1910  }
1911  boxaaAddBoxa(baa, boxa, L_INSERT);
1912  }
1913  return baa;
1914 }
1915 
1916 
1924 BOXAA *
1925 boxaaReadMem(const l_uint8 *data,
1926  size_t size)
1927 {
1928 FILE *fp;
1929 BOXAA *baa;
1930 
1931  PROCNAME("boxaaReadMem");
1932 
1933  if (!data)
1934  return (BOXAA *)ERROR_PTR("data not defined", procName, NULL);
1935  if ((fp = fopenReadFromMemory(data, size)) == NULL)
1936  return (BOXAA *)ERROR_PTR("stream not opened", procName, NULL);
1937 
1938  baa = boxaaReadStream(fp);
1939  fclose(fp);
1940  if (!baa) L_ERROR("baa not read\n", procName);
1941  return baa;
1942 }
1943 
1944 
1952 l_ok
1953 boxaaWrite(const char *filename,
1954  BOXAA *baa)
1955 {
1956 l_int32 ret;
1957 FILE *fp;
1958 
1959  PROCNAME("boxaaWrite");
1960 
1961  if (!filename)
1962  return ERROR_INT("filename not defined", procName, 1);
1963  if (!baa)
1964  return ERROR_INT("baa not defined", procName, 1);
1965 
1966  if ((fp = fopenWriteStream(filename, "w")) == NULL)
1967  return ERROR_INT("stream not opened", procName, 1);
1968  ret = boxaaWriteStream(fp, baa);
1969  fclose(fp);
1970  if (ret)
1971  return ERROR_INT("baa not written to stream", procName, 1);
1972  return 0;
1973 }
1974 
1975 
1983 l_ok
1985  BOXAA *baa)
1986 {
1987 l_int32 n, i, x, y, w, h;
1988 BOX *box;
1989 BOXA *boxa;
1990 
1991  PROCNAME("boxaaWriteStream");
1992 
1993  if (!fp)
1994  return ERROR_INT("stream not defined", procName, 1);
1995  if (!baa)
1996  return ERROR_INT("baa not defined", procName, 1);
1997 
1998  n = boxaaGetCount(baa);
1999  fprintf(fp, "\nBoxaa Version %d\n", BOXAA_VERSION_NUMBER);
2000  fprintf(fp, "Number of boxa = %d\n", n);
2001 
2002  for (i = 0; i < n; i++) {
2003  if ((boxa = boxaaGetBoxa(baa, i, L_CLONE)) == NULL)
2004  return ERROR_INT("boxa not found", procName, 1);
2005  boxaGetExtent(boxa, NULL, NULL, &box);
2006  boxGetGeometry(box, &x, &y, &w, &h);
2007  fprintf(fp, "\nBoxa[%d] extent: x = %d, y = %d, w = %d, h = %d",
2008  i, x, y, w, h);
2009  boxaWriteStream(fp, boxa);
2010  boxDestroy(&box);
2011  boxaDestroy(&boxa);
2012  }
2013  return 0;
2014 }
2015 
2016 
2030 l_ok
2031 boxaaWriteMem(l_uint8 **pdata,
2032  size_t *psize,
2033  BOXAA *baa)
2034 {
2035 l_int32 ret;
2036 FILE *fp;
2037 
2038  PROCNAME("boxaaWriteMem");
2039 
2040  if (pdata) *pdata = NULL;
2041  if (psize) *psize = 0;
2042  if (!pdata)
2043  return ERROR_INT("&data not defined", procName, 1);
2044  if (!psize)
2045  return ERROR_INT("&size not defined", procName, 1);
2046  if (!baa)
2047  return ERROR_INT("baa not defined", procName, 1);
2048 
2049 #if HAVE_FMEMOPEN
2050  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
2051  return ERROR_INT("stream not opened", procName, 1);
2052  ret = boxaaWriteStream(fp, baa);
2053 #else
2054  L_INFO("work-around: writing to a temp file\n", procName);
2055  #ifdef _WIN32
2056  if ((fp = fopenWriteWinTempfile()) == NULL)
2057  return ERROR_INT("tmpfile stream not opened", procName, 1);
2058  #else
2059  if ((fp = tmpfile()) == NULL)
2060  return ERROR_INT("tmpfile stream not opened", procName, 1);
2061  #endif /* _WIN32 */
2062  ret = boxaaWriteStream(fp, baa);
2063  rewind(fp);
2064  *pdata = l_binaryReadStream(fp, psize);
2065 #endif /* HAVE_FMEMOPEN */
2066  fclose(fp);
2067  return ret;
2068 }
2069 
2070 
2071 /*---------------------------------------------------------------------*
2072  * Boxa serialized I/O *
2073  *---------------------------------------------------------------------*/
2080 BOXA *
2081 boxaRead(const char *filename)
2082 {
2083 FILE *fp;
2084 BOXA *boxa;
2085 
2086  PROCNAME("boxaRead");
2087 
2088  if (!filename)
2089  return (BOXA *)ERROR_PTR("filename not defined", procName, NULL);
2090 
2091  if ((fp = fopenReadStream(filename)) == NULL)
2092  return (BOXA *)ERROR_PTR("stream not opened", procName, NULL);
2093  boxa = boxaReadStream(fp);
2094  fclose(fp);
2095  if (!boxa)
2096  return (BOXA *)ERROR_PTR("boxa not read", procName, NULL);
2097  return boxa;
2098 }
2099 
2100 
2107 BOXA *
2109 {
2110 l_int32 n, i, x, y, w, h, version;
2111 l_int32 ignore;
2112 BOX *box;
2113 BOXA *boxa;
2114 
2115  PROCNAME("boxaReadStream");
2116 
2117  if (!fp)
2118  return (BOXA *)ERROR_PTR("stream not defined", procName, NULL);
2119 
2120  if (fscanf(fp, "\nBoxa Version %d\n", &version) != 1)
2121  return (BOXA *)ERROR_PTR("not a boxa file", procName, NULL);
2122  if (version != BOXA_VERSION_NUMBER)
2123  return (BOXA *)ERROR_PTR("invalid boxa version", procName, NULL);
2124  if (fscanf(fp, "Number of boxes = %d\n", &n) != 1)
2125  return (BOXA *)ERROR_PTR("not a boxa file", procName, NULL);
2126 
2127  if ((boxa = boxaCreate(n)) == NULL)
2128  return (BOXA *)ERROR_PTR("boxa not made", procName, NULL);
2129  for (i = 0; i < n; i++) {
2130  if (fscanf(fp, " Box[%d]: x = %d, y = %d, w = %d, h = %d\n",
2131  &ignore, &x, &y, &w, &h) != 5) {
2132  boxaDestroy(&boxa);
2133  return (BOXA *)ERROR_PTR("box descr not valid", procName, NULL);
2134  }
2135  box = boxCreate(x, y, w, h);
2136  boxaAddBox(boxa, box, L_INSERT);
2137  }
2138 
2139  return boxa;
2140 }
2141 
2142 
2150 BOXA *
2151 boxaReadMem(const l_uint8 *data,
2152  size_t size)
2153 {
2154 FILE *fp;
2155 BOXA *boxa;
2156 
2157  PROCNAME("boxaReadMem");
2158 
2159  if (!data)
2160  return (BOXA *)ERROR_PTR("data not defined", procName, NULL);
2161  if ((fp = fopenReadFromMemory(data, size)) == NULL)
2162  return (BOXA *)ERROR_PTR("stream not opened", procName, NULL);
2163 
2164  boxa = boxaReadStream(fp);
2165  fclose(fp);
2166  if (!boxa) L_ERROR("boxa not read\n", procName);
2167  return boxa;
2168 }
2169 
2170 
2187 l_ok
2188 boxaWriteDebug(const char *filename,
2189  BOXA *boxa)
2190 {
2191  PROCNAME("boxaWriteDebug");
2192 
2193  if (LeptDebugOK) {
2194  return boxaWrite(filename, boxa);
2195  } else {
2196  L_INFO("write to named temp file %s is disabled\n", procName, filename);
2197  return 0;
2198  }
2199 }
2200 
2201 
2209 l_ok
2210 boxaWrite(const char *filename,
2211  BOXA *boxa)
2212 {
2213 l_int32 ret;
2214 FILE *fp;
2215 
2216  PROCNAME("boxaWrite");
2217 
2218  if (!filename)
2219  return ERROR_INT("filename not defined", procName, 1);
2220  if (!boxa)
2221  return ERROR_INT("boxa not defined", procName, 1);
2222 
2223  if ((fp = fopenWriteStream(filename, "w")) == NULL)
2224  return ERROR_INT("stream not opened", procName, 1);
2225  ret = boxaWriteStream(fp, boxa);
2226  fclose(fp);
2227  if (ret)
2228  return ERROR_INT("boxa not written to stream", procName, 1);
2229 
2230  return 0;
2231 }
2232 
2233 
2241 l_ok
2243  BOXA *boxa)
2244 {
2245 l_int32 n, i;
2246 BOX *box;
2247 
2248  PROCNAME("boxaWriteStream");
2249 
2250  if (!fp)
2251  return ERROR_INT("stream not defined", procName, 1);
2252  if (!boxa)
2253  return ERROR_INT("boxa not defined", procName, 1);
2254 
2255  n = boxaGetCount(boxa);
2256  fprintf(fp, "\nBoxa Version %d\n", BOXA_VERSION_NUMBER);
2257  fprintf(fp, "Number of boxes = %d\n", n);
2258  for (i = 0; i < n; i++) {
2259  if ((box = boxaGetBox(boxa, i, L_CLONE)) == NULL)
2260  return ERROR_INT("box not found", procName, 1);
2261  fprintf(fp, " Box[%d]: x = %d, y = %d, w = %d, h = %d\n",
2262  i, box->x, box->y, box->w, box->h);
2263  boxDestroy(&box);
2264  }
2265  return 0;
2266 }
2267 
2268 
2282 l_ok
2283 boxaWriteMem(l_uint8 **pdata,
2284  size_t *psize,
2285  BOXA *boxa)
2286 {
2287 l_int32 ret;
2288 FILE *fp;
2289 
2290  PROCNAME("boxaWriteMem");
2291 
2292  if (pdata) *pdata = NULL;
2293  if (psize) *psize = 0;
2294  if (!pdata)
2295  return ERROR_INT("&data not defined", procName, 1);
2296  if (!psize)
2297  return ERROR_INT("&size not defined", procName, 1);
2298  if (!boxa)
2299  return ERROR_INT("boxa not defined", procName, 1);
2300 
2301 #if HAVE_FMEMOPEN
2302  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
2303  return ERROR_INT("stream not opened", procName, 1);
2304  ret = boxaWriteStream(fp, boxa);
2305 #else
2306  L_INFO("work-around: writing to a temp file\n", procName);
2307  #ifdef _WIN32
2308  if ((fp = fopenWriteWinTempfile()) == NULL)
2309  return ERROR_INT("tmpfile stream not opened", procName, 1);
2310  #else
2311  if ((fp = tmpfile()) == NULL)
2312  return ERROR_INT("tmpfile stream not opened", procName, 1);
2313  #endif /* _WIN32 */
2314  ret = boxaWriteStream(fp, boxa);
2315  rewind(fp);
2316  *pdata = l_binaryReadStream(fp, psize);
2317 #endif /* HAVE_FMEMOPEN */
2318  fclose(fp);
2319  return ret;
2320 }
2321 
2322 
2323 /*---------------------------------------------------------------------*
2324  * Debug printing *
2325  *---------------------------------------------------------------------*/
2339 l_ok
2341  BOX *box)
2342 {
2343  PROCNAME("boxPrintStreamInfo");
2344 
2345  if (!fp)
2346  return ERROR_INT("stream not defined", procName, 1);
2347  if (!box)
2348  return ERROR_INT("box not defined", procName, 1);
2349 
2350  fprintf(fp, " Box: x = %d, y = %d, w = %d, h = %d\n",
2351  box->x, box->y, box->w, box->h);
2352  return 0;
2353 }
l_ok boxaaWrite(const char *filename, BOXAA *baa)
boxaaWrite()
Definition: boxbasic.c:1953
#define BOXAA_VERSION_NUMBER
Definition: pix.h:451
BOXA * boxaReadStream(FILE *fp)
boxaReadStream()
Definition: boxbasic.c:2108
BOXAA * boxaaReadStream(FILE *fp)
boxaaReadStream()
Definition: boxbasic.c:1880
BOX * boxaGetValidBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetValidBox()
Definition: boxbasic.c:802
l_int32 n
Definition: pix.h:494
l_uint32 refcount
Definition: pix.h:496
Definition: pix.h:717
l_ok boxaExtendArrayToSize(BOXA *boxa, l_int32 size)
boxaExtendArrayToSize()
Definition: boxbasic.c:689
l_ok boxaaReplaceBoxa(BOXAA *baa, l_int32 index, BOXA *boxa)
boxaaReplaceBoxa()
Definition: boxbasic.c:1634
static const l_int32 INITIAL_PTR_ARRAYSIZE
Definition: boxbasic.c:136
l_ok boxSetSideLocations(BOX *box, l_int32 l, l_int32 r, l_int32 t, l_int32 b)
boxSetSideLocations()
Definition: boxbasic.c:405
l_int32 boxaaGetCount(BOXAA *baa)
boxaaGetCount()
Definition: boxbasic.c:1424
l_uint32 refcount
Definition: pix.h:486
l_ok boxaExtendArray(BOXA *boxa)
boxaExtendArray()
Definition: boxbasic.c:665
Definition: pix.h:716
BOXAA * boxaaReadMem(const l_uint8 *data, size_t size)
boxaaReadMem()
Definition: boxbasic.c:1925
l_ok boxGetSideLocations(BOX *box, l_int32 *pl, l_int32 *pr, l_int32 *pt, l_int32 *pb)
boxGetSideLocations()
Definition: boxbasic.c:371
l_ok boxaWriteStream(FILE *fp, BOXA *boxa)
boxaWriteStream()
Definition: boxbasic.c:2242
l_int32 y
Definition: pix.h:483
NUMA * numaMakeConstant(l_float32 val, l_int32 size)
numaMakeConstant()
Definition: numafunc1.c:781
struct Boxa ** boxa
Definition: pix.h:506
l_ok boxaReplaceBox(BOXA *boxa, l_int32 index, BOX *box)
boxaReplaceBox()
Definition: boxbasic.c:946
l_ok boxaaExtendWithInit(BOXAA *baa, l_int32 maxindex, BOXA *boxa)
boxaaExtendWithInit()
Definition: boxbasic.c:1593
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
Definition: utils2.c:1734
BOXA * boxaCopy(BOXA *boxa, l_int32 copyflag)
boxaCopy()
Definition: boxbasic.c:534
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
Definition: boxbasic.c:580
Definition: pix.h:492
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
Definition: utils2.c:1161
l_ok numaSetValue(NUMA *na, l_int32 index, l_float32 val)
numaSetValue()
Definition: numabasic.c:759
Definition: array.h:116
Definition: pix.h:502
BOX * boxClone(BOX *box)
boxClone()
Definition: boxbasic.c:252
l_ok boxaaWriteStream(FILE *fp, BOXAA *baa)
boxaaWriteStream()
Definition: boxbasic.c:1984
l_int32 nalloc
Definition: pix.h:505
l_ok boxaaAddBoxa(BOXAA *baa, BOXA *ba, l_int32 copyflag)
boxaaAddBoxa()
Definition: boxbasic.c:1327
BOXAA * boxaaReadFromFiles(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
boxaaReadFromFiles()
Definition: boxbasic.c:1809
l_ok boxaaRemoveBoxa(BOXAA *baa, l_int32 index)
boxaaRemoveBoxa()
Definition: boxbasic.c:1721
Definition: array.h:59
void boxaaDestroy(BOXAA **pbaa)
boxaaDestroy()
Definition: boxbasic.c:1289
l_ok boxaRemoveBoxAndSave(BOXA *boxa, l_int32 index, BOX **pbox)
boxaRemoveBoxAndSave()
Definition: boxbasic.c:1053
BOX * boxCreateValid(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreateValid()
Definition: boxbasic.c:210
l_int32 nalloc
Definition: pix.h:495
l_int32 w
Definition: pix.h:484
BOXA * boxaReadMem(const l_uint8 *data, size_t size)
boxaReadMem()
Definition: boxbasic.c:2151
l_ok boxaGetBoxGeometry(BOXA *boxa, l_int32 index, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxaGetBoxGeometry()
Definition: boxbasic.c:863
BOXA * boxaSaveValid(BOXA *boxas, l_int32 copyflag)
boxaSaveValid()
Definition: boxbasic.c:1095
l_ok boxaAddBox(BOXA *boxa, BOX *box, l_int32 copyflag)
boxaAddBox()
Definition: boxbasic.c:618
l_ok boxaWriteDebug(const char *filename, BOXA *boxa)
boxaWriteDebug()
Definition: boxbasic.c:2188
l_ok boxaaAddBox(BOXAA *baa, l_int32 index, BOX *box, l_int32 accessflag)
boxaaAddBox()
Definition: boxbasic.c:1761
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
Definition: utils2.c:1780
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:681
l_ok boxaWrite(const char *filename, BOXA *boxa)
boxaWrite()
Definition: boxbasic.c:2210
NUMA * boxaFindInvalidBoxes(BOXA *boxa)
boxaFindInvalidBoxes()
Definition: boxbasic.c:830
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
Definition: boxbasic.c:763
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
Definition: sarray1.c:1717
l_int32 x
Definition: pix.h:482
l_ok boxaWriteMem(l_uint8 **pdata, size_t *psize, BOXA *boxa)
boxaWriteMem()
Definition: boxbasic.c:2283
l_ok boxaGetExtent(BOXA *boxa, l_int32 *pw, l_int32 *ph, BOX **pbox)
boxaGetExtent()
Definition: boxfunc4.c:943
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1700
BOXAA * boxaaCreate(l_int32 n)
boxaaCreate()
Definition: boxbasic.c:1223
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1657
l_ok boxaInitFull(BOXA *boxa, BOX *box)
boxaInitFull()
Definition: boxbasic.c:1159
l_int32 boxGetRefcount(BOX *box)
Return the current reference count of box.
Definition: boxbasic.c:433
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
Definition: utils2.c:1262
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:621
BOXAA * boxaaRead(const char *filename)
boxaaRead()
Definition: boxbasic.c:1853
l_int32 n
Definition: pix.h:504
l_int32 boxaGetValidCount(BOXA *boxa)
boxaGetValidCount()
Definition: boxbasic.c:735
l_ok boxaIsFull(BOXA *boxa, l_int32 *pfull)
boxaIsFull()
Definition: boxbasic.c:899
l_ok boxaRemoveBox(BOXA *boxa, l_int32 index)
boxaRemoveBox()
Definition: boxbasic.c:1029
Definition: pix.h:718
l_ok boxaaInitFull(BOXAA *baa, BOXA *boxa)
boxaaInitFull()
Definition: boxbasic.c:1553
l_int32 h
Definition: pix.h:485
#define BOXA_VERSION_NUMBER
Definition: pix.h:450
BOX * boxCopy(BOX *box)
boxCopy()
Definition: boxbasic.c:230
Definition: pix.h:719
l_int32 boxaaGetBoxCount(BOXAA *baa)
boxaaGetBoxCount()
Definition: boxbasic.c:1441
BOX * boxaaGetBox(BOXAA *baa, l_int32 iboxa, l_int32 ibox, l_int32 accessflag)
boxaaGetBox()
Definition: boxbasic.c:1501
BOXA * boxaCreate(l_int32 n)
boxaCreate()
Definition: boxbasic.c:499
l_ok boxaaWriteMem(l_uint8 **pdata, size_t *psize, BOXAA *baa)
boxaaWriteMem()
Definition: boxbasic.c:2031
l_ok boxaaInsertBoxa(BOXAA *baa, l_int32 index, BOXA *boxa)
boxaaInsertBoxa()
Definition: boxbasic.c:1675
void boxDestroy(BOX **pbox)
boxDestroy()
Definition: boxbasic.c:278
l_int32 boxaGetCount(BOXA *boxa)
boxaGetCount()
Definition: boxbasic.c:718
l_ok boxPrintStreamInfo(FILE *fp, BOX *box)
boxPrintStreamInfo()
Definition: boxbasic.c:2340
l_ok boxaaExtendArray(BOXAA *baa)
boxaaExtendArray()
Definition: boxbasic.c:1364
l_ok boxSetGeometry(BOX *box, l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxSetGeometry()
Definition: boxbasic.c:340
l_ok boxaClear(BOXA *boxa)
boxaClear()
Definition: boxbasic.c:1196
struct Box ** box
Definition: pix.h:497
BOXAA * boxaaCopy(BOXAA *baas, l_int32 copyflag)
boxaaCopy()
Definition: boxbasic.c:1258
l_ok boxIsValid(BOX *box, l_int32 *pvalid)
boxIsValid()
Definition: boxbasic.c:472
l_ok boxGetGeometry(BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
Definition: boxbasic.c:310
Definition: pix.h:480
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
Definition: boxbasic.c:165
BOXA * boxaRead(const char *filename)
boxaRead()
Definition: boxbasic.c:2081
l_ok boxChangeRefcount(BOX *box, l_int32 delta)
Adjust the current references count of box by delta.
Definition: boxbasic.c:451
l_ok boxaaExtendArrayToSize(BOXAA *baa, l_int32 size)
boxaaExtendArrayToSize()
Definition: boxbasic.c:1395
l_ok boxaInsertBox(BOXA *boxa, l_int32 index, BOX *box)
boxaInsertBox()
Definition: boxbasic.c:984
BOXA * boxaaGetBoxa(BOXAA *baa, l_int32 index, l_int32 accessflag)
boxaaGetBoxa()
Definition: boxbasic.c:1471
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:355