Leptonica  1.73
Image processing and image analysis suite
pixcomp.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 
144 #include <string.h>
145 #include "allheaders.h"
146 
147 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20; /* n'import quoi */
148 
149  /* These two globals are defined in writefile.c */
150 extern l_int32 NumImageFileFormatExtensions;
151 extern const char *ImageFileFormatExtensions[];
152 
153  /* Static function */
154 static l_int32 pixacompExtendArray(PIXAC *pixac);
155 
156 
157 /*---------------------------------------------------------------------*
158  * Pixcomp creation and destruction *
159  *---------------------------------------------------------------------*/
173 PIXC *
175  l_int32 comptype)
176 {
177 size_t size;
178 char *text;
179 l_int32 ret, format;
180 l_uint8 *data;
181 PIXC *pixc;
182 
183  PROCNAME("pixcompCreateFromPix");
184 
185  if (!pix)
186  return (PIXC *)ERROR_PTR("pix not defined", procName, NULL);
187  if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 &&
188  comptype != IFF_PNG && comptype != IFF_JFIF_JPEG)
189  return (PIXC *)ERROR_PTR("invalid comptype", procName, NULL);
190 
191  if ((pixc = (PIXC *)LEPT_CALLOC(1, sizeof(PIXC))) == NULL)
192  return (PIXC *)ERROR_PTR("pixc not made", procName, NULL);
193  pixGetDimensions(pix, &pixc->w, &pixc->h, &pixc->d);
194  pixGetResolution(pix, &pixc->xres, &pixc->yres);
195  if (pixGetColormap(pix))
196  pixc->cmapflag = 1;
197  if ((text = pixGetText(pix)) != NULL)
198  pixc->text = stringNew(text);
199 
200  pixcompDetermineFormat(comptype, pixc->d, pixc->cmapflag, &format);
201  pixc->comptype = format;
202  ret = pixWriteMem(&data, &size, pix, format);
203  if (ret) {
204  L_ERROR("write to memory failed\n", procName);
205  pixcompDestroy(&pixc);
206  return NULL;
207  }
208  pixc->data = data;
209  pixc->size = size;
210 
211  return pixc;
212 }
213 
214 
230 PIXC *
232  size_t size,
233  l_int32 copyflag)
234 {
235 l_int32 format, w, h, d, bps, spp, iscmap;
236 PIXC *pixc;
237 
238  PROCNAME("pixcompCreateFromString");
239 
240  if (!data)
241  return (PIXC *)ERROR_PTR("data not defined", procName, NULL);
242  if (copyflag != L_INSERT && copyflag != L_COPY)
243  return (PIXC *)ERROR_PTR("invalid copyflag", procName, NULL);
244 
245  if (pixReadHeaderMem(data, size, &format, &w, &h, &bps, &spp, &iscmap) == 1)
246  return (PIXC *)ERROR_PTR("header data not read", procName, NULL);
247  if ((pixc = (PIXC *)LEPT_CALLOC(1, sizeof(PIXC))) == NULL)
248  return (PIXC *)ERROR_PTR("pixc not made", procName, NULL);
249  d = (spp == 3) ? 32 : bps * spp;
250  pixc->w = w;
251  pixc->h = h;
252  pixc->d = d;
253  pixc->comptype = format;
254  pixc->cmapflag = iscmap;
255  if (copyflag == L_INSERT)
256  pixc->data = data;
257  else
258  pixc->data = l_binaryCopy(data, size);
259  pixc->size = size;
260  return pixc;
261 }
262 
263 
279 PIXC *
280 pixcompCreateFromFile(const char *filename,
281  l_int32 comptype)
282 {
283 l_int32 format;
284 size_t nbytes;
285 l_uint8 *data;
286 PIX *pix;
287 PIXC *pixc;
288 
289  PROCNAME("pixcompCreateFromFile");
290 
291  if (!filename)
292  return (PIXC *)ERROR_PTR("filename not defined", procName, NULL);
293  if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 &&
294  comptype != IFF_PNG && comptype != IFF_JFIF_JPEG)
295  return (PIXC *)ERROR_PTR("invalid comptype", procName, NULL);
296 
297  findFileFormat(filename, &format);
298  if (format == IFF_UNKNOWN) {
299  L_ERROR("unreadable file: %s\n", procName, filename);
300  return NULL;
301  }
302 
303  /* Can we accept the encoded file directly? Remember that
304  * png is the "universal" compression type, so if requested
305  * it takes precedence. Otherwise, if the file is already
306  * compressed in g4 or jpeg, just accept the string. */
307  if ((format == IFF_TIFF_G4 && comptype != IFF_PNG) ||
308  (format == IFF_JFIF_JPEG && comptype != IFF_PNG))
309  comptype = format;
310  if (comptype != IFF_DEFAULT && comptype == format) {
311  data = l_binaryRead(filename, &nbytes);
312  if ((pixc = pixcompCreateFromString(data, nbytes, L_INSERT)) == NULL) {
313  LEPT_FREE(data);
314  return (PIXC *)ERROR_PTR("pixc not made (string)", procName, NULL);
315  }
316  return pixc;
317  }
318 
319  /* Need to recompress in the default format */
320  if ((pix = pixRead(filename)) == NULL)
321  return (PIXC *)ERROR_PTR("pix not read", procName, NULL);
322  if ((pixc = pixcompCreateFromPix(pix, comptype)) == NULL) {
323  pixDestroy(&pix);
324  return (PIXC *)ERROR_PTR("pixc not made", procName, NULL);
325  }
326  pixDestroy(&pix);
327  return pixc;
328 }
329 
330 
342 void
344 {
345 PIXC *pixc;
346 
347  PROCNAME("pixcompDestroy");
348 
349  if (!ppixc) {
350  L_WARNING("ptr address is null!\n", procName);
351  return;
352  }
353 
354  if ((pixc = *ppixc) == NULL)
355  return;
356 
357  LEPT_FREE(pixc->data);
358  if (pixc->text)
359  LEPT_FREE(pixc->text);
360  LEPT_FREE(pixc);
361  *ppixc = NULL;
362  return;
363 }
364 
365 
372 PIXC *
374 {
375 size_t size;
376 l_uint8 *datas, *datad;
377 PIXC *pixcd;
378 
379  PROCNAME("pixcompCopy");
380 
381  if (!pixcs)
382  return (PIXC *)ERROR_PTR("pixcs not defined", procName, NULL);
383 
384  if ((pixcd = (PIXC *)LEPT_CALLOC(1, sizeof(PIXC))) == NULL)
385  return (PIXC *)ERROR_PTR("pixcd not made", procName, NULL);
386  pixcd->w = pixcs->w;
387  pixcd->h = pixcs->h;
388  pixcd->d = pixcs->d;
389  pixcd->xres = pixcs->xres;
390  pixcd->yres = pixcs->yres;
391  pixcd->comptype = pixcs->comptype;
392  if (pixcs->text != NULL)
393  pixcd->text = stringNew(pixcs->text);
394  pixcd->cmapflag = pixcs->cmapflag;
395 
396  /* Copy image data */
397  size = pixcs->size;
398  datas = pixcs->data;
399  datad = (l_uint8 *)LEPT_CALLOC(size, sizeof(l_int8));
400  memcpy((char*)datad, (char*)datas, size);
401  pixcd->data = datad;
402  pixcd->size = size;
403  return pixcd;
404 }
405 
406 
407 /*---------------------------------------------------------------------*
408  * Pixcomp accessors *
409  *---------------------------------------------------------------------*/
417 l_int32
419  l_int32 *pw,
420  l_int32 *ph,
421  l_int32 *pd)
422 {
423  PROCNAME("pixcompGetDimensions");
424 
425  if (!pixc)
426  return ERROR_INT("pixc not defined", procName, 1);
427  if (pw) *pw = pixc->w;
428  if (ph) *ph = pixc->h;
429  if (pd) *pd = pixc->d;
430  return 0;
431 }
432 
433 
454 l_int32
455 pixcompDetermineFormat(l_int32 comptype,
456  l_int32 d,
457  l_int32 cmapflag,
458  l_int32 *pformat)
459 {
460 
461  PROCNAME("pixcompDetermineFormat");
462 
463  if (!pformat)
464  return ERROR_INT("&format not defined", procName, 1);
465  *pformat = IFF_PNG; /* init value and default */
466  if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 &&
467  comptype != IFF_PNG && comptype != IFF_JFIF_JPEG)
468  return ERROR_INT("invalid comptype", procName, 1);
469 
470  if (comptype == IFF_DEFAULT) {
471  if (d == 1)
472  *pformat = IFF_TIFF_G4;
473  else if (d == 16)
474  *pformat = IFF_PNG;
475  else if (d >= 8 && !cmapflag)
476  *pformat = IFF_JFIF_JPEG;
477  } else if (comptype == IFF_TIFF_G4 && d == 1) {
478  *pformat = IFF_TIFF_G4;
479  } else if (comptype == IFF_JFIF_JPEG && d >= 8 && !cmapflag) {
480  *pformat = IFF_JFIF_JPEG;
481  }
482 
483  return 0;
484 }
485 
486 
487 /*---------------------------------------------------------------------*
488  * Pixcomp conversion to Pix *
489  *---------------------------------------------------------------------*/
496 PIX *
498 {
499 l_int32 w, h, d, cmapinpix, format;
500 PIX *pix;
501 
502  PROCNAME("pixCreateFromPixcomp");
503 
504  if (!pixc)
505  return (PIX *)ERROR_PTR("pixc not defined", procName, NULL);
506 
507  if ((pix = pixReadMem(pixc->data, pixc->size)) == NULL)
508  return (PIX *)ERROR_PTR("pix not read", procName, NULL);
509  pixSetResolution(pix, pixc->xres, pixc->yres);
510  if (pixc->text)
511  pixSetText(pix, pixc->text);
512 
513  /* Check fields for consistency */
514  pixGetDimensions(pix, &w, &h, &d);
515  if (pixc->w != w) {
516  L_INFO("pix width %d != pixc width %d\n", procName, w, pixc->w);
517  L_ERROR("pix width %d != pixc width\n", procName, w);
518  }
519  if (pixc->h != h)
520  L_ERROR("pix height %d != pixc height\n", procName, h);
521  if (pixc->d != d) {
522  if (pixc->d == 16) /* we strip 16 --> 8 bpp by default */
523  L_WARNING("pix depth %d != pixc depth 16\n", procName, d);
524  else
525  L_ERROR("pix depth %d != pixc depth\n", procName, d);
526  }
527  cmapinpix = (pixGetColormap(pix) != NULL);
528  if ((cmapinpix && !pixc->cmapflag) || (!cmapinpix && pixc->cmapflag))
529  L_ERROR("pix cmap flag inconsistent\n", procName);
530  format = pixGetInputFormat(pix);
531  if (format != pixc->comptype) {
532  L_ERROR("pix comptype %d not equal to pixc comptype\n",
533  procName, format);
534  }
535 
536  return pix;
537 }
538 
539 
540 /*---------------------------------------------------------------------*
541  * Pixacomp creation and destruction *
542  *---------------------------------------------------------------------*/
549 PIXAC *
550 pixacompCreate(l_int32 n)
551 {
552 PIXAC *pixac;
553 
554  PROCNAME("pixacompCreate");
555 
556  if (n <= 0)
557  n = INITIAL_PTR_ARRAYSIZE;
558 
559  if ((pixac = (PIXAC *)LEPT_CALLOC(1, sizeof(PIXAC))) == NULL)
560  return (PIXAC *)ERROR_PTR("pixac not made", procName, NULL);
561  pixac->n = 0;
562  pixac->nalloc = n;
563  pixac->offset = 0;
564 
565  if ((pixac->pixc = (PIXC **)LEPT_CALLOC(n, sizeof(PIXC *))) == NULL) {
566  pixacompDestroy(&pixac);
567  return (PIXAC *)ERROR_PTR("pixc ptrs not made", procName, NULL);
568  }
569  if ((pixac->boxa = boxaCreate(n)) == NULL) {
570  pixacompDestroy(&pixac);
571  return (PIXAC *)ERROR_PTR("boxa not made", procName, NULL);
572  }
573 
574  return pixac;
575 }
576 
577 
616 PIXAC *
618  l_int32 offset,
619  PIX *pix,
620  l_int32 comptype)
621 {
622 l_int32 i;
623 PIX *pixt;
624 PIXC *pixc;
625 PIXAC *pixac;
626 
627  PROCNAME("pixacompCreateWithInit");
628 
629  if (n <= 0)
630  return (PIXAC *)ERROR_PTR("n must be > 0", procName, NULL);
631  if (pix) {
632  if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 &&
633  comptype != IFF_PNG && comptype != IFF_JFIF_JPEG)
634  return (PIXAC *)ERROR_PTR("invalid comptype", procName, NULL);
635  } else {
636  comptype = IFF_TIFF_G4;
637  }
638  if (offset < 0) {
639  L_WARNING("offset < 0; setting to 0\n", procName);
640  offset = 0;
641  }
642 
643  if ((pixac = pixacompCreate(n)) == NULL)
644  return (PIXAC *)ERROR_PTR("pixac not made", procName, NULL);
645  pixacompSetOffset(pixac, offset);
646  if (pix)
647  pixt = pixClone(pix);
648  else
649  pixt = pixCreate(1, 1, 1);
650  for (i = 0; i < n; i++) {
651  pixc = pixcompCreateFromPix(pixt, comptype);
652  pixacompAddPixcomp(pixac, pixc, L_INSERT);
653  }
654  pixDestroy(&pixt);
655 
656  return pixac;
657 }
658 
659 
678 PIXAC *
680  l_int32 comptype,
681  l_int32 accesstype)
682 {
683 l_int32 i, n;
684 BOXA *boxa;
685 PIX *pix;
686 PIXAC *pixac;
687 
688  PROCNAME("pixacompCreateFromPixa");
689 
690  if (!pixa)
691  return (PIXAC *)ERROR_PTR("pixa not defined", procName, NULL);
692  if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 &&
693  comptype != IFF_PNG && comptype != IFF_JFIF_JPEG)
694  return (PIXAC *)ERROR_PTR("invalid comptype", procName, NULL);
695  if (accesstype != L_COPY && accesstype != L_CLONE &&
696  accesstype != L_COPY_CLONE)
697  return (PIXAC *)ERROR_PTR("invalid accesstype", procName, NULL);
698 
699  n = pixaGetCount(pixa);
700  if ((pixac = pixacompCreate(n)) == NULL)
701  return (PIXAC *)ERROR_PTR("pixac not made", procName, NULL);
702  for (i = 0; i < n; i++) {
703  pix = pixaGetPix(pixa, i, L_CLONE);
704  pixacompAddPix(pixac, pix, comptype);
705  pixDestroy(&pix);
706  }
707  if ((boxa = pixaGetBoxa(pixa, accesstype)) != NULL) {
708  boxaDestroy(&pixac->boxa);
709  pixac->boxa = boxa;
710  }
711 
712  return pixac;
713 }
714 
715 
737 PIXAC *
738 pixacompCreateFromFiles(const char *dirname,
739  const char *substr,
740  l_int32 comptype)
741 {
742 PIXAC *pixac;
743 SARRAY *sa;
744 
745  PROCNAME("pixacompCreateFromFiles");
746 
747  if (!dirname)
748  return (PIXAC *)ERROR_PTR("dirname not defined", procName, NULL);
749  if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 &&
750  comptype != IFF_PNG && comptype != IFF_JFIF_JPEG)
751  return (PIXAC *)ERROR_PTR("invalid comptype", procName, NULL);
752 
753  if ((sa = getSortedPathnamesInDirectory(dirname, substr, 0, 0)) == NULL)
754  return (PIXAC *)ERROR_PTR("sa not made", procName, NULL);
755  pixac = pixacompCreateFromSA(sa, comptype);
756  sarrayDestroy(&sa);
757  return pixac;
758 }
759 
760 
776 PIXAC *
778  l_int32 comptype)
779 {
780 char *str;
781 l_int32 i, n;
782 PIXC *pixc;
783 PIXAC *pixac;
784 
785  PROCNAME("pixacompCreateFromSA");
786 
787  if (!sa)
788  return (PIXAC *)ERROR_PTR("sarray not defined", procName, NULL);
789  if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 &&
790  comptype != IFF_PNG && comptype != IFF_JFIF_JPEG)
791  return (PIXAC *)ERROR_PTR("invalid comptype", procName, NULL);
792 
793  n = sarrayGetCount(sa);
794  pixac = pixacompCreate(n);
795  for (i = 0; i < n; i++) {
796  str = sarrayGetString(sa, i, L_NOCOPY);
797  if ((pixc = pixcompCreateFromFile(str, comptype)) == NULL) {
798  L_ERROR("pixc not read from file: %s\n", procName, str);
799  continue;
800  }
801  pixacompAddPixcomp(pixac, pixc, L_INSERT);
802  }
803  return pixac;
804 }
805 
806 
818 void
820 {
821 l_int32 i;
822 PIXAC *pixac;
823 
824  PROCNAME("pixacompDestroy");
825 
826  if (ppixac == NULL) {
827  L_WARNING("ptr address is NULL!\n", procName);
828  return;
829  }
830 
831  if ((pixac = *ppixac) == NULL)
832  return;
833 
834  for (i = 0; i < pixac->n; i++)
835  pixcompDestroy(&pixac->pixc[i]);
836  LEPT_FREE(pixac->pixc);
837  boxaDestroy(&pixac->boxa);
838  LEPT_FREE(pixac);
839 
840  *ppixac = NULL;
841  return;
842 }
843 
844 
845 /*---------------------------------------------------------------------*
846  * Pixacomp addition *
847  *---------------------------------------------------------------------*/
865 l_int32
867  PIX *pix,
868  l_int32 comptype)
869 {
870 l_int32 cmapflag, format;
871 PIXC *pixc;
872 
873  PROCNAME("pixacompAddPix");
874 
875  if (!pixac)
876  return ERROR_INT("pixac not defined", procName, 1);
877  if (!pix)
878  return ERROR_INT("pix not defined", procName, 1);
879  if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 &&
880  comptype != IFF_PNG && comptype != IFF_JFIF_JPEG)
881  return ERROR_INT("invalid format", procName, 1);
882 
883  cmapflag = pixGetColormap(pix) ? 1 : 0;
884  pixcompDetermineFormat(comptype, pixGetDepth(pix), cmapflag, &format);
885  if ((pixc = pixcompCreateFromPix(pix, format)) == NULL)
886  return ERROR_INT("pixc not made", procName, 1);
887  pixacompAddPixcomp(pixac, pixc, L_INSERT);
888  return 0;
889 }
890 
891 
907 l_int32
909  PIXC *pixc,
910  l_int32 copyflag)
911 {
912 l_int32 n;
913 
914  PROCNAME("pixacompAddPixcomp");
915 
916  if (!pixac)
917  return ERROR_INT("pixac not defined", procName, 1);
918  if (!pixc)
919  return ERROR_INT("pixc not defined", procName, 1);
920  if (copyflag != L_INSERT && copyflag != L_COPY)
921  return ERROR_INT("invalid copyflag", procName, 1);
922 
923  n = pixac->n;
924  if (n >= pixac->nalloc)
925  pixacompExtendArray(pixac);
926  if (copyflag == L_INSERT)
927  pixac->pixc[n] = pixc;
928  else /* L_COPY */
929  pixac->pixc[n] = pixcompCopy(pixc);
930  pixac->n++;
931 
932  return 0;
933 }
934 
935 
950 static l_int32
952 {
953  PROCNAME("pixacompExtendArray");
954 
955  if (!pixac)
956  return ERROR_INT("pixac not defined", procName, 1);
957 
958  if ((pixac->pixc = (PIXC **)reallocNew((void **)&pixac->pixc,
959  sizeof(PIXC *) * pixac->nalloc,
960  2 * sizeof(PIXC *) * pixac->nalloc)) == NULL)
961  return ERROR_INT("new ptr array not returned", procName, 1);
962  pixac->nalloc = 2 * pixac->nalloc;
963  boxaExtendArray(pixac->boxa);
964  return 0;
965 }
966 
967 
985 l_int32
987  l_int32 index,
988  PIX *pix,
989  l_int32 comptype)
990 {
991 l_int32 n, aindex;
992 PIXC *pixc;
993 
994  PROCNAME("pixacompReplacePix");
995 
996  if (!pixac)
997  return ERROR_INT("pixac not defined", procName, 1);
998  n = pixacompGetCount(pixac);
999  aindex = index - pixac->offset;
1000  if (aindex < 0 || aindex >= n)
1001  return ERROR_INT("array index out of bounds", procName, 1);
1002  if (!pix)
1003  return ERROR_INT("pix not defined", procName, 1);
1004  if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 &&
1005  comptype != IFF_PNG && comptype != IFF_JFIF_JPEG)
1006  return ERROR_INT("invalid format", procName, 1);
1007 
1008  pixc = pixcompCreateFromPix(pix, comptype);
1009  pixacompReplacePixcomp(pixac, index, pixc);
1010  return 0;
1011 }
1012 
1013 
1030 l_int32
1032  l_int32 index,
1033  PIXC *pixc)
1034 {
1035 l_int32 n, aindex;
1036 PIXC *pixct;
1037 
1038  PROCNAME("pixacompReplacePixcomp");
1039 
1040  if (!pixac)
1041  return ERROR_INT("pixac not defined", procName, 1);
1042  n = pixacompGetCount(pixac);
1043  aindex = index - pixac->offset;
1044  if (aindex < 0 || aindex >= n)
1045  return ERROR_INT("array index out of bounds", procName, 1);
1046  if (!pixc)
1047  return ERROR_INT("pixc not defined", procName, 1);
1048 
1049  pixct = pixacompGetPixcomp(pixac, index, L_NOCOPY); /* use %index */
1050  pixcompDestroy(&pixct);
1051  pixac->pixc[aindex] = pixc; /* replace; use array index */
1052 
1053  return 0;
1054 }
1055 
1056 
1065 l_int32
1067  BOX *box,
1068  l_int32 copyflag)
1069 {
1070  PROCNAME("pixacompAddBox");
1071 
1072  if (!pixac)
1073  return ERROR_INT("pixac not defined", procName, 1);
1074  if (!box)
1075  return ERROR_INT("box not defined", procName, 1);
1076  if (copyflag != L_INSERT && copyflag != L_COPY)
1077  return ERROR_INT("invalid copyflag", procName, 1);
1078 
1079  boxaAddBox(pixac->boxa, box, copyflag);
1080  return 0;
1081 }
1082 
1083 
1084 /*---------------------------------------------------------------------*
1085  * Pixacomp accessors *
1086  *---------------------------------------------------------------------*/
1093 l_int32
1095 {
1096  PROCNAME("pixacompGetCount");
1097 
1098  if (!pixac)
1099  return ERROR_INT("pixac not defined", procName, 0);
1100 
1101  return pixac->n;
1102 }
1103 
1104 
1121 PIXC *
1123  l_int32 index,
1124  l_int32 copyflag)
1125 {
1126 l_int32 aindex;
1127 
1128  PROCNAME("pixacompGetPixcomp");
1129 
1130  if (!pixac)
1131  return (PIXC *)ERROR_PTR("pixac not defined", procName, NULL);
1132  if (copyflag != L_NOCOPY && copyflag != L_COPY)
1133  return (PIXC *)ERROR_PTR("invalid copyflag", procName, NULL);
1134  aindex = index - pixac->offset;
1135  if (aindex < 0 || aindex >= pixac->n)
1136  return (PIXC *)ERROR_PTR("array index not valid", procName, NULL);
1137 
1138  if (copyflag == L_NOCOPY)
1139  return pixac->pixc[aindex];
1140  else /* L_COPY */
1141  return pixcompCopy(pixac->pixc[aindex]);
1142 }
1143 
1144 
1158 PIX *
1160  l_int32 index)
1161 {
1162 l_int32 aindex;
1163 PIXC *pixc;
1164 
1165  PROCNAME("pixacompGetPix");
1166 
1167  if (!pixac)
1168  return (PIX *)ERROR_PTR("pixac not defined", procName, NULL);
1169  aindex = index - pixac->offset;
1170  if (aindex < 0 || aindex >= pixac->n)
1171  return (PIX *)ERROR_PTR("array index not valid", procName, NULL);
1172 
1173  pixc = pixacompGetPixcomp(pixac, index, L_NOCOPY);
1174  return pixCreateFromPixcomp(pixc);
1175 }
1176 
1177 
1192 l_int32
1194  l_int32 index,
1195  l_int32 *pw,
1196  l_int32 *ph,
1197  l_int32 *pd)
1198 {
1199 l_int32 aindex;
1200 PIXC *pixc;
1201 
1202  PROCNAME("pixacompGetPixDimensions");
1203 
1204  if (!pixac)
1205  return ERROR_INT("pixac not defined", procName, 1);
1206  aindex = index - pixac->offset;
1207  if (aindex < 0 || aindex >= pixac->n)
1208  return ERROR_INT("array index not valid", procName, 1);
1209 
1210  if ((pixc = pixac->pixc[aindex]) == NULL)
1211  return ERROR_INT("pixc not found!", procName, 1);
1212  pixcompGetDimensions(pixc, pw, ph, pd);
1213  return 0;
1214 }
1215 
1216 
1224 BOXA *
1226  l_int32 accesstype)
1227 {
1228  PROCNAME("pixacompGetBoxa");
1229 
1230  if (!pixac)
1231  return (BOXA *)ERROR_PTR("pixac not defined", procName, NULL);
1232  if (!pixac->boxa)
1233  return (BOXA *)ERROR_PTR("boxa not defined", procName, NULL);
1234  if (accesstype != L_COPY && accesstype != L_CLONE &&
1235  accesstype != L_COPY_CLONE)
1236  return (BOXA *)ERROR_PTR("invalid accesstype", procName, NULL);
1237 
1238  return boxaCopy(pixac->boxa, accesstype);
1239 }
1240 
1241 
1248 l_int32
1250 {
1251  PROCNAME("pixacompGetBoxaCount");
1252 
1253  if (!pixac)
1254  return ERROR_INT("pixac not defined", procName, 0);
1255 
1256  return boxaGetCount(pixac->boxa);
1257 }
1258 
1259 
1282 BOX *
1284  l_int32 index,
1285  l_int32 accesstype)
1286 {
1287 l_int32 aindex;
1288 BOX *box;
1289 
1290  PROCNAME("pixacompGetBox");
1291 
1292  if (!pixac)
1293  return (BOX *)ERROR_PTR("pixac not defined", procName, NULL);
1294  if (!pixac->boxa)
1295  return (BOX *)ERROR_PTR("boxa not defined", procName, NULL);
1296  aindex = index - pixac->offset;
1297  if (aindex < 0 || aindex >= pixac->boxa->n)
1298  return (BOX *)ERROR_PTR("array index not valid", procName, NULL);
1299  if (accesstype != L_COPY && accesstype != L_CLONE)
1300  return (BOX *)ERROR_PTR("invalid accesstype", procName, NULL);
1301 
1302  box = pixac->boxa->box[aindex];
1303  if (box) {
1304  if (accesstype == L_COPY)
1305  return boxCopy(box);
1306  else /* accesstype == L_CLONE */
1307  return boxClone(box);
1308  } else {
1309  return NULL;
1310  }
1311 }
1312 
1313 
1328 l_int32
1330  l_int32 index,
1331  l_int32 *px,
1332  l_int32 *py,
1333  l_int32 *pw,
1334  l_int32 *ph)
1335 {
1336 l_int32 aindex;
1337 BOX *box;
1338 
1339  PROCNAME("pixacompGetBoxGeometry");
1340 
1341  if (!pixac)
1342  return ERROR_INT("pixac not defined", procName, 1);
1343  aindex = index - pixac->offset;
1344  if (aindex < 0 || aindex >= pixac->n)
1345  return ERROR_INT("array index not valid", procName, 1);
1346 
1347  if ((box = pixacompGetBox(pixac, aindex, L_CLONE)) == NULL)
1348  return ERROR_INT("box not found!", procName, 1);
1349  boxGetGeometry(box, px, py, pw, ph);
1350  boxDestroy(&box);
1351  return 0;
1352 }
1353 
1354 
1368 l_int32
1370 {
1371  PROCNAME("pixacompGetOffset");
1372 
1373  if (!pixac)
1374  return ERROR_INT("pixac not defined", procName, 0);
1375  return pixac->offset;
1376 }
1377 
1378 
1393 l_int32
1395  l_int32 offset)
1396 {
1397  PROCNAME("pixacompSetOffset");
1398 
1399  if (!pixac)
1400  return ERROR_INT("pixac not defined", procName, 1);
1401  pixac->offset = L_MAX(0, offset);
1402  return 0;
1403 }
1404 
1405 
1406 /*---------------------------------------------------------------------*
1407  * Pixacomp conversion to Pixa *
1408  *---------------------------------------------------------------------*/
1423 PIXA *
1425  l_int32 accesstype)
1426 {
1427 l_int32 i, n, offset;
1428 PIX *pix;
1429 PIXA *pixa;
1430 
1431  PROCNAME("pixaCreateFromPixacomp");
1432 
1433  if (!pixac)
1434  return (PIXA *)ERROR_PTR("pixac not defined", procName, NULL);
1435  if (accesstype != L_COPY && accesstype != L_CLONE &&
1436  accesstype != L_COPY_CLONE)
1437  return (PIXA *)ERROR_PTR("invalid accesstype", procName, NULL);
1438 
1439  n = pixacompGetCount(pixac);
1440  offset = pixacompGetOffset(pixac);
1441  pixacompSetOffset(pixac, 0);
1442  if ((pixa = pixaCreate(n)) == NULL)
1443  return (PIXA *)ERROR_PTR("pixa not made", procName, NULL);
1444  for (i = 0; i < n; i++) {
1445  if ((pix = pixacompGetPix(pixac, i)) == NULL) {
1446  L_WARNING("pix %d not made\n", procName, i);
1447  continue;
1448  }
1449  pixaAddPix(pixa, pix, L_INSERT);
1450  }
1451  if (pixa->boxa) {
1452  boxaDestroy(&pixa->boxa);
1453  pixa->boxa = pixacompGetBoxa(pixac, accesstype);
1454  }
1455  pixacompSetOffset(pixac, offset);
1456 
1457  return pixa;
1458 }
1459 
1460 
1461 /*---------------------------------------------------------------------*
1462  * Combining pixacomp
1463  *---------------------------------------------------------------------*/
1481 l_int32
1483  PIXAC *pixacs,
1484  l_int32 istart,
1485  l_int32 iend)
1486 {
1487 l_int32 i, n, nb;
1488 BOXA *boxas, *boxad;
1489 PIXC *pixc;
1490 
1491  PROCNAME("pixacompJoin");
1492 
1493  if (!pixacd)
1494  return ERROR_INT("pixacd not defined", procName, 1);
1495  if (!pixacs || ((n = pixacompGetCount(pixacs)) == 0))
1496  return 0;
1497 
1498  if (istart < 0)
1499  istart = 0;
1500  if (iend < 0 || iend >= n)
1501  iend = n - 1;
1502  if (istart > iend)
1503  return ERROR_INT("istart > iend; nothing to add", procName, 1);
1504 
1505  for (i = istart; i <= iend; i++) {
1506  pixc = pixacompGetPixcomp(pixacs, i, L_NOCOPY);
1507  pixacompAddPixcomp(pixacd, pixc, L_COPY);
1508  }
1509 
1510  boxas = pixacompGetBoxa(pixacs, L_CLONE);
1511  boxad = pixacompGetBoxa(pixacd, L_CLONE);
1512  nb = pixacompGetBoxaCount(pixacs);
1513  iend = L_MIN(iend, nb - 1);
1514  boxaJoin(boxad, boxas, istart, iend);
1515  boxaDestroy(&boxas); /* just the clones */
1516  boxaDestroy(&boxad); /* ditto */
1517  return 0;
1518 }
1519 
1520 
1534 PIXAC *
1536  PIXAC *pixac2)
1537 {
1538 l_int32 i, n1, n2, n, nb1, nb2;
1539 BOX *box;
1540 PIXC *pixc1, *pixc2;
1541 PIXAC *pixacd;
1542 
1543  PROCNAME("pixacompInterleave");
1544 
1545  if (!pixac1)
1546  return (PIXAC *)ERROR_PTR("pixac1 not defined", procName, NULL);
1547  if (!pixac2)
1548  return (PIXAC *)ERROR_PTR("pixac2 not defined", procName, NULL);
1549  n1 = pixacompGetCount(pixac1);
1550  n2 = pixacompGetCount(pixac2);
1551  n = L_MIN(n1, n2);
1552  if (n == 0)
1553  return (PIXAC *)ERROR_PTR("at least one input pixac is empty",
1554  procName, NULL);
1555  if (n1 != n2)
1556  L_WARNING("counts differ: %d != %d\n", procName, n1, n2);
1557 
1558  pixacd = pixacompCreate(2 * n);
1559  nb1 = pixacompGetBoxaCount(pixac1);
1560  nb2 = pixacompGetBoxaCount(pixac2);
1561  for (i = 0; i < n; i++) {
1562  pixc1 = pixacompGetPixcomp(pixac1, i, L_COPY);
1563  pixacompAddPixcomp(pixacd, pixc1, L_INSERT);
1564  if (i < nb1) {
1565  box = pixacompGetBox(pixac1, i, L_COPY);
1566  pixacompAddBox(pixacd, box, L_INSERT);
1567  }
1568  pixc2 = pixacompGetPixcomp(pixac2, i, L_COPY);
1569  pixacompAddPixcomp(pixacd, pixc2, L_INSERT);
1570  if (i < nb2) {
1571  box = pixacompGetBox(pixac2, i, L_COPY);
1572  pixacompAddBox(pixacd, box, L_INSERT);
1573  }
1574  }
1575 
1576  return pixacd;
1577 }
1578 
1579 
1580 /*---------------------------------------------------------------------*
1581  * Pixacomp serialized I/O *
1582  *---------------------------------------------------------------------*/
1596 PIXAC *
1597 pixacompRead(const char *filename)
1598 {
1599 FILE *fp;
1600 PIXAC *pixac;
1601 
1602  PROCNAME("pixacompRead");
1603 
1604  if (!filename)
1605  return (PIXAC *)ERROR_PTR("filename not defined", procName, NULL);
1606 
1607  if ((fp = fopenReadStream(filename)) == NULL)
1608  return (PIXAC *)ERROR_PTR("stream not opened", procName, NULL);
1609  pixac = pixacompReadStream(fp);
1610  fclose(fp);
1611  if (!pixac)
1612  return (PIXAC *)ERROR_PTR("pixac not read", procName, NULL);
1613  return pixac;
1614 }
1615 
1616 
1623 PIXAC *
1625 {
1626 char buf[256];
1627 l_uint8 *data;
1628 l_int32 n, offset, i, w, h, d, ignore;
1629 l_int32 comptype, size, cmapflag, version, xres, yres;
1630 BOXA *boxa;
1631 PIXC *pixc;
1632 PIXAC *pixac;
1633 
1634  PROCNAME("pixacompReadStream");
1635 
1636  if (!fp)
1637  return (PIXAC *)ERROR_PTR("stream not defined", procName, NULL);
1638 
1639  if (fscanf(fp, "\nPixacomp Version %d\n", &version) != 1)
1640  return (PIXAC *)ERROR_PTR("not a pixacomp file", procName, NULL);
1641  if (version != PIXACOMP_VERSION_NUMBER)
1642  return (PIXAC *)ERROR_PTR("invalid pixacomp version", procName, NULL);
1643  if (fscanf(fp, "Number of pixcomp = %d\n", &n) != 1)
1644  return (PIXAC *)ERROR_PTR("not a pixacomp file", procName, NULL);
1645  if (fscanf(fp, "Offset of index into array = %d", &offset) != 1)
1646  return (PIXAC *)ERROR_PTR("offset not read", procName, NULL);
1647 
1648  if ((pixac = pixacompCreate(n)) == NULL)
1649  return (PIXAC *)ERROR_PTR("pixac not made", procName, NULL);
1650  if ((boxa = boxaReadStream(fp)) == NULL) {
1651  pixacompDestroy(&pixac);
1652  return (PIXAC *)ERROR_PTR("boxa not made", procName, NULL);
1653  }
1654  boxaDestroy(&pixac->boxa); /* empty */
1655  pixac->boxa = boxa;
1656  pixacompSetOffset(pixac, offset);
1657 
1658  for (i = 0; i < n; i++) {
1659  if (fscanf(fp, "\nPixcomp[%d]: w = %d, h = %d, d = %d\n",
1660  &ignore, &w, &h, &d) != 4) {
1661  pixacompDestroy(&pixac);
1662  return (PIXAC *)ERROR_PTR("size reading", procName, NULL);
1663  }
1664  if (fscanf(fp, " comptype = %d, size = %d, cmapflag = %d\n",
1665  &comptype, &size, &cmapflag) != 3) {
1666  pixacompDestroy(&pixac);
1667  return (PIXAC *)ERROR_PTR("comptype/size reading", procName, NULL);
1668  }
1669 
1670  /* Use fgets() and sscanf(); not fscanf(), for the last
1671  * bit of header data before the binary data. The reason is
1672  * that fscanf throws away white space, and if the binary data
1673  * happens to begin with ascii character(s) that are white
1674  * space, it will swallow them and all will be lost! */
1675  if (fgets(buf, sizeof(buf), fp) == NULL) {
1676  pixacompDestroy(&pixac);
1677  return (PIXAC *)ERROR_PTR("fgets read fail", procName, NULL);
1678  }
1679  if (sscanf(buf, " xres = %d, yres = %d\n", &xres, &yres) != 2) {
1680  pixacompDestroy(&pixac);
1681  return (PIXAC *)ERROR_PTR("read fail for res", procName, NULL);
1682  }
1683  if ((data = (l_uint8 *)LEPT_CALLOC(1, size)) == NULL) {
1684  pixacompDestroy(&pixac);
1685  return (PIXAC *)ERROR_PTR("calloc fail for data", procName, NULL);
1686  }
1687  if (fread(data, 1, size, fp) != size) {
1688  pixacompDestroy(&pixac);
1689  LEPT_FREE(data);
1690  return (PIXAC *)ERROR_PTR("error reading data", procName, NULL);
1691  }
1692  fgetc(fp); /* swallow the ending nl */
1693  pixc = (PIXC *)LEPT_CALLOC(1, sizeof(PIXC));
1694  pixc->w = w;
1695  pixc->h = h;
1696  pixc->d = d;
1697  pixc->xres = xres;
1698  pixc->yres = yres;
1699  pixc->comptype = comptype;
1700  pixc->cmapflag = cmapflag;
1701  pixc->data = data;
1702  pixc->size = size;
1703  pixacompAddPixcomp(pixac, pixc, L_INSERT);
1704  }
1705  return pixac;
1706 }
1707 
1708 
1721 PIXAC *
1722 pixacompReadMem(const l_uint8 *data,
1723  size_t size)
1724 {
1725 FILE *fp;
1726 PIXAC *pixac;
1727 
1728  PROCNAME("pixacompReadMem");
1729 
1730  if (!data)
1731  return (PIXAC *)ERROR_PTR("data not defined", procName, NULL);
1732  if ((fp = fopenReadFromMemory(data, size)) == NULL)
1733  return (PIXAC *)ERROR_PTR("stream not opened", procName, NULL);
1734 
1735  pixac = pixacompReadStream(fp);
1736  fclose(fp);
1737  if (!pixac) L_ERROR("pixac not read\n", procName);
1738  return pixac;
1739 }
1740 
1741 
1756 l_int32
1757 pixacompWrite(const char *filename,
1758  PIXAC *pixac)
1759 {
1760 l_int32 ret;
1761 FILE *fp;
1762 
1763  PROCNAME("pixacompWrite");
1764 
1765  if (!filename)
1766  return ERROR_INT("filename not defined", procName, 1);
1767  if (!pixac)
1768  return ERROR_INT("pixacomp not defined", procName, 1);
1769 
1770  if ((fp = fopenWriteStream(filename, "wb")) == NULL)
1771  return ERROR_INT("stream not opened", procName, 1);
1772  ret = pixacompWriteStream(fp, pixac);
1773  fclose(fp);
1774  if (ret)
1775  return ERROR_INT("pixacomp not written to stream", procName, 1);
1776  return 0;
1777 }
1778 
1779 
1787 l_int32
1789  PIXAC *pixac)
1790 {
1791 l_int32 n, i;
1792 PIXC *pixc;
1793 
1794  PROCNAME("pixacompWriteStream");
1795 
1796  if (!fp)
1797  return ERROR_INT("stream not defined", procName, 1);
1798  if (!pixac)
1799  return ERROR_INT("pixac not defined", procName, 1);
1800 
1801  n = pixacompGetCount(pixac);
1802  fprintf(fp, "\nPixacomp Version %d\n", PIXACOMP_VERSION_NUMBER);
1803  fprintf(fp, "Number of pixcomp = %d\n", n);
1804  fprintf(fp, "Offset of index into array = %d", pixac->offset);
1805  boxaWriteStream(fp, pixac->boxa);
1806  for (i = 0; i < n; i++) {
1807  if ((pixc = pixacompGetPixcomp(pixac, pixac->offset + i, L_NOCOPY))
1808  == NULL)
1809  return ERROR_INT("pixc not found", procName, 1);
1810  fprintf(fp, "\nPixcomp[%d]: w = %d, h = %d, d = %d\n",
1811  i, pixc->w, pixc->h, pixc->d);
1812  fprintf(fp, " comptype = %d, size = %lu, cmapflag = %d\n",
1813  pixc->comptype, (unsigned long)pixc->size, pixc->cmapflag);
1814  fprintf(fp, " xres = %d, yres = %d\n", pixc->xres, pixc->yres);
1815  fwrite(pixc->data, 1, pixc->size, fp);
1816  fprintf(fp, "\n");
1817  }
1818  return 0;
1819 }
1820 
1821 
1835 l_int32
1836 pixacompWriteMem(l_uint8 **pdata,
1837  size_t *psize,
1838  PIXAC *pixac)
1839 {
1840 l_int32 ret;
1841 FILE *fp;
1842 
1843  PROCNAME("pixacompWriteMem");
1844 
1845  if (pdata) *pdata = NULL;
1846  if (psize) *psize = 0;
1847  if (!pdata)
1848  return ERROR_INT("&data not defined", procName, 1);
1849  if (!psize)
1850  return ERROR_INT("&size not defined", procName, 1);
1851  if (!pixac)
1852  return ERROR_INT("&pixac not defined", procName, 1);
1853 
1854 #if HAVE_FMEMOPEN
1855  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
1856  return ERROR_INT("stream not opened", procName, 1);
1857  ret = pixacompWriteStream(fp, pixac);
1858 #else
1859  L_INFO("work-around: writing to a temp file\n", procName);
1860  #ifdef _WIN32
1861  if ((fp = fopenWriteWinTempfile()) == NULL)
1862  return ERROR_INT("tmpfile stream not opened", procName, 1);
1863  #else
1864  if ((fp = tmpfile()) == NULL)
1865  return ERROR_INT("tmpfile stream not opened", procName, 1);
1866  #endif /* _WIN32 */
1867  ret = pixacompWriteStream(fp, pixac);
1868  rewind(fp);
1869  *pdata = l_binaryReadStream(fp, psize);
1870 #endif /* HAVE_FMEMOPEN */
1871  fclose(fp);
1872  return ret;
1873 }
1874 
1875 
1876 /*--------------------------------------------------------------------*
1877  * Conversion to pdf *
1878  *--------------------------------------------------------------------*/
1905 l_int32
1907  l_int32 res,
1908  l_float32 scalefactor,
1909  l_int32 type,
1910  l_int32 quality,
1911  const char *title,
1912  const char *fileout)
1913 {
1914 l_uint8 *data;
1915 l_int32 ret;
1916 size_t nbytes;
1917 
1918  PROCNAME("pixacompConvertToPdf");
1919 
1920  if (!pixac)
1921  return ERROR_INT("pixac not defined", procName, 1);
1922 
1923  ret = pixacompConvertToPdfData(pixac, res, scalefactor, type, quality,
1924  title, &data, &nbytes);
1925  if (ret) {
1926  LEPT_FREE(data);
1927  return ERROR_INT("conversion to pdf failed", procName, 1);
1928  }
1929 
1930  ret = l_binaryWrite(fileout, "w", data, nbytes);
1931  LEPT_FREE(data);
1932  if (ret)
1933  L_ERROR("pdf data not written to file\n", procName);
1934  return ret;
1935 }
1936 
1937 
1957 l_int32
1959  l_int32 res,
1960  l_float32 scalefactor,
1961  l_int32 type,
1962  l_int32 quality,
1963  const char *title,
1964  l_uint8 **pdata,
1965  size_t *pnbytes)
1966 {
1967 l_uint8 *imdata;
1968 l_int32 i, n, ret, scaledres, pagetype;
1969 size_t imbytes;
1970 L_BYTEA *ba;
1971 PIX *pixs, *pix;
1972 L_PTRA *pa_data;
1973 
1974  PROCNAME("pixacompConvertToPdfData");
1975 
1976  if (!pdata)
1977  return ERROR_INT("&data not defined", procName, 1);
1978  *pdata = NULL;
1979  if (!pnbytes)
1980  return ERROR_INT("&nbytes not defined", procName, 1);
1981  *pnbytes = 0;
1982  if (!pixac)
1983  return ERROR_INT("pixac not defined", procName, 1);
1984  if (scalefactor <= 0.0) scalefactor = 1.0;
1985  if (type < L_DEFAULT_ENCODE || type > L_FLATE_ENCODE) {
1986  L_WARNING("invalid compression type; using per-page default\n",
1987  procName);
1988  type = L_DEFAULT_ENCODE;
1989  }
1990 
1991  /* Generate all the encoded pdf strings */
1992  n = pixacompGetCount(pixac);
1993  pa_data = ptraCreate(n);
1994  for (i = 0; i < n; i++) {
1995  if ((pixs =
1996  pixacompGetPix(pixac, pixacompGetOffset(pixac) + i)) == NULL) {
1997  L_ERROR("pix[%d] not retrieved\n", procName, i);
1998  continue;
1999  }
2000  if (pixGetWidth(pixs) == 1) { /* used sometimes as placeholders */
2001  L_INFO("placeholder image[%d] has w = 1\n", procName, i);
2002  pixDestroy(&pixs);
2003  continue;
2004  }
2005  if (scalefactor != 1.0)
2006  pix = pixScale(pixs, scalefactor, scalefactor);
2007  else
2008  pix = pixClone(pixs);
2009  pixDestroy(&pixs);
2010  scaledres = (l_int32)(res * scalefactor);
2011  if (type != L_DEFAULT_ENCODE) {
2012  pagetype = type;
2013  } else if (selectDefaultPdfEncoding(pix, &pagetype) != 0) {
2014  L_ERROR("encoding type selection failed for pix[%d]\n",
2015  procName, i);
2016  pixDestroy(&pix);
2017  continue;
2018  }
2019  ret = pixConvertToPdfData(pix, pagetype, quality, &imdata, &imbytes,
2020  0, 0, scaledres, title, NULL, 0);
2021  pixDestroy(&pix);
2022  if (ret) {
2023  L_ERROR("pdf encoding failed for pix[%d]\n", procName, i);
2024  continue;
2025  }
2026  ba = l_byteaInitFromMem(imdata, imbytes);
2027  LEPT_FREE(imdata);
2028  ptraAdd(pa_data, ba);
2029  }
2030  ptraGetActualCount(pa_data, &n);
2031  if (n == 0) {
2032  L_ERROR("no pdf files made\n", procName);
2033  ptraDestroy(&pa_data, FALSE, FALSE);
2034  return 1;
2035  }
2036 
2037  /* Concatenate them */
2038  ret = ptraConcatenatePdfToData(pa_data, NULL, pdata, pnbytes);
2039 
2040  ptraGetActualCount(pa_data, &n); /* recalculate in case it changes */
2041  for (i = 0; i < n; i++) {
2042  ba = (L_BYTEA *)ptraRemove(pa_data, i, L_NO_COMPACTION);
2043  l_byteaDestroy(&ba);
2044  }
2045  ptraDestroy(&pa_data, FALSE, FALSE);
2046  return ret;
2047 }
2048 
2049 
2050 /*--------------------------------------------------------------------*
2051  * Output for debugging *
2052  *--------------------------------------------------------------------*/
2061 l_int32
2063  PIXAC *pixac,
2064  const char *text)
2065 {
2066 l_int32 i, n, nboxes;
2067 PIXC *pixc;
2068 
2069  PROCNAME("pixacompWriteStreamInfo");
2070 
2071  if (!fp)
2072  return ERROR_INT("fp not defined", procName, 1);
2073  if (!pixac)
2074  return ERROR_INT("pixac not defined", procName, 1);
2075 
2076  if (text)
2077  fprintf(fp, "Pixacomp Info for %s:\n", text);
2078  else
2079  fprintf(fp, "Pixacomp Info:\n");
2080  n = pixacompGetCount(pixac);
2081  nboxes = pixacompGetBoxaCount(pixac);
2082  fprintf(fp, "Number of pixcomp: %d\n", n);
2083  fprintf(fp, "Size of pixcomp array alloc: %d\n", pixac->nalloc);
2084  fprintf(fp, "Offset of index into array: %d\n", pixac->offset);
2085  if (nboxes > 0)
2086  fprintf(fp, "Boxa has %d boxes\n", nboxes);
2087  else
2088  fprintf(fp, "Boxa is empty\n");
2089  for (i = 0; i < n; i++) {
2090  pixc = pixacompGetPixcomp(pixac, pixac->offset + i, L_NOCOPY);
2091  pixcompWriteStreamInfo(fp, pixc, NULL);
2092  }
2093  return 0;
2094 }
2095 
2096 
2105 l_int32
2107  PIXC *pixc,
2108  const char *text)
2109 {
2110  PROCNAME("pixcompWriteStreamInfo");
2111 
2112  if (!fp)
2113  return ERROR_INT("fp not defined", procName, 1);
2114  if (!pixc)
2115  return ERROR_INT("pixc not defined", procName, 1);
2116 
2117  if (text)
2118  fprintf(fp, " Pixcomp Info for %s:", text);
2119  else
2120  fprintf(fp, " Pixcomp Info:");
2121  fprintf(fp, " width = %d, height = %d, depth = %d\n",
2122  pixc->w, pixc->h, pixc->d);
2123  fprintf(fp, " xres = %d, yres = %d, size in bytes = %lu\n",
2124  pixc->xres, pixc->yres, (unsigned long)pixc->size);
2125  if (pixc->cmapflag)
2126  fprintf(fp, " has colormap\n");
2127  else
2128  fprintf(fp, " no colormap\n");
2129  if (pixc->comptype < NumImageFileFormatExtensions) {
2130  fprintf(fp, " comptype = %s (%d)\n",
2131  ImageFileFormatExtensions[pixc->comptype], pixc->comptype);
2132  } else {
2133  fprintf(fp, " Error!! Invalid comptype index: %d\n", pixc->comptype);
2134  }
2135  return 0;
2136 }
2137 
2138 
2161 PIX *
2163  l_int32 outdepth,
2164  l_int32 tilewidth,
2165  l_int32 ncols,
2166  l_int32 background,
2167  l_int32 spacing,
2168  l_int32 border)
2169 {
2170 PIX *pixd;
2171 PIXA *pixa;
2172 
2173  PROCNAME("pixacompDisplayTiledAndScaled");
2174 
2175  if (!pixac)
2176  return (PIX *)ERROR_PTR("pixac not defined", procName, NULL);
2177 
2178  if ((pixa = pixaCreateFromPixacomp(pixac, L_COPY)) == NULL)
2179  return (PIX *)ERROR_PTR("pixa not made", procName, NULL);
2180 
2181  pixd = pixaDisplayTiledAndScaled(pixa, outdepth, tilewidth, ncols,
2182  background, spacing, border);
2183  pixaDestroy(&pixa);
2184  return pixd;
2185 }
2186 
BOXA * boxaReadStream(FILE *fp)
boxaReadStream()
Definition: boxbasic.c:2122
l_int32 selectDefaultPdfEncoding(PIX *pix, l_int32 *ptype)
selectDefaultPdfEncoding()
Definition: pdfio1.c:454
l_int32 pixacompReplacePix(PIXAC *pixac, l_int32 index, PIX *pix, l_int32 comptype)
pixacompReplacePix()
Definition: pixcomp.c:986
l_int32 boxaExtendArray(BOXA *boxa)
boxaExtendArray()
Definition: boxbasic.c:662
PIXAC * pixacompCreateFromPixa(PIXA *pixa, l_int32 comptype, l_int32 accesstype)
pixacompCreateFromPixa()
Definition: pixcomp.c:679
PIX * pixacompGetPix(PIXAC *pixac, l_int32 index)
pixacompGetPix()
Definition: pixcomp.c:1159
l_int32 n
Definition: pix.h:494
l_int32 pixacompGetOffset(PIXAC *pixac)
pixacompGetOffset()
Definition: pixcomp.c:1369
l_int32 ptraConcatenatePdfToData(L_PTRA *pa_data, SARRAY *sa, l_uint8 **pdata, size_t *pnbytes)
ptraConcatenatePdfToData()
Definition: pdfio2.c:306
struct Boxa * boxa
Definition: pix.h:460
PIXC * pixcompCreateFromPix(PIX *pix, l_int32 comptype)
pixcompCreateFromPix()
Definition: pixcomp.c:174
l_int32 pixcompGetDimensions(PIXC *pixc, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixcompGetDimensions()
Definition: pixcomp.c:418
l_int32 boxaWriteStream(FILE *fp, BOXA *boxa)
boxaWriteStream()
Definition: boxbasic.c:2225
l_int32 pixacompGetBoxaCount(PIXAC *pixac)
pixacompGetBoxaCount()
Definition: pixcomp.c:1249
PIX * pixCreateFromPixcomp(PIXC *pixc)
pixCreateFromPixcomp()
Definition: pixcomp.c:497
PIXA * pixaCreate(l_int32 n)
pixaCreate()
Definition: pixabasic.c:161
PIXAC * pixacompRead(const char *filename)
pixacompRead()
Definition: pixcomp.c:1597
l_int32 pixacompAddPix(PIXAC *pixac, PIX *pix, l_int32 comptype)
pixacompAddPix()
Definition: pixcomp.c:866
l_int32 pixConvertToPdfData(PIX *pix, l_int32 type, l_int32 quality, l_uint8 **pdata, size_t *pnbytes, l_int32 x, l_int32 y, l_int32 res, const char *title, L_PDF_DATA **plpd, l_int32 position)
pixConvertToPdfData()
Definition: pdfio2.c:181
Definition: pix.h:704
char * stringNew(const char *src)
stringNew()
Definition: utils2.c:202
PIXA * pixaCreateFromPixacomp(PIXAC *pixac, l_int32 accesstype)
pixaCreateFromPixacomp()
Definition: pixcomp.c:1424
size_t nbytes
Definition: pixalloc.c:120
l_int32 comptype
Definition: pix.h:642
l_int32 boxaAddBox(BOXA *boxa, BOX *box, l_int32 copyflag)
boxaAddBox()
Definition: boxbasic.c:615
l_int32 pixacompSetOffset(PIXAC *pixac, l_int32 offset)
pixacompSetOffset()
Definition: pixcomp.c:1394
PIXAC * pixacompReadMem(const l_uint8 *data, size_t size)
pixacompReadMem()
Definition: pixcomp.c:1722
l_int32 n
Definition: pix.h:660
l_int32 pixacompGetPixDimensions(PIXAC *pixac, l_int32 index, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixacompGetPixDimensions()
Definition: pixcomp.c:1193
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
Definition: pix1.c:302
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
Definition: utils2.c:1670
l_int32 pixReadHeaderMem(const l_uint8 *data, size_t size, l_int32 *pformat, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
pixReadHeaderMem()
Definition: readfile.c:948
BOXA * boxaCopy(BOXA *boxa, l_int32 copyflag)
boxaCopy()
Definition: boxbasic.c:531
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
Definition: boxbasic.c:577
PIXAC * pixacompCreate(l_int32 n)
pixacompCreate()
Definition: pixcomp.c:550
l_uint8 * l_binaryCopy(l_uint8 *datas, size_t size)
l_binaryCopy()
Definition: utils2.c:1469
l_int32 boxaJoin(BOXA *boxad, BOXA *boxas, l_int32 istart, l_int32 iend)
boxaJoin()
Definition: boxfunc1.c:2312
l_int32 w
Definition: pix.h:635
BOX * pixacompGetBox(PIXAC *pixac, l_int32 index, l_int32 accesstype)
pixacompGetBox()
Definition: pixcomp.c:1283
PIX * pixReadMem(const l_uint8 *data, size_t size)
pixReadMem()
Definition: readfile.c:827
Definition: pix.h:492
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
Definition: utils2.c:1102
l_int32 findFileFormat(const char *filename, l_int32 *pformat)
findFileFormat()
Definition: readfile.c:568
l_int32 ptraAdd(L_PTRA *pa, void *item)
ptraAdd()
Definition: ptra.c:242
l_int32 pixacompGetBoxGeometry(PIXAC *pixac, l_int32 index, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
pixacompGetBoxGeometry()
Definition: pixcomp.c:1329
Definition: array.h:116
l_int32 d
Definition: pix.h:637
l_int32 h
Definition: pix.h:636
BOX * boxClone(BOX *box)
boxClone()
Definition: boxbasic.c:251
l_int32 pixacompConvertToPdf(PIXAC *pixac, l_int32 res, l_float32 scalefactor, l_int32 type, l_int32 quality, const char *title, const char *fileout)
pixacompConvertToPdf()
Definition: pixcomp.c:1906
#define PIXACOMP_VERSION_NUMBER
Definition: pix.h:655
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
Definition: utils2.c:1154
static const l_int32 L_INSERT
Definition: pix.h:710
L_PTRA * ptraCreate(l_int32 n)
ptraCreate()
Definition: ptra.c:139
l_int32 pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
Definition: pixabasic.c:493
PIXAC * pixacompCreateWithInit(l_int32 n, l_int32 offset, PIX *pix, l_int32 comptype)
pixacompCreateWithInit()
Definition: pixcomp.c:617
l_int32 pixcompDetermineFormat(l_int32 comptype, l_int32 d, l_int32 cmapflag, l_int32 *pformat)
pixcompDetermineFormat()
Definition: pixcomp.c:455
l_int32 pixacompAddPixcomp(PIXAC *pixac, PIXC *pixc, l_int32 copyflag)
pixacompAddPixcomp()
Definition: pixcomp.c:908
PIXAC * pixacompInterleave(PIXAC *pixac1, PIXAC *pixac2)
pixacompInterleave()
Definition: pixcomp.c:1535
Definition: ptra.h:51
l_int32 boxGetGeometry(BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
Definition: boxbasic.c:309
l_int32 nalloc
Definition: pix.h:661
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
Definition: utils2.c:1716
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:675
l_int32 xres
Definition: pix.h:638
PIXAC * pixacompCreateFromFiles(const char *dirname, const char *substr, l_int32 comptype)
pixacompCreateFromFiles()
Definition: pixcomp.c:738
PIXAC * pixacompReadStream(FILE *fp)
pixacompReadStream()
Definition: pixcomp.c:1624
PIX * pixClone(PIX *pixs)
pixClone()
Definition: pix1.c:517
PIX * pixaDisplayTiledAndScaled(PIXA *pixa, l_int32 outdepth, l_int32 tilewidth, l_int32 ncols, l_int32 background, l_int32 spacing, l_int32 border)
pixaDisplayTiledAndScaled()
Definition: pixafunc2.c:1122
PIXC * pixacompGetPixcomp(PIXAC *pixac, l_int32 index, l_int32 copyflag)
pixacompGetPixcomp()
Definition: pixcomp.c:1122
Definition: pix.h:658
l_int32 pixGetResolution(PIX *pix, l_int32 *pxres, l_int32 *pyres)
pixGetResolution()
Definition: pix1.c:1297
l_int32 ptraGetActualCount(L_PTRA *pa, l_int32 *pcount)
ptraGetActualCount()
Definition: ptra.c:727
void pixacompDestroy(PIXAC **ppixac)
pixacompDestroy()
Definition: pixcomp.c:819
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:545
static l_int32 pixacompExtendArray(PIXAC *pixac)
pixacompExtendArray()
Definition: pixcomp.c:951
struct Boxa * boxa
Definition: pix.h:664
l_int32 pixacompWriteMem(l_uint8 **pdata, size_t *psize, PIXAC *pixac)
pixacompWriteMem()
Definition: pixcomp.c:1836
l_int32 pixacompJoin(PIXAC *pixacd, PIXAC *pixacs, l_int32 istart, l_int32 iend)
pixacompJoin()
Definition: pixcomp.c:1482
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
Definition: sarray1.c:1710
Definition: pix.h:454
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1636
l_int32 pixSetResolution(PIX *pix, l_int32 xres, l_int32 yres)
pixSetResolution()
Definition: pix1.c:1323
PIXAC * pixacompCreateFromSA(SARRAY *sa, l_int32 comptype)
pixacompCreateFromSA()
Definition: pixcomp.c:777
Definition: pix.h:633
void ptraDestroy(L_PTRA **ppa, l_int32 freeflag, l_int32 warnflag)
ptraDestroy()
Definition: ptra.c:185
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1593
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
Definition: utils2.c:1200
PIX * pixacompDisplayTiledAndScaled(PIXAC *pixac, l_int32 outdepth, l_int32 tilewidth, l_int32 ncols, l_int32 background, l_int32 spacing, l_int32 border)
pixacompDisplayTiledAndScaled()
Definition: pixcomp.c:2162
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:615
PIX * pixRead(const char *filename)
pixRead()
Definition: readfile.c:189
char * pixGetText(PIX *pix)
pixGetText()
Definition: pix1.c:1442
l_int32 yres
Definition: pix.h:640
l_int32 l_binaryWrite(const char *filename, const char *operation, void *data, size_t nbytes)
l_binaryWrite()
Definition: utils2.c:1367
PIXC * pixcompCreateFromString(l_uint8 *data, size_t size, l_int32 copyflag)
pixcompCreateFromString()
Definition: pixcomp.c:231
char * text
Definition: pix.h:644
l_int32 pixacompAddBox(PIXAC *pixac, BOX *box, l_int32 copyflag)
pixacompAddBox()
Definition: pixcomp.c:1066
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
Definition: pixabasic.c:660
PIXC * pixcompCreateFromFile(const char *filename, l_int32 comptype)
pixcompCreateFromFile()
Definition: pixcomp.c:280
Definition: pix.h:705
void * ptraRemove(L_PTRA *pa, l_int32 index, l_int32 flag)
ptraRemove()
Definition: ptra.c:434
BOX * boxCopy(BOX *box)
boxCopy()
Definition: boxbasic.c:229
Definition: pix.h:134
PIXC * pixcompCopy(PIXC *pixcs)
pixcompCopy()
Definition: pixcomp.c:373
void pixcompDestroy(PIXC **ppixc)
pixcompDestroy()
Definition: pixcomp.c:343
Definition: pix.h:706
l_int32 cmapflag
Definition: pix.h:645
BOXA * boxaCreate(l_int32 n)
boxaCreate()
Definition: boxbasic.c:496
l_uint8 * data
Definition: pix.h:646
void boxDestroy(BOX **pbox)
boxDestroy()
Definition: boxbasic.c:277
l_int32 pixacompReplacePixcomp(PIXAC *pixac, l_int32 index, PIXC *pixc)
pixacompReplacePixcomp()
Definition: pixcomp.c:1031
l_int32 boxaGetCount(BOXA *boxa)
boxaGetCount()
Definition: boxbasic.c:715
l_int32 pixacompGetCount(PIXAC *pixac)
pixacompGetCount()
Definition: pixcomp.c:1094
void l_byteaDestroy(L_BYTEA **pba)
l_byteaDestroy()
Definition: bytearray.c:245
l_int32 pixGetDimensions(PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1052
l_int32 offset
Definition: pix.h:662
struct PixComp ** pixc
Definition: pix.h:663
struct Box ** box
Definition: pix.h:497
l_int32 pixacompWriteStreamInfo(FILE *fp, PIXAC *pixac, const char *text)
pixacompWriteStreamInfo()
Definition: pixcomp.c:2062
l_int32 pixacompWrite(const char *filename, PIXAC *pixac)
pixacompWrite()
Definition: pixcomp.c:1757
Definition: pix.h:480
PIX * pixScale(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScale()
Definition: scale1.c:243
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
Definition: pixabasic.c:398
l_int32 pixacompWriteStream(FILE *fp, PIXAC *pixac)
pixacompWriteStream()
Definition: pixcomp.c:1788
L_BYTEA * l_byteaInitFromMem(l_uint8 *data, size_t size)
l_byteaInitFromMem()
Definition: bytearray.c:122
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
Definition: pixabasic.c:620
l_int32 pixcompWriteStreamInfo(FILE *fp, PIXC *pixc, const char *text)
pixcompWriteStreamInfo()
Definition: pixcomp.c:2106
BOXA * pixacompGetBoxa(PIXAC *pixac, l_int32 accesstype)
pixacompGetBoxa()
Definition: pixcomp.c:1225
size_t size
Definition: pix.h:647
l_int32 pixSetText(PIX *pix, const char *textstring)
pixSetText()
Definition: pix1.c:1466
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:349
Definition: array.h:126
l_int32 pixacompConvertToPdfData(PIXAC *pixac, l_int32 res, l_float32 scalefactor, l_int32 type, l_int32 quality, const char *title, l_uint8 **pdata, size_t *pnbytes)
pixacompConvertToPdfData()
Definition: pixcomp.c:1958
BOXA * pixaGetBoxa(PIXA *pixa, l_int32 accesstype)
pixaGetBoxa()
Definition: pixabasic.c:729