Leptonica  1.73
Image processing and image analysis suite
fpix1.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 
122 #include <string.h>
123 #include "allheaders.h"
124 
125 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20; /* must be > 0 */
126 
127  /* Static functions */
128 static l_int32 fpixaExtendArray(FPIXA *fpixa);
129 static l_int32 fpixaExtendArrayToSize(FPIXA *fpixa, l_int32 size);
130 
131 
132 /*--------------------------------------------------------------------*
133  * FPix Create/copy/destroy *
134  *--------------------------------------------------------------------*/
148 FPIX *
149 fpixCreate(l_int32 width,
150  l_int32 height)
151 {
152 l_float32 *data;
153 l_uint64 bignum;
154 FPIX *fpixd;
155 
156  PROCNAME("fpixCreate");
157 
158  if (width <= 0)
159  return (FPIX *)ERROR_PTR("width must be > 0", procName, NULL);
160  if (height <= 0)
161  return (FPIX *)ERROR_PTR("height must be > 0", procName, NULL);
162 
163  /* Avoid overflow in malloc arg, malicious or otherwise */
164  bignum = 4L * width * height; /* max number of bytes requested */
165  if (bignum > ((1LL << 31) - 1)) {
166  L_ERROR("requested w = %d, h = %d\n", procName, width, height);
167  return (FPIX *)ERROR_PTR("requested bytes >= 2^31", procName, NULL);
168  }
169 
170  if ((fpixd = (FPIX *)LEPT_CALLOC(1, sizeof(FPIX))) == NULL)
171  return (FPIX *)ERROR_PTR("LEPT_CALLOC fail for fpixd", procName, NULL);
172  fpixSetDimensions(fpixd, width, height);
173  fpixSetWpl(fpixd, width); /* 4-byte words */
174  fpixd->refcount = 1;
175 
176  data = (l_float32 *)LEPT_CALLOC(width * height, sizeof(l_float32));
177  if (!data) {
178  fpixDestroy(&fpixd);
179  return (FPIX *)ERROR_PTR("LEPT_CALLOC fail for data", procName, NULL);
180  }
181  fpixSetData(fpixd, data);
182 
183  return fpixd;
184 }
185 
186 
200 FPIX *
202 {
203 l_int32 w, h;
204 FPIX *fpixd;
205 
206  PROCNAME("fpixCreateTemplate");
207 
208  if (!fpixs)
209  return (FPIX *)ERROR_PTR("fpixs not defined", procName, NULL);
210 
211  fpixGetDimensions(fpixs, &w, &h);
212  fpixd = fpixCreate(w, h);
213  fpixCopyResolution(fpixd, fpixs);
214  return fpixd;
215 }
216 
217 
229 FPIX *
231 {
232  PROCNAME("fpixClone");
233 
234  if (!fpix)
235  return (FPIX *)ERROR_PTR("fpix not defined", procName, NULL);
236  fpixChangeRefcount(fpix, 1);
237 
238  return fpix;
239 }
240 
241 
272 FPIX *
273 fpixCopy(FPIX *fpixd, /* can be null */
274  FPIX *fpixs)
275 {
276 l_int32 w, h, bytes;
277 l_float32 *datas, *datad;
278 
279  PROCNAME("fpixCopy");
280 
281  if (!fpixs)
282  return (FPIX *)ERROR_PTR("fpixs not defined", procName, NULL);
283  if (fpixs == fpixd)
284  return fpixd;
285 
286  /* Total bytes in image data */
287  fpixGetDimensions(fpixs, &w, &h);
288  bytes = 4 * w * h;
289 
290  /* If we're making a new fpix ... */
291  if (!fpixd) {
292  if ((fpixd = fpixCreateTemplate(fpixs)) == NULL)
293  return (FPIX *)ERROR_PTR("fpixd not made", procName, NULL);
294  datas = fpixGetData(fpixs);
295  datad = fpixGetData(fpixd);
296  memcpy((char *)datad, (char *)datas, bytes);
297  return fpixd;
298  }
299 
300  /* Reallocate image data if sizes are different */
301  fpixResizeImageData(fpixd, fpixs);
302 
303  /* Copy data */
304  fpixCopyResolution(fpixd, fpixs);
305  datas = fpixGetData(fpixs);
306  datad = fpixGetData(fpixd);
307  memcpy((char*)datad, (char*)datas, bytes);
308  return fpixd;
309 }
310 
311 
326 l_int32
328  FPIX *fpixs)
329 {
330 l_int32 ws, hs, wd, hd, bytes;
331 l_float32 *data;
332 
333  PROCNAME("fpixResizeImageData");
334 
335  if (!fpixs)
336  return ERROR_INT("fpixs not defined", procName, 1);
337  if (!fpixd)
338  return ERROR_INT("fpixd not defined", procName, 1);
339 
340  fpixGetDimensions(fpixs, &ws, &hs);
341  fpixGetDimensions(fpixd, &wd, &hd);
342  if (ws == wd && hs == hd) /* nothing to do */
343  return 0;
344 
345  fpixSetDimensions(fpixd, ws, hs);
346  fpixSetWpl(fpixd, ws);
347  bytes = 4 * ws * hs;
348  data = fpixGetData(fpixd);
349  if (data) LEPT_FREE(data);
350  if ((data = (l_float32 *)LEPT_MALLOC(bytes)) == NULL)
351  return ERROR_INT("LEPT_MALLOC fail for data", procName, 1);
352  fpixSetData(fpixd, data);
353  return 0;
354 }
355 
356 
369 void
371 {
372 l_float32 *data;
373 FPIX *fpix;
374 
375  PROCNAME("fpixDestroy");
376 
377  if (!pfpix) {
378  L_WARNING("ptr address is null!\n", procName);
379  return;
380  }
381 
382  if ((fpix = *pfpix) == NULL)
383  return;
384 
385  /* Decrement the ref count. If it is 0, destroy the fpix. */
386  fpixChangeRefcount(fpix, -1);
387  if (fpixGetRefcount(fpix) <= 0) {
388  if ((data = fpixGetData(fpix)) != NULL)
389  LEPT_FREE(data);
390  LEPT_FREE(fpix);
391  }
392 
393  *pfpix = NULL;
394  return;
395 }
396 
397 
398 /*--------------------------------------------------------------------*
399  * FPix Accessors *
400  *--------------------------------------------------------------------*/
408 l_int32
410  l_int32 *pw,
411  l_int32 *ph)
412 {
413  PROCNAME("fpixGetDimensions");
414 
415  if (!pw && !ph)
416  return ERROR_INT("no return val requested", procName, 1);
417  if (pw) *pw = 0;
418  if (ph) *ph = 0;
419  if (!fpix)
420  return ERROR_INT("fpix not defined", procName, 1);
421  if (pw) *pw = fpix->w;
422  if (ph) *ph = fpix->h;
423  return 0;
424 }
425 
426 
434 l_int32
436  l_int32 w,
437  l_int32 h)
438 {
439  PROCNAME("fpixSetDimensions");
440 
441  if (!fpix)
442  return ERROR_INT("fpix not defined", procName, 1);
443  fpix->w = w;
444  fpix->h = h;
445  return 0;
446 }
447 
448 
455 l_int32
457 {
458  PROCNAME("fpixGetWpl");
459 
460  if (!fpix)
461  return ERROR_INT("fpix not defined", procName, UNDEF);
462  return fpix->wpl;
463 }
464 
465 
473 l_int32
475  l_int32 wpl)
476 {
477  PROCNAME("fpixSetWpl");
478 
479  if (!fpix)
480  return ERROR_INT("fpix not defined", procName, 1);
481 
482  fpix->wpl = wpl;
483  return 0;
484 }
485 
486 
493 l_int32
495 {
496  PROCNAME("fpixGetRefcount");
497 
498  if (!fpix)
499  return ERROR_INT("fpix not defined", procName, UNDEF);
500  return fpix->refcount;
501 }
502 
503 
511 l_int32
513  l_int32 delta)
514 {
515  PROCNAME("fpixChangeRefcount");
516 
517  if (!fpix)
518  return ERROR_INT("fpix not defined", procName, 1);
519 
520  fpix->refcount += delta;
521  return 0;
522 }
523 
524 
532 l_int32
534  l_int32 *pxres,
535  l_int32 *pyres)
536 {
537  PROCNAME("fpixGetResolution");
538 
539  if (!fpix)
540  return ERROR_INT("fpix not defined", procName, 1);
541  if (pxres) *pxres = fpix->xres;
542  if (pyres) *pyres = fpix->yres;
543  return 0;
544 }
545 
546 
554 l_int32
556  l_int32 xres,
557  l_int32 yres)
558 {
559  PROCNAME("fpixSetResolution");
560 
561  if (!fpix)
562  return ERROR_INT("fpix not defined", procName, 1);
563 
564  fpix->xres = xres;
565  fpix->yres = yres;
566  return 0;
567 }
568 
569 
576 l_int32
578  FPIX *fpixs)
579 {
580 l_int32 xres, yres;
581  PROCNAME("fpixCopyResolution");
582 
583  if (!fpixs || !fpixd)
584  return ERROR_INT("fpixs and fpixd not both defined", procName, 1);
585 
586  fpixGetResolution(fpixs, &xres, &yres);
587  fpixSetResolution(fpixd, xres, yres);
588  return 0;
589 }
590 
591 
598 l_float32 *
600 {
601  PROCNAME("fpixGetData");
602 
603  if (!fpix)
604  return (l_float32 *)ERROR_PTR("fpix not defined", procName, NULL);
605  return fpix->data;
606 }
607 
608 
616 l_int32
618  l_float32 *data)
619 {
620  PROCNAME("fpixSetData");
621 
622  if (!fpix)
623  return ERROR_INT("fpix not defined", procName, 1);
624 
625  fpix->data = data;
626  return 0;
627 }
628 
629 
638 l_int32
640  l_int32 x,
641  l_int32 y,
642  l_float32 *pval)
643 {
644 l_int32 w, h;
645 
646  PROCNAME("fpixGetPixel");
647 
648  if (!pval)
649  return ERROR_INT("pval not defined", procName, 1);
650  *pval = 0.0;
651  if (!fpix)
652  return ERROR_INT("fpix not defined", procName, 1);
653 
654  fpixGetDimensions(fpix, &w, &h);
655  if (x < 0 || x >= w)
656  return ERROR_INT("x out of bounds", procName, 1);
657  if (y < 0 || y >= h)
658  return ERROR_INT("y out of bounds", procName, 1);
659 
660  *pval = *(fpix->data + y * w + x);
661  return 0;
662 }
663 
664 
673 l_int32
675  l_int32 x,
676  l_int32 y,
677  l_float32 val)
678 {
679 l_int32 w, h;
680 
681  PROCNAME("fpixSetPixel");
682 
683  if (!fpix)
684  return ERROR_INT("fpix not defined", procName, 1);
685 
686  fpixGetDimensions(fpix, &w, &h);
687  if (x < 0 || x >= w)
688  return ERROR_INT("x out of bounds", procName, 1);
689  if (y < 0 || y >= h)
690  return ERROR_INT("y out of bounds", procName, 1);
691 
692  *(fpix->data + y * w + x) = val;
693  return 0;
694 }
695 
696 
697 /*--------------------------------------------------------------------*
698  * FPixa Create/copy/destroy *
699  *--------------------------------------------------------------------*/
706 FPIXA *
707 fpixaCreate(l_int32 n)
708 {
709 FPIXA *fpixa;
710 
711  PROCNAME("fpixaCreate");
712 
713  if (n <= 0)
714  n = INITIAL_PTR_ARRAYSIZE;
715 
716  if ((fpixa = (FPIXA *)LEPT_CALLOC(1, sizeof(FPIXA))) == NULL)
717  return (FPIXA *)ERROR_PTR("fpixa not made", procName, NULL);
718  fpixa->n = 0;
719  fpixa->nalloc = n;
720  fpixa->refcount = 1;
721 
722  if ((fpixa->fpix = (FPIX **)LEPT_CALLOC(n, sizeof(FPIX *))) == NULL) {
723  fpixaDestroy(&fpixa);
724  return (FPIXA *)ERROR_PTR("fpixa ptrs not made", procName, NULL);
725  }
726 
727  return fpixa;
728 }
729 
730 
746 FPIXA *
748  l_int32 copyflag)
749 {
750 l_int32 i;
751 FPIX *fpixc;
752 FPIXA *fpixac;
753 
754  PROCNAME("fpixaCopy");
755 
756  if (!fpixa)
757  return (FPIXA *)ERROR_PTR("fpixa not defined", procName, NULL);
758 
759  if (copyflag == L_CLONE) {
760  fpixaChangeRefcount(fpixa, 1);
761  return fpixa;
762  }
763 
764  if (copyflag != L_COPY && copyflag != L_COPY_CLONE)
765  return (FPIXA *)ERROR_PTR("invalid copyflag", procName, NULL);
766 
767  if ((fpixac = fpixaCreate(fpixa->n)) == NULL)
768  return (FPIXA *)ERROR_PTR("fpixac not made", procName, NULL);
769  for (i = 0; i < fpixa->n; i++) {
770  if (copyflag == L_COPY)
771  fpixc = fpixaGetFPix(fpixa, i, L_COPY);
772  else /* copy-clone */
773  fpixc = fpixaGetFPix(fpixa, i, L_CLONE);
774  fpixaAddFPix(fpixac, fpixc, L_INSERT);
775  }
776 
777  return fpixac;
778 }
779 
780 
793 void
795 {
796 l_int32 i;
797 FPIXA *fpixa;
798 
799  PROCNAME("fpixaDestroy");
800 
801  if (pfpixa == NULL) {
802  L_WARNING("ptr address is NULL!\n", procName);
803  return;
804  }
805 
806  if ((fpixa = *pfpixa) == NULL)
807  return;
808 
809  /* Decrement the refcount. If it is 0, destroy the pixa. */
810  fpixaChangeRefcount(fpixa, -1);
811  if (fpixa->refcount <= 0) {
812  for (i = 0; i < fpixa->n; i++)
813  fpixDestroy(&fpixa->fpix[i]);
814  LEPT_FREE(fpixa->fpix);
815  LEPT_FREE(fpixa);
816  }
817 
818  *pfpixa = NULL;
819  return;
820 }
821 
822 
823 /*--------------------------------------------------------------------*
824  * FPixa addition *
825  *--------------------------------------------------------------------*/
834 l_int32
836  FPIX *fpix,
837  l_int32 copyflag)
838 {
839 l_int32 n;
840 FPIX *fpixc;
841 
842  PROCNAME("fpixaAddFPix");
843 
844  if (!fpixa)
845  return ERROR_INT("fpixa not defined", procName, 1);
846  if (!fpix)
847  return ERROR_INT("fpix not defined", procName, 1);
848 
849  if (copyflag == L_INSERT)
850  fpixc = fpix;
851  else if (copyflag == L_COPY)
852  fpixc = fpixCopy(NULL, fpix);
853  else if (copyflag == L_CLONE)
854  fpixc = fpixClone(fpix);
855  else
856  return ERROR_INT("invalid copyflag", procName, 1);
857  if (!fpixc)
858  return ERROR_INT("fpixc not made", procName, 1);
859 
860  n = fpixaGetCount(fpixa);
861  if (n >= fpixa->nalloc)
862  fpixaExtendArray(fpixa);
863  fpixa->fpix[n] = fpixc;
864  fpixa->n++;
865 
866  return 0;
867 }
868 
869 
881 static l_int32
883 {
884  PROCNAME("fpixaExtendArray");
885 
886  if (!fpixa)
887  return ERROR_INT("fpixa not defined", procName, 1);
888 
889  return fpixaExtendArrayToSize(fpixa, 2 * fpixa->nalloc);
890 }
891 
892 
905 static l_int32
907  l_int32 size)
908 {
909  PROCNAME("fpixaExtendArrayToSize");
910 
911  if (!fpixa)
912  return ERROR_INT("fpixa not defined", procName, 1);
913 
914  if (size > fpixa->nalloc) {
915  if ((fpixa->fpix = (FPIX **)reallocNew((void **)&fpixa->fpix,
916  sizeof(FPIX *) * fpixa->nalloc,
917  size * sizeof(FPIX *))) == NULL)
918  return ERROR_INT("new ptr array not returned", procName, 1);
919  fpixa->nalloc = size;
920  }
921  return 0;
922 }
923 
924 
925 /*--------------------------------------------------------------------*
926  * FPixa accessors *
927  *--------------------------------------------------------------------*/
934 l_int32
936 {
937  PROCNAME("fpixaGetCount");
938 
939  if (!fpixa)
940  return ERROR_INT("fpixa not defined", procName, 0);
941 
942  return fpixa->n;
943 }
944 
945 
953 l_int32
955  l_int32 delta)
956 {
957  PROCNAME("fpixaChangeRefcount");
958 
959  if (!fpixa)
960  return ERROR_INT("fpixa not defined", procName, 1);
961 
962  fpixa->refcount += delta;
963  return 0;
964 }
965 
966 
975 FPIX *
977  l_int32 index,
978  l_int32 accesstype)
979 {
980  PROCNAME("fpixaGetFPix");
981 
982  if (!fpixa)
983  return (FPIX *)ERROR_PTR("fpixa not defined", procName, NULL);
984  if (index < 0 || index >= fpixa->n)
985  return (FPIX *)ERROR_PTR("index not valid", procName, NULL);
986 
987  if (accesstype == L_COPY)
988  return fpixCopy(NULL, fpixa->fpix[index]);
989  else if (accesstype == L_CLONE)
990  return fpixClone(fpixa->fpix[index]);
991  else
992  return (FPIX *)ERROR_PTR("invalid accesstype", procName, NULL);
993 }
994 
995 
1004 l_int32
1006  l_int32 index,
1007  l_int32 *pw,
1008  l_int32 *ph)
1009 {
1010 FPIX *fpix;
1011 
1012  PROCNAME("fpixaGetFPixDimensions");
1013 
1014  if (!pw && !ph)
1015  return ERROR_INT("no return val requested", procName, 1);
1016  if (pw) *pw = 0;
1017  if (ph) *ph = 0;
1018  if (!fpixa)
1019  return ERROR_INT("fpixa not defined", procName, 1);
1020  if (index < 0 || index >= fpixa->n)
1021  return ERROR_INT("index not valid", procName, 1);
1022 
1023  if ((fpix = fpixaGetFPix(fpixa, index, L_CLONE)) == NULL)
1024  return ERROR_INT("fpix not found!", procName, 1);
1025  fpixGetDimensions(fpix, pw, ph);
1026  fpixDestroy(&fpix);
1027  return 0;
1028 }
1029 
1030 
1038 l_float32 *
1040  l_int32 index)
1041 {
1042 l_int32 n;
1043 l_float32 *data;
1044 FPIX *fpix;
1045 
1046  PROCNAME("fpixaGetData");
1047 
1048  if (!fpixa)
1049  return (l_float32 *)ERROR_PTR("fpixa not defined", procName, NULL);
1050  n = fpixaGetCount(fpixa);
1051  if (index < 0 || index >= n)
1052  return (l_float32 *)ERROR_PTR("invalid index", procName, NULL);
1053 
1054  fpix = fpixaGetFPix(fpixa, index, L_CLONE);
1055  data = fpixGetData(fpix);
1056  fpixDestroy(&fpix);
1057  return data;
1058 }
1059 
1060 
1070 l_int32
1072  l_int32 index,
1073  l_int32 x,
1074  l_int32 y,
1075  l_float32 *pval)
1076 {
1077 l_int32 n, ret;
1078 FPIX *fpix;
1079 
1080  PROCNAME("fpixaGetPixel");
1081 
1082  if (!pval)
1083  return ERROR_INT("pval not defined", procName, 1);
1084  *pval = 0.0;
1085  if (!fpixa)
1086  return ERROR_INT("fpixa not defined", procName, 1);
1087  n = fpixaGetCount(fpixa);
1088  if (index < 0 || index >= n)
1089  return ERROR_INT("invalid index into fpixa", procName, 1);
1090 
1091  fpix = fpixaGetFPix(fpixa, index, L_CLONE);
1092  ret = fpixGetPixel(fpix, x, y, pval);
1093  fpixDestroy(&fpix);
1094  return ret;
1095 }
1096 
1097 
1107 l_int32
1109  l_int32 index,
1110  l_int32 x,
1111  l_int32 y,
1112  l_float32 val)
1113 {
1114 l_int32 n, ret;
1115 FPIX *fpix;
1116 
1117  PROCNAME("fpixaSetPixel");
1118 
1119  if (!fpixa)
1120  return ERROR_INT("fpixa not defined", procName, 1);
1121  n = fpixaGetCount(fpixa);
1122  if (index < 0 || index >= n)
1123  return ERROR_INT("invalid index into fpixa", procName, 1);
1124 
1125  fpix = fpixaGetFPix(fpixa, index, L_CLONE);
1126  ret = fpixSetPixel(fpix, x, y, val);
1127  fpixDestroy(&fpix);
1128  return ret;
1129 }
1130 
1131 
1132 /*--------------------------------------------------------------------*
1133  * DPix Create/copy/destroy *
1134  *--------------------------------------------------------------------*/
1148 DPIX *
1149 dpixCreate(l_int32 width,
1150  l_int32 height)
1151 {
1152 l_float64 *data;
1153 l_uint64 bignum;
1154 DPIX *dpix;
1155 
1156  PROCNAME("dpixCreate");
1157 
1158  if (width <= 0)
1159  return (DPIX *)ERROR_PTR("width must be > 0", procName, NULL);
1160  if (height <= 0)
1161  return (DPIX *)ERROR_PTR("height must be > 0", procName, NULL);
1162 
1163  /* Avoid overflow in malloc arg, malicious or otherwise */
1164  bignum = 8L * width * height; /* max number of bytes requested */
1165  if (bignum > ((1LL << 31) - 1)) {
1166  L_ERROR("requested w = %d, h = %d\n", procName, width, height);
1167  return (DPIX *)ERROR_PTR("requested bytes >= 2^31", procName, NULL);
1168  }
1169 
1170  if ((dpix = (DPIX *)LEPT_CALLOC(1, sizeof(DPIX))) == NULL)
1171  return (DPIX *)ERROR_PTR("LEPT_CALLOC fail for dpix", procName, NULL);
1172  dpixSetDimensions(dpix, width, height);
1173  dpixSetWpl(dpix, width); /* 8 byte words */
1174  dpix->refcount = 1;
1175 
1176  data = (l_float64 *)LEPT_CALLOC(width * height, sizeof(l_float64));
1177  if (!data) {
1178  dpixDestroy(&dpix);
1179  return (DPIX *)ERROR_PTR("LEPT_CALLOC fail for data", procName, NULL);
1180  }
1181  dpixSetData(dpix, data);
1182 
1183  return dpix;
1184 }
1185 
1186 
1200 DPIX *
1202 {
1203 l_int32 w, h;
1204 DPIX *dpixd;
1205 
1206  PROCNAME("dpixCreateTemplate");
1207 
1208  if (!dpixs)
1209  return (DPIX *)ERROR_PTR("dpixs not defined", procName, NULL);
1210 
1211  dpixGetDimensions(dpixs, &w, &h);
1212  dpixd = dpixCreate(w, h);
1213  dpixCopyResolution(dpixd, dpixs);
1214  return dpixd;
1215 }
1216 
1217 
1229 DPIX *
1231 {
1232  PROCNAME("dpixClone");
1233 
1234  if (!dpix)
1235  return (DPIX *)ERROR_PTR("dpix not defined", procName, NULL);
1236  dpixChangeRefcount(dpix, 1);
1237 
1238  return dpix;
1239 }
1240 
1241 
1272 DPIX *
1273 dpixCopy(DPIX *dpixd, /* can be null */
1274  DPIX *dpixs)
1275 {
1276 l_int32 w, h, bytes;
1277 l_float64 *datas, *datad;
1278 
1279  PROCNAME("dpixCopy");
1280 
1281  if (!dpixs)
1282  return (DPIX *)ERROR_PTR("dpixs not defined", procName, NULL);
1283  if (dpixs == dpixd)
1284  return dpixd;
1285 
1286  /* Total bytes in image data */
1287  dpixGetDimensions(dpixs, &w, &h);
1288  bytes = 8 * w * h;
1289 
1290  /* If we're making a new dpix ... */
1291  if (!dpixd) {
1292  if ((dpixd = dpixCreateTemplate(dpixs)) == NULL)
1293  return (DPIX *)ERROR_PTR("dpixd not made", procName, NULL);
1294  datas = dpixGetData(dpixs);
1295  datad = dpixGetData(dpixd);
1296  memcpy((char *)datad, (char *)datas, bytes);
1297  return dpixd;
1298  }
1299 
1300  /* Reallocate image data if sizes are different */
1301  dpixResizeImageData(dpixd, dpixs);
1302 
1303  /* Copy data */
1304  dpixCopyResolution(dpixd, dpixs);
1305  datas = dpixGetData(dpixs);
1306  datad = dpixGetData(dpixd);
1307  memcpy((char*)datad, (char*)datas, bytes);
1308  return dpixd;
1309 }
1310 
1311 
1318 l_int32
1320  DPIX *dpixs)
1321 {
1322 l_int32 ws, hs, wd, hd, bytes;
1323 l_float64 *data;
1324 
1325  PROCNAME("dpixResizeImageData");
1326 
1327  if (!dpixs)
1328  return ERROR_INT("dpixs not defined", procName, 1);
1329  if (!dpixd)
1330  return ERROR_INT("dpixd not defined", procName, 1);
1331 
1332  dpixGetDimensions(dpixs, &ws, &hs);
1333  dpixGetDimensions(dpixd, &wd, &hd);
1334  if (ws == wd && hs == hd) /* nothing to do */
1335  return 0;
1336 
1337  dpixSetDimensions(dpixd, ws, hs);
1338  dpixSetWpl(dpixd, ws); /* 8 byte words */
1339  bytes = 8 * ws * hs;
1340  data = dpixGetData(dpixd);
1341  if (data) LEPT_FREE(data);
1342  if ((data = (l_float64 *)LEPT_MALLOC(bytes)) == NULL)
1343  return ERROR_INT("LEPT_MALLOC fail for data", procName, 1);
1344  dpixSetData(dpixd, data);
1345  return 0;
1346 }
1347 
1348 
1361 void
1363 {
1364 l_float64 *data;
1365 DPIX *dpix;
1366 
1367  PROCNAME("dpixDestroy");
1368 
1369  if (!pdpix) {
1370  L_WARNING("ptr address is null!\n", procName);
1371  return;
1372  }
1373 
1374  if ((dpix = *pdpix) == NULL)
1375  return;
1376 
1377  /* Decrement the ref count. If it is 0, destroy the dpix. */
1378  dpixChangeRefcount(dpix, -1);
1379  if (dpixGetRefcount(dpix) <= 0) {
1380  if ((data = dpixGetData(dpix)) != NULL)
1381  LEPT_FREE(data);
1382  LEPT_FREE(dpix);
1383  }
1384 
1385  *pdpix = NULL;
1386  return;
1387 }
1388 
1389 
1390 /*--------------------------------------------------------------------*
1391  * DPix Accessors *
1392  *--------------------------------------------------------------------*/
1400 l_int32
1402  l_int32 *pw,
1403  l_int32 *ph)
1404 {
1405  PROCNAME("dpixGetDimensions");
1406 
1407  if (!pw && !ph)
1408  return ERROR_INT("no return val requested", procName, 1);
1409  if (pw) *pw = 0;
1410  if (ph) *ph = 0;
1411  if (!dpix)
1412  return ERROR_INT("dpix not defined", procName, 1);
1413  if (pw) *pw = dpix->w;
1414  if (ph) *ph = dpix->h;
1415  return 0;
1416 }
1417 
1418 
1426 l_int32
1428  l_int32 w,
1429  l_int32 h)
1430 {
1431  PROCNAME("dpixSetDimensions");
1432 
1433  if (!dpix)
1434  return ERROR_INT("dpix not defined", procName, 1);
1435  dpix->w = w;
1436  dpix->h = h;
1437  return 0;
1438 }
1439 
1440 
1447 l_int32
1449 {
1450  PROCNAME("dpixGetWpl");
1451 
1452  if (!dpix)
1453  return ERROR_INT("dpix not defined", procName, 1);
1454  return dpix->wpl;
1455 }
1456 
1457 
1465 l_int32
1467  l_int32 wpl)
1468 {
1469  PROCNAME("dpixSetWpl");
1470 
1471  if (!dpix)
1472  return ERROR_INT("dpix not defined", procName, 1);
1473 
1474  dpix->wpl = wpl;
1475  return 0;
1476 }
1477 
1478 
1485 l_int32
1487 {
1488  PROCNAME("dpixGetRefcount");
1489 
1490  if (!dpix)
1491  return ERROR_INT("dpix not defined", procName, UNDEF);
1492  return dpix->refcount;
1493 }
1494 
1495 
1503 l_int32
1505  l_int32 delta)
1506 {
1507  PROCNAME("dpixChangeRefcount");
1508 
1509  if (!dpix)
1510  return ERROR_INT("dpix not defined", procName, 1);
1511 
1512  dpix->refcount += delta;
1513  return 0;
1514 }
1515 
1516 
1524 l_int32
1526  l_int32 *pxres,
1527  l_int32 *pyres)
1528 {
1529  PROCNAME("dpixGetResolution");
1530 
1531  if (!dpix)
1532  return ERROR_INT("dpix not defined", procName, 1);
1533  if (pxres) *pxres = dpix->xres;
1534  if (pyres) *pyres = dpix->yres;
1535  return 0;
1536 }
1537 
1538 
1546 l_int32
1548  l_int32 xres,
1549  l_int32 yres)
1550 {
1551  PROCNAME("dpixSetResolution");
1552 
1553  if (!dpix)
1554  return ERROR_INT("dpix not defined", procName, 1);
1555 
1556  dpix->xres = xres;
1557  dpix->yres = yres;
1558  return 0;
1559 }
1560 
1561 
1568 l_int32
1570  DPIX *dpixs)
1571 {
1572 l_int32 xres, yres;
1573  PROCNAME("dpixCopyResolution");
1574 
1575  if (!dpixs || !dpixd)
1576  return ERROR_INT("dpixs and dpixd not both defined", procName, 1);
1577 
1578  dpixGetResolution(dpixs, &xres, &yres);
1579  dpixSetResolution(dpixd, xres, yres);
1580  return 0;
1581 }
1582 
1583 
1590 l_float64 *
1592 {
1593  PROCNAME("dpixGetData");
1594 
1595  if (!dpix)
1596  return (l_float64 *)ERROR_PTR("dpix not defined", procName, NULL);
1597  return dpix->data;
1598 }
1599 
1600 
1608 l_int32
1610  l_float64 *data)
1611 {
1612  PROCNAME("dpixSetData");
1613 
1614  if (!dpix)
1615  return ERROR_INT("dpix not defined", procName, 1);
1616 
1617  dpix->data = data;
1618  return 0;
1619 }
1620 
1621 
1630 l_int32
1632  l_int32 x,
1633  l_int32 y,
1634  l_float64 *pval)
1635 {
1636 l_int32 w, h;
1637 
1638  PROCNAME("dpixGetPixel");
1639 
1640  if (!pval)
1641  return ERROR_INT("pval not defined", procName, 1);
1642  *pval = 0.0;
1643  if (!dpix)
1644  return ERROR_INT("dpix not defined", procName, 1);
1645 
1646  dpixGetDimensions(dpix, &w, &h);
1647  if (x < 0 || x >= w)
1648  return ERROR_INT("x out of bounds", procName, 1);
1649  if (y < 0 || y >= h)
1650  return ERROR_INT("y out of bounds", procName, 1);
1651 
1652  *pval = *(dpix->data + y * w + x);
1653  return 0;
1654 }
1655 
1656 
1665 l_int32
1667  l_int32 x,
1668  l_int32 y,
1669  l_float64 val)
1670 {
1671 l_int32 w, h;
1672 
1673  PROCNAME("dpixSetPixel");
1674 
1675  if (!dpix)
1676  return ERROR_INT("dpix not defined", procName, 1);
1677 
1678  dpixGetDimensions(dpix, &w, &h);
1679  if (x < 0 || x >= w)
1680  return ERROR_INT("x out of bounds", procName, 1);
1681  if (y < 0 || y >= h)
1682  return ERROR_INT("y out of bounds", procName, 1);
1683 
1684  *(dpix->data + y * w + x) = val;
1685  return 0;
1686 }
1687 
1688 
1689 /*--------------------------------------------------------------------*
1690  * FPix serialized I/O *
1691  *--------------------------------------------------------------------*/
1698 FPIX *
1699 fpixRead(const char *filename)
1700 {
1701 FILE *fp;
1702 FPIX *fpix;
1703 
1704  PROCNAME("fpixRead");
1705 
1706  if (!filename)
1707  return (FPIX *)ERROR_PTR("filename not defined", procName, NULL);
1708 
1709  if ((fp = fopenReadStream(filename)) == NULL)
1710  return (FPIX *)ERROR_PTR("stream not opened", procName, NULL);
1711  fpix = fpixReadStream(fp);
1712  fclose(fp);
1713  if (!fpix)
1714  return (FPIX *)ERROR_PTR("fpix not read", procName, NULL);
1715  return fpix;
1716 }
1717 
1718 
1725 FPIX *
1727 {
1728 char buf[256];
1729 l_int32 w, h, nbytes, xres, yres, version;
1730 l_float32 *data;
1731 FPIX *fpix;
1732 
1733  PROCNAME("fpixReadStream");
1734 
1735  if (!fp)
1736  return (FPIX *)ERROR_PTR("stream not defined", procName, NULL);
1737 
1738  if (fscanf(fp, "\nFPix Version %d\n", &version) != 1)
1739  return (FPIX *)ERROR_PTR("not a fpix file", procName, NULL);
1740  if (version != FPIX_VERSION_NUMBER)
1741  return (FPIX *)ERROR_PTR("invalid fpix version", procName, NULL);
1742  if (fscanf(fp, "w = %d, h = %d, nbytes = %d\n", &w, &h, &nbytes) != 3)
1743  return (FPIX *)ERROR_PTR("read fail for data size", procName, NULL);
1744 
1745  /* Use fgets() and sscanf(); not fscanf(), for the last
1746  * bit of header data before the float data. The reason is
1747  * that fscanf throws away white space, and if the float data
1748  * happens to begin with ascii character(s) that are white
1749  * space, it will swallow them and all will be lost! */
1750  if (fgets(buf, sizeof(buf), fp) == NULL)
1751  return (FPIX *)ERROR_PTR("fgets read fail", procName, NULL);
1752  if (sscanf(buf, "xres = %d, yres = %d\n", &xres, &yres) != 2)
1753  return (FPIX *)ERROR_PTR("read fail for xres, yres", procName, NULL);
1754 
1755  if ((fpix = fpixCreate(w, h)) == NULL)
1756  return (FPIX *)ERROR_PTR("fpix not made", procName, NULL);
1757  fpixSetResolution(fpix, xres, yres);
1758  data = fpixGetData(fpix);
1759  if (fread(data, 1, nbytes, fp) != nbytes) {
1760  fpixDestroy(&fpix);
1761  return (FPIX *)ERROR_PTR("read error for nbytes", procName, NULL);
1762  }
1763  fgetc(fp); /* ending nl */
1764 
1765  /* Convert to little-endian if necessary */
1766  fpixEndianByteSwap(fpix, fpix);
1767  return fpix;
1768 }
1769 
1770 
1778 FPIX *
1779 fpixReadMem(const l_uint8 *data,
1780  size_t size)
1781 {
1782 FILE *fp;
1783 FPIX *fpix;
1784 
1785  PROCNAME("fpixReadMem");
1786 
1787  if (!data)
1788  return (FPIX *)ERROR_PTR("data not defined", procName, NULL);
1789  if ((fp = fopenReadFromMemory(data, size)) == NULL)
1790  return (FPIX *)ERROR_PTR("stream not opened", procName, NULL);
1791 
1792  fpix = fpixReadStream(fp);
1793  fclose(fp);
1794  if (!fpix) L_ERROR("fpix not read\n", procName);
1795  return fpix;
1796 }
1797 
1798 
1806 l_int32
1807 fpixWrite(const char *filename,
1808  FPIX *fpix)
1809 {
1810 l_int32 ret;
1811 FILE *fp;
1812 
1813  PROCNAME("fpixWrite");
1814 
1815  if (!filename)
1816  return ERROR_INT("filename not defined", procName, 1);
1817  if (!fpix)
1818  return ERROR_INT("fpix not defined", procName, 1);
1819 
1820  if ((fp = fopenWriteStream(filename, "wb")) == NULL)
1821  return ERROR_INT("stream not opened", procName, 1);
1822  ret = fpixWriteStream(fp, fpix);
1823  fclose(fp);
1824  if (ret)
1825  return ERROR_INT("fpix not written to stream", procName, 1);
1826  return 0;
1827 }
1828 
1829 
1837 l_int32
1839  FPIX *fpix)
1840 {
1841 l_int32 w, h, nbytes, xres, yres;
1842 l_float32 *data;
1843 FPIX *fpixt;
1844 
1845  PROCNAME("fpixWriteStream");
1846 
1847  if (!fp)
1848  return ERROR_INT("stream not defined", procName, 1);
1849  if (!fpix)
1850  return ERROR_INT("fpix not defined", procName, 1);
1851 
1852  /* Convert to little-endian if necessary */
1853  fpixt = fpixEndianByteSwap(NULL, fpix);
1854 
1855  fpixGetDimensions(fpixt, &w, &h);
1856  data = fpixGetData(fpixt);
1857  nbytes = w * h * sizeof(l_float32);
1858  fpixGetResolution(fpixt, &xres, &yres);
1859  fprintf(fp, "\nFPix Version %d\n", FPIX_VERSION_NUMBER);
1860  fprintf(fp, "w = %d, h = %d, nbytes = %d\n", w, h, nbytes);
1861  fprintf(fp, "xres = %d, yres = %d\n", xres, yres);
1862  fwrite(data, 1, nbytes, fp);
1863  fprintf(fp, "\n");
1864 
1865  fpixDestroy(&fpixt);
1866  return 0;
1867 }
1868 
1869 
1883 l_int32
1884 fpixWriteMem(l_uint8 **pdata,
1885  size_t *psize,
1886  FPIX *fpix)
1887 {
1888 l_int32 ret;
1889 FILE *fp;
1890 
1891  PROCNAME("fpixWriteMem");
1892 
1893  if (pdata) *pdata = NULL;
1894  if (psize) *psize = 0;
1895  if (!pdata)
1896  return ERROR_INT("&data not defined", procName, 1);
1897  if (!psize)
1898  return ERROR_INT("&size not defined", procName, 1);
1899  if (!fpix)
1900  return ERROR_INT("fpix not defined", procName, 1);
1901 
1902 #if HAVE_FMEMOPEN
1903  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
1904  return ERROR_INT("stream not opened", procName, 1);
1905  ret = fpixWriteStream(fp, fpix);
1906 #else
1907  L_INFO("work-around: writing to a temp file\n", procName);
1908  #ifdef _WIN32
1909  if ((fp = fopenWriteWinTempfile()) == NULL)
1910  return ERROR_INT("tmpfile stream not opened", procName, 1);
1911  #else
1912  if ((fp = tmpfile()) == NULL)
1913  return ERROR_INT("tmpfile stream not opened", procName, 1);
1914  #endif /* _WIN32 */
1915  ret = fpixWriteStream(fp, fpix);
1916  rewind(fp);
1917  *pdata = l_binaryReadStream(fp, psize);
1918 #endif /* HAVE_FMEMOPEN */
1919  fclose(fp);
1920  return ret;
1921 }
1922 
1923 
1943 FPIX *
1945  FPIX *fpixs)
1946 {
1947  PROCNAME("fpixEndianByteSwap");
1948 
1949  if (!fpixs)
1950  return (FPIX *)ERROR_PTR("fpixs not defined", procName, fpixd);
1951  if (fpixd && (fpixs != fpixd))
1952  return (FPIX *)ERROR_PTR("fpixd != fpixs", procName, fpixd);
1953 
1954 #ifdef L_BIG_ENDIAN
1955  {
1956  l_uint32 *data;
1957  l_int32 i, j, w, h;
1958  l_uint32 word;
1959 
1960  fpixGetDimensions(fpixs, &w, &h);
1961  fpixd = fpixCopy(fpixd, fpixs); /* no copy if fpixd == fpixs */
1962 
1963  data = (l_uint32 *)fpixGetData(fpixd);
1964  for (i = 0; i < h; i++) {
1965  for (j = 0; j < w; j++, data++) {
1966  word = *data;
1967  *data = (word >> 24) |
1968  ((word >> 8) & 0x0000ff00) |
1969  ((word << 8) & 0x00ff0000) |
1970  (word << 24);
1971  }
1972  }
1973  return fpixd;
1974  }
1975 #else /* L_LITTLE_ENDIAN */
1976 
1977  if (fpixd)
1978  return fpixd; /* no-op */
1979  else
1980  return fpixClone(fpixs);
1981 
1982 #endif /* L_BIG_ENDIAN */
1983 }
1984 
1985 
1986 /*--------------------------------------------------------------------*
1987  * DPix serialized I/O *
1988  *--------------------------------------------------------------------*/
1995 DPIX *
1996 dpixRead(const char *filename)
1997 {
1998 FILE *fp;
1999 DPIX *dpix;
2000 
2001  PROCNAME("dpixRead");
2002 
2003  if (!filename)
2004  return (DPIX *)ERROR_PTR("filename not defined", procName, NULL);
2005 
2006  if ((fp = fopenReadStream(filename)) == NULL)
2007  return (DPIX *)ERROR_PTR("stream not opened", procName, NULL);
2008  dpix = dpixReadStream(fp);
2009  fclose(fp);
2010  if (!dpix)
2011  return (DPIX *)ERROR_PTR("dpix not read", procName, NULL);
2012  return dpix;
2013 }
2014 
2015 
2022 DPIX *
2024 {
2025 char buf[256];
2026 l_int32 w, h, nbytes, version, xres, yres;
2027 l_float64 *data;
2028 DPIX *dpix;
2029 
2030  PROCNAME("dpixReadStream");
2031 
2032  if (!fp)
2033  return (DPIX *)ERROR_PTR("stream not defined", procName, NULL);
2034 
2035  if (fscanf(fp, "\nDPix Version %d\n", &version) != 1)
2036  return (DPIX *)ERROR_PTR("not a dpix file", procName, NULL);
2037  if (version != DPIX_VERSION_NUMBER)
2038  return (DPIX *)ERROR_PTR("invalid dpix version", procName, NULL);
2039  if (fscanf(fp, "w = %d, h = %d, nbytes = %d\n", &w, &h, &nbytes) != 3)
2040  return (DPIX *)ERROR_PTR("read fail for data size", procName, NULL);
2041 
2042  /* Use fgets() and sscanf(); not fscanf(), for the last
2043  * bit of header data before the float data. The reason is
2044  * that fscanf throws away white space, and if the float data
2045  * happens to begin with ascii character(s) that are white
2046  * space, it will swallow them and all will be lost! */
2047  if (fgets(buf, sizeof(buf), fp) == NULL)
2048  return (DPIX *)ERROR_PTR("fgets read fail", procName, NULL);
2049  if (sscanf(buf, "xres = %d, yres = %d\n", &xres, &yres) != 2)
2050  return (DPIX *)ERROR_PTR("read fail for xres, yres", procName, NULL);
2051 
2052  if ((dpix = dpixCreate(w, h)) == NULL)
2053  return (DPIX *)ERROR_PTR("dpix not made", procName, NULL);
2054  dpixSetResolution(dpix, xres, yres);
2055  data = dpixGetData(dpix);
2056  if (fread(data, 1, nbytes, fp) != nbytes) {
2057  dpixDestroy(&dpix);
2058  return (DPIX *)ERROR_PTR("read error for nbytes", procName, NULL);
2059  }
2060  fgetc(fp); /* ending nl */
2061 
2062  /* Convert to little-endian if necessary */
2063  dpixEndianByteSwap(dpix, dpix);
2064  return dpix;
2065 }
2066 
2067 
2075 DPIX *
2076 dpixReadMem(const l_uint8 *data,
2077  size_t size)
2078 {
2079 FILE *fp;
2080 DPIX *dpix;
2081 
2082  PROCNAME("dpixReadMem");
2083 
2084  if (!data)
2085  return (DPIX *)ERROR_PTR("data not defined", procName, NULL);
2086  if ((fp = fopenReadFromMemory(data, size)) == NULL)
2087  return (DPIX *)ERROR_PTR("stream not opened", procName, NULL);
2088 
2089  dpix = dpixReadStream(fp);
2090  fclose(fp);
2091  if (!dpix) L_ERROR("dpix not read\n", procName);
2092  return dpix;
2093 }
2094 
2095 
2103 l_int32
2104 dpixWrite(const char *filename,
2105  DPIX *dpix)
2106 {
2107 l_int32 ret;
2108 FILE *fp;
2109 
2110  PROCNAME("dpixWrite");
2111 
2112  if (!filename)
2113  return ERROR_INT("filename not defined", procName, 1);
2114  if (!dpix)
2115  return ERROR_INT("dpix not defined", procName, 1);
2116 
2117  if ((fp = fopenWriteStream(filename, "wb")) == NULL)
2118  return ERROR_INT("stream not opened", procName, 1);
2119  ret = dpixWriteStream(fp, dpix);
2120  fclose(fp);
2121  if (ret)
2122  return ERROR_INT("dpix not written to stream", procName, 1);
2123  return 0;
2124 }
2125 
2126 
2134 l_int32
2136  DPIX *dpix)
2137 {
2138 l_int32 w, h, nbytes, xres, yres;
2139 l_float64 *data;
2140 DPIX *dpixt;
2141 
2142  PROCNAME("dpixWriteStream");
2143 
2144  if (!fp)
2145  return ERROR_INT("stream not defined", procName, 1);
2146  if (!dpix)
2147  return ERROR_INT("dpix not defined", procName, 1);
2148 
2149  /* Convert to little-endian if necessary */
2150  dpixt = dpixEndianByteSwap(NULL, dpix);
2151 
2152  dpixGetDimensions(dpixt, &w, &h);
2153  dpixGetResolution(dpixt, &xres, &yres);
2154  data = dpixGetData(dpixt);
2155  nbytes = w * h * sizeof(l_float64);
2156  fprintf(fp, "\nDPix Version %d\n", DPIX_VERSION_NUMBER);
2157  fprintf(fp, "w = %d, h = %d, nbytes = %d\n", w, h, nbytes);
2158  fprintf(fp, "xres = %d, yres = %d\n", xres, yres);
2159  fwrite(data, 1, nbytes, fp);
2160  fprintf(fp, "\n");
2161 
2162  dpixDestroy(&dpixt);
2163  return 0;
2164 }
2165 
2166 
2180 l_int32
2181 dpixWriteMem(l_uint8 **pdata,
2182  size_t *psize,
2183  DPIX *dpix)
2184 {
2185 l_int32 ret;
2186 FILE *fp;
2187 
2188  PROCNAME("dpixWriteMem");
2189 
2190  if (pdata) *pdata = NULL;
2191  if (psize) *psize = 0;
2192  if (!pdata)
2193  return ERROR_INT("&data not defined", procName, 1);
2194  if (!psize)
2195  return ERROR_INT("&size not defined", procName, 1);
2196  if (!dpix)
2197  return ERROR_INT("dpix not defined", procName, 1);
2198 
2199 #if HAVE_FMEMOPEN
2200  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
2201  return ERROR_INT("stream not opened", procName, 1);
2202  ret = dpixWriteStream(fp, dpix);
2203 #else
2204  L_INFO("work-around: writing to a temp file\n", procName);
2205  #ifdef _WIN32
2206  if ((fp = fopenWriteWinTempfile()) == NULL)
2207  return ERROR_INT("tmpfile stream not opened", procName, 1);
2208  #else
2209  if ((fp = tmpfile()) == NULL)
2210  return ERROR_INT("tmpfile stream not opened", procName, 1);
2211  #endif /* _WIN32 */
2212  ret = dpixWriteStream(fp, dpix);
2213  rewind(fp);
2214  *pdata = l_binaryReadStream(fp, psize);
2215 #endif /* HAVE_FMEMOPEN */
2216  fclose(fp);
2217  return ret;
2218 }
2219 
2220 
2240 DPIX *
2242  DPIX *dpixs)
2243 {
2244  PROCNAME("dpixEndianByteSwap");
2245 
2246  if (!dpixs)
2247  return (DPIX *)ERROR_PTR("dpixs not defined", procName, dpixd);
2248  if (dpixd && (dpixs != dpixd))
2249  return (DPIX *)ERROR_PTR("dpixd != dpixs", procName, dpixd);
2250 
2251 #ifdef L_BIG_ENDIAN
2252  {
2253  l_uint32 *data;
2254  l_int32 i, j, w, h;
2255  l_uint32 word;
2256 
2257  dpixGetDimensions(dpixs, &w, &h);
2258  dpixd = dpixCopy(dpixd, dpixs); /* no copy if dpixd == dpixs */
2259 
2260  data = (l_uint32 *)dpixGetData(dpixd);
2261  for (i = 0; i < h; i++) {
2262  for (j = 0; j < 2 * w; j++, data++) {
2263  word = *data;
2264  *data = (word >> 24) |
2265  ((word >> 8) & 0x0000ff00) |
2266  ((word << 8) & 0x00ff0000) |
2267  (word << 24);
2268  }
2269  }
2270  return dpixd;
2271  }
2272 #else /* L_LITTLE_ENDIAN */
2273 
2274  if (dpixd)
2275  return dpixd; /* no-op */
2276  else
2277  return dpixClone(dpixs);
2278 
2279 #endif /* L_BIG_ENDIAN */
2280 }
2281 
2282 
2283 /*--------------------------------------------------------------------*
2284  * Print FPix (subsampled, for debugging) *
2285  *--------------------------------------------------------------------*/
2299 l_int32
2301  FPIX *fpix,
2302  l_int32 factor)
2303 {
2304 l_int32 i, j, w, h, count;
2305 l_float32 val;
2306 
2307  PROCNAME("fpixPrintStream");
2308 
2309  if (!fp)
2310  return ERROR_INT("stream not defined", procName, 1);
2311  if (!fpix)
2312  return ERROR_INT("fpix not defined", procName, 1);
2313  if (factor < 1)
2314  return ERROR_INT("sampling factor < 1f", procName, 1);
2315 
2316  fpixGetDimensions(fpix, &w, &h);
2317  fprintf(fp, "\nFPix: w = %d, h = %d\n", w, h);
2318  for (i = 0; i < h; i += factor) {
2319  for (count = 0, j = 0; j < w; j += factor, count++) {
2320  fpixGetPixel(fpix, j, i, &val);
2321  fprintf(fp, "val[%d, %d] = %f ", i, j, val);
2322  if ((count + 1) % 3 == 0) fprintf(fp, "\n");
2323  }
2324  if (count % 3) fprintf(fp, "\n");
2325  }
2326  fprintf(fp, "\n");
2327  return 0;
2328 }
l_int32 fpixaChangeRefcount(FPIXA *fpixa, l_int32 delta)
fpixaChangeRefcount()
Definition: fpix1.c:954
DPIX * dpixClone(DPIX *dpix)
dpixClone()
Definition: fpix1.c:1230
FPIXA * fpixaCopy(FPIXA *fpixa, l_int32 copyflag)
fpixaCopy()
Definition: fpix1.c:747
void dpixDestroy(DPIX **pdpix)
dpixDestroy()
Definition: fpix1.c:1362
l_uint32 refcount
Definition: pix.h:618
struct FPix ** fpix
Definition: pix.h:602
DPIX * dpixCreate(l_int32 width, l_int32 height)
dpixCreate()
Definition: fpix1.c:1149
l_int32 dpixCopyResolution(DPIX *dpixd, DPIX *dpixs)
dpixCopyResolution()
Definition: fpix1.c:1569
l_int32 yres
Definition: pix.h:621
l_int32 dpixWrite(const char *filename, DPIX *dpix)
dpixWrite()
Definition: fpix1.c:2104
l_uint32 refcount
Definition: pix.h:587
l_int32 fpixaGetCount(FPIXA *fpixa)
fpixaGetCount()
Definition: fpix1.c:935
l_float64 * dpixGetData(DPIX *dpix)
dpixGetData()
Definition: fpix1.c:1591
l_int32 fpixResizeImageData(FPIX *fpixd, FPIX *fpixs)
fpixResizeImageData()
Definition: fpix1.c:327
l_int32 dpixGetWpl(DPIX *dpix)
dpixGetWpl()
Definition: fpix1.c:1448
l_int32 fpixWrite(const char *filename, FPIX *fpix)
fpixWrite()
Definition: fpix1.c:1807
l_int32 fpixSetDimensions(FPIX *fpix, l_int32 w, l_int32 h)
fpixSetDimensions()
Definition: fpix1.c:435
l_int32 dpixSetResolution(DPIX *dpix, l_int32 xres, l_int32 yres)
dpixSetResolution()
Definition: fpix1.c:1547
l_int32 dpixChangeRefcount(DPIX *dpix, l_int32 delta)
dpixChangeRefcount()
Definition: fpix1.c:1504
DPIX * dpixRead(const char *filename)
dpixRead()
Definition: fpix1.c:1996
l_uint32 refcount
Definition: pix.h:601
l_int32 xres
Definition: pix.h:619
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
Definition: utils2.c:1670
FPIX * fpixEndianByteSwap(FPIX *fpixd, FPIX *fpixs)
fpixEndianByteSwap()
Definition: fpix1.c:1944
Definition: pix.h:597
FPIXA * fpixaCreate(l_int32 n)
fpixaCreate()
Definition: fpix1.c:707
void fpixaDestroy(FPIXA **pfpixa)
fpixaDestroy()
Definition: fpix1.c:794
DPIX * dpixCopy(DPIX *dpixd, DPIX *dpixs)
dpixCopy()
Definition: fpix1.c:1273
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
Definition: utils2.c:1102
l_int32 nalloc
Definition: pix.h:600
DPIX * dpixCreateTemplate(DPIX *dpixs)
dpixCreateTemplate()
Definition: fpix1.c:1201
l_int32 fpixaGetFPixDimensions(FPIXA *fpixa, l_int32 index, l_int32 *pw, l_int32 *ph)
fpixaGetFPixDimensions()
Definition: fpix1.c:1005
l_int32 fpixSetWpl(FPIX *fpix, l_int32 wpl)
fpixSetWpl()
Definition: fpix1.c:474
l_int32 fpixGetPixel(FPIX *fpix, l_int32 x, l_int32 y, l_float32 *pval)
fpixGetPixel()
Definition: fpix1.c:639
l_int32 fpixSetResolution(FPIX *fpix, l_int32 xres, l_int32 yres)
fpixSetResolution()
Definition: fpix1.c:555
l_int32 dpixGetDimensions(DPIX *dpix, l_int32 *pw, l_int32 *ph)
dpixGetDimensions()
Definition: fpix1.c:1401
l_int32 wpl
Definition: pix.h:586
static const l_int32 L_INSERT
Definition: pix.h:710
FPIX * fpixReadMem(const l_uint8 *data, size_t size)
fpixReadMem()
Definition: fpix1.c:1779
DPIX * dpixEndianByteSwap(DPIX *dpixd, DPIX *dpixs)
dpixEndianByteSwap()
Definition: fpix1.c:2241
l_int32 fpixSetData(FPIX *fpix, l_float32 *data)
fpixSetData()
Definition: fpix1.c:617
l_int32 fpixaAddFPix(FPIXA *fpixa, FPIX *fpix, l_int32 copyflag)
fpixaAddFPix()
Definition: fpix1.c:835
l_int32 yres
Definition: pix.h:590
l_int32 dpixSetPixel(DPIX *dpix, l_int32 x, l_int32 y, l_float64 val)
dpixSetPixel()
Definition: fpix1.c:1666
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
Definition: utils2.c:1716
l_int32 fpixGetWpl(FPIX *fpix)
fpixGetWpl()
Definition: fpix1.c:456
FPIX * fpixClone(FPIX *fpix)
fpixClone()
Definition: fpix1.c:230
l_float32 * fpixaGetData(FPIXA *fpixa, l_int32 index)
fpixaGetData()
Definition: fpix1.c:1039
l_int32 fpixWriteStream(FILE *fp, FPIX *fpix)
fpixWriteStream()
Definition: fpix1.c:1838
DPIX * dpixReadMem(const l_uint8 *data, size_t size)
dpixReadMem()
Definition: fpix1.c:2076
l_int32 w
Definition: pix.h:615
l_int32 fpixWriteMem(l_uint8 **pdata, size_t *psize, FPIX *fpix)
fpixWriteMem()
Definition: fpix1.c:1884
l_int32 xres
Definition: pix.h:588
l_float32 * fpixGetData(FPIX *fpix)
fpixGetData()
Definition: fpix1.c:599
l_int32 fpixaSetPixel(FPIXA *fpixa, l_int32 index, l_int32 x, l_int32 y, l_float32 val)
fpixaSetPixel()
Definition: fpix1.c:1108
l_float64 * data
Definition: pix.h:623
l_int32 dpixGetResolution(DPIX *dpix, l_int32 *pxres, l_int32 *pyres)
dpixGetResolution()
Definition: fpix1.c:1525
l_int32 h
Definition: pix.h:616
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1636
l_int32 fpixCopyResolution(FPIX *fpixd, FPIX *fpixs)
fpixCopyResolution()
Definition: fpix1.c:577
l_int32 dpixSetWpl(DPIX *dpix, l_int32 wpl)
dpixSetWpl()
Definition: fpix1.c:1466
l_int32 fpixGetDimensions(FPIX *fpix, l_int32 *pw, l_int32 *ph)
fpixGetDimensions()
Definition: fpix1.c:409
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
l_int32 fpixGetResolution(FPIX *fpix, l_int32 *pxres, l_int32 *pyres)
fpixGetResolution()
Definition: fpix1.c:533
FPIX * fpixCreateTemplate(FPIX *fpixs)
fpixCreateTemplate()
Definition: fpix1.c:201
FPIX * fpixCreate(l_int32 width, l_int32 height)
fpixCreate()
Definition: fpix1.c:149
l_int32 fpixaGetPixel(FPIXA *fpixa, l_int32 index, l_int32 x, l_int32 y, l_float32 *pval)
fpixaGetPixel()
Definition: fpix1.c:1071
l_int32 fpixChangeRefcount(FPIX *fpix, l_int32 delta)
fpixChangeRefcount()
Definition: fpix1.c:512
static l_int32 fpixaExtendArrayToSize(FPIXA *fpixa, l_int32 size)
fpixaExtendArrayToSize()
Definition: fpix1.c:906
l_int32 dpixSetDimensions(DPIX *dpix, l_int32 w, l_int32 h)
dpixSetDimensions()
Definition: fpix1.c:1427
l_int32 dpixWriteMem(l_uint8 **pdata, size_t *psize, DPIX *dpix)
dpixWriteMem()
Definition: fpix1.c:2181
l_int32 w
Definition: pix.h:584
#define DPIX_VERSION_NUMBER
Definition: pix.h:610
l_int32 dpixSetData(DPIX *dpix, l_float64 *data)
dpixSetData()
Definition: fpix1.c:1609
l_int32 h
Definition: pix.h:585
Definition: pix.h:705
l_float32 * data
Definition: pix.h:592
l_int32 dpixResizeImageData(DPIX *dpixd, DPIX *dpixs)
dpixResizeImageData()
Definition: fpix1.c:1319
Definition: pix.h:706
FPIX * fpixCopy(FPIX *fpixd, FPIX *fpixs)
fpixCopy()
Definition: fpix1.c:273
FPIX * fpixRead(const char *filename)
fpixRead()
Definition: fpix1.c:1699
l_int32 fpixSetPixel(FPIX *fpix, l_int32 x, l_int32 y, l_float32 val)
fpixSetPixel()
Definition: fpix1.c:674
l_int32 wpl
Definition: pix.h:617
FPIX * fpixReadStream(FILE *fp)
fpixReadStream()
Definition: fpix1.c:1726
l_int32 fpixPrintStream(FILE *fp, FPIX *fpix, l_int32 factor)
fpixPrintStream()
Definition: fpix1.c:2300
l_int32 dpixGetPixel(DPIX *dpix, l_int32 x, l_int32 y, l_float64 *pval)
dpixGetPixel()
Definition: fpix1.c:1631
static l_int32 fpixaExtendArray(FPIXA *fpixa)
fpixaExtendArray()
Definition: fpix1.c:882
void fpixDestroy(FPIX **pfpix)
fpixDestroy()
Definition: fpix1.c:370
l_int32 fpixGetRefcount(FPIX *fpix)
fpixGetRefcount()
Definition: fpix1.c:494
Definition: pix.h:613
l_int32 n
Definition: pix.h:599
l_int32 dpixGetRefcount(DPIX *dpix)
dpixGetRefcount()
Definition: fpix1.c:1486
FPIX * fpixaGetFPix(FPIXA *fpixa, l_int32 index, l_int32 accesstype)
fpixaGetFPix()
Definition: fpix1.c:976
DPIX * dpixReadStream(FILE *fp)
dpixReadStream()
Definition: fpix1.c:2023
#define FPIX_VERSION_NUMBER
Definition: pix.h:579
Definition: pix.h:582
l_int32 dpixWriteStream(FILE *fp, DPIX *dpix)
dpixWriteStream()
Definition: fpix1.c:2135