Leptonica  1.77.0
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 
126 #include <string.h>
127 #include "allheaders.h"
128 
129 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20; /* must be > 0 */
130 
131  /* Static functions */
132 static l_int32 fpixaExtendArray(FPIXA *fpixa);
133 static l_int32 fpixaExtendArrayToSize(FPIXA *fpixa, l_int32 size);
134 
135 
136 /*--------------------------------------------------------------------*
137  * FPix Create/copy/destroy *
138  *--------------------------------------------------------------------*/
152 FPIX *
153 fpixCreate(l_int32 width,
154  l_int32 height)
155 {
156 l_float32 *data;
157 l_uint64 npix64;
158 FPIX *fpixd;
159 
160  PROCNAME("fpixCreate");
161 
162  if (width <= 0)
163  return (FPIX *)ERROR_PTR("width must be > 0", procName, NULL);
164  if (height <= 0)
165  return (FPIX *)ERROR_PTR("height must be > 0", procName, NULL);
166 
167  /* Avoid overflow in malloc arg, malicious or otherwise */
168  npix64 = (l_uint64)width * (l_uint64)height; /* # of 4-byte pixels */
169  if (npix64 >= (1LL << 29)) {
170  L_ERROR("requested w = %d, h = %d\n", procName, width, height);
171  return (FPIX *)ERROR_PTR("requested bytes >= 2^31", procName, NULL);
172  }
173 
174  fpixd = (FPIX *)LEPT_CALLOC(1, sizeof(FPIX));
175  fpixSetDimensions(fpixd, width, height);
176  fpixSetWpl(fpixd, width); /* 4-byte words */
177  fpixd->refcount = 1;
178 
179  data = (l_float32 *)LEPT_CALLOC((size_t)width * height, sizeof(l_float32));
180  if (!data) {
181  fpixDestroy(&fpixd);
182  return (FPIX *)ERROR_PTR("calloc fail for data", procName, NULL);
183  }
184  fpixSetData(fpixd, data);
185  return fpixd;
186 }
187 
188 
202 FPIX *
204 {
205 l_int32 w, h;
206 FPIX *fpixd;
207 
208  PROCNAME("fpixCreateTemplate");
209 
210  if (!fpixs)
211  return (FPIX *)ERROR_PTR("fpixs not defined", procName, NULL);
212 
213  fpixGetDimensions(fpixs, &w, &h);
214  if ((fpixd = fpixCreate(w, h)) == NULL)
215  return (FPIX *)ERROR_PTR("fpixd not made", procName, NULL);
216  fpixCopyResolution(fpixd, fpixs);
217  return fpixd;
218 }
219 
220 
232 FPIX *
234 {
235  PROCNAME("fpixClone");
236 
237  if (!fpix)
238  return (FPIX *)ERROR_PTR("fpix not defined", procName, NULL);
239  fpixChangeRefcount(fpix, 1);
240 
241  return fpix;
242 }
243 
244 
275 FPIX *
276 fpixCopy(FPIX *fpixd, /* can be null */
277  FPIX *fpixs)
278 {
279 l_int32 w, h, bytes;
280 l_float32 *datas, *datad;
281 
282  PROCNAME("fpixCopy");
283 
284  if (!fpixs)
285  return (FPIX *)ERROR_PTR("fpixs not defined", procName, NULL);
286  if (fpixs == fpixd)
287  return fpixd;
288 
289  /* Total bytes in image data */
290  fpixGetDimensions(fpixs, &w, &h);
291  bytes = 4 * w * h;
292 
293  /* If we're making a new fpix ... */
294  if (!fpixd) {
295  if ((fpixd = fpixCreateTemplate(fpixs)) == NULL)
296  return (FPIX *)ERROR_PTR("fpixd not made", procName, NULL);
297  datas = fpixGetData(fpixs);
298  datad = fpixGetData(fpixd);
299  memcpy(datad, datas, bytes);
300  return fpixd;
301  }
302 
303  /* Reallocate image data if sizes are different */
304  fpixResizeImageData(fpixd, fpixs);
305 
306  /* Copy data */
307  fpixCopyResolution(fpixd, fpixs);
308  datas = fpixGetData(fpixs);
309  datad = fpixGetData(fpixd);
310  memcpy(datad, datas, bytes);
311  return fpixd;
312 }
313 
314 
329 l_ok
331  FPIX *fpixs)
332 {
333 l_int32 ws, hs, wd, hd, bytes;
334 l_float32 *data;
335 
336  PROCNAME("fpixResizeImageData");
337 
338  if (!fpixs)
339  return ERROR_INT("fpixs not defined", procName, 1);
340  if (!fpixd)
341  return ERROR_INT("fpixd not defined", procName, 1);
342 
343  fpixGetDimensions(fpixs, &ws, &hs);
344  fpixGetDimensions(fpixd, &wd, &hd);
345  if (ws == wd && hs == hd) /* nothing to do */
346  return 0;
347 
348  fpixSetDimensions(fpixd, ws, hs);
349  fpixSetWpl(fpixd, ws);
350  bytes = 4 * ws * hs;
351  data = fpixGetData(fpixd);
352  if (data) LEPT_FREE(data);
353  if ((data = (l_float32 *)LEPT_MALLOC(bytes)) == NULL)
354  return ERROR_INT("LEPT_MALLOC fail for data", procName, 1);
355  fpixSetData(fpixd, data);
356  return 0;
357 }
358 
359 
372 void
374 {
375 l_float32 *data;
376 FPIX *fpix;
377 
378  PROCNAME("fpixDestroy");
379 
380  if (!pfpix) {
381  L_WARNING("ptr address is null!\n", procName);
382  return;
383  }
384 
385  if ((fpix = *pfpix) == NULL)
386  return;
387 
388  /* Decrement the ref count. If it is 0, destroy the fpix. */
389  fpixChangeRefcount(fpix, -1);
390  if (fpixGetRefcount(fpix) <= 0) {
391  if ((data = fpixGetData(fpix)) != NULL)
392  LEPT_FREE(data);
393  LEPT_FREE(fpix);
394  }
395 
396  *pfpix = NULL;
397  return;
398 }
399 
400 
401 /*--------------------------------------------------------------------*
402  * FPix Accessors *
403  *--------------------------------------------------------------------*/
411 l_ok
413  l_int32 *pw,
414  l_int32 *ph)
415 {
416  PROCNAME("fpixGetDimensions");
417 
418  if (!pw && !ph)
419  return ERROR_INT("no return val requested", procName, 1);
420  if (pw) *pw = 0;
421  if (ph) *ph = 0;
422  if (!fpix)
423  return ERROR_INT("fpix not defined", procName, 1);
424  if (pw) *pw = fpix->w;
425  if (ph) *ph = fpix->h;
426  return 0;
427 }
428 
429 
437 l_ok
439  l_int32 w,
440  l_int32 h)
441 {
442  PROCNAME("fpixSetDimensions");
443 
444  if (!fpix)
445  return ERROR_INT("fpix not defined", procName, 1);
446  fpix->w = w;
447  fpix->h = h;
448  return 0;
449 }
450 
451 
458 l_int32
460 {
461  PROCNAME("fpixGetWpl");
462 
463  if (!fpix)
464  return ERROR_INT("fpix not defined", procName, UNDEF);
465  return fpix->wpl;
466 }
467 
468 
476 l_ok
478  l_int32 wpl)
479 {
480  PROCNAME("fpixSetWpl");
481 
482  if (!fpix)
483  return ERROR_INT("fpix not defined", procName, 1);
484 
485  fpix->wpl = wpl;
486  return 0;
487 }
488 
489 
496 l_int32
498 {
499  PROCNAME("fpixGetRefcount");
500 
501  if (!fpix)
502  return ERROR_INT("fpix not defined", procName, UNDEF);
503  return fpix->refcount;
504 }
505 
506 
514 l_ok
516  l_int32 delta)
517 {
518  PROCNAME("fpixChangeRefcount");
519 
520  if (!fpix)
521  return ERROR_INT("fpix not defined", procName, 1);
522 
523  fpix->refcount += delta;
524  return 0;
525 }
526 
527 
535 l_ok
537  l_int32 *pxres,
538  l_int32 *pyres)
539 {
540  PROCNAME("fpixGetResolution");
541 
542  if (!fpix)
543  return ERROR_INT("fpix not defined", procName, 1);
544  if (pxres) *pxres = fpix->xres;
545  if (pyres) *pyres = fpix->yres;
546  return 0;
547 }
548 
549 
557 l_ok
559  l_int32 xres,
560  l_int32 yres)
561 {
562  PROCNAME("fpixSetResolution");
563 
564  if (!fpix)
565  return ERROR_INT("fpix not defined", procName, 1);
566 
567  fpix->xres = xres;
568  fpix->yres = yres;
569  return 0;
570 }
571 
572 
579 l_ok
581  FPIX *fpixs)
582 {
583 l_int32 xres, yres;
584  PROCNAME("fpixCopyResolution");
585 
586  if (!fpixs || !fpixd)
587  return ERROR_INT("fpixs and fpixd not both defined", procName, 1);
588 
589  fpixGetResolution(fpixs, &xres, &yres);
590  fpixSetResolution(fpixd, xres, yres);
591  return 0;
592 }
593 
594 
601 l_float32 *
603 {
604  PROCNAME("fpixGetData");
605 
606  if (!fpix)
607  return (l_float32 *)ERROR_PTR("fpix not defined", procName, NULL);
608  return fpix->data;
609 }
610 
611 
619 l_ok
621  l_float32 *data)
622 {
623  PROCNAME("fpixSetData");
624 
625  if (!fpix)
626  return ERROR_INT("fpix not defined", procName, 1);
627 
628  fpix->data = data;
629  return 0;
630 }
631 
632 
645 l_ok
647  l_int32 x,
648  l_int32 y,
649  l_float32 *pval)
650 {
651 l_int32 w, h;
652 
653  PROCNAME("fpixGetPixel");
654 
655  if (!pval)
656  return ERROR_INT("pval not defined", procName, 1);
657  *pval = 0.0;
658  if (!fpix)
659  return ERROR_INT("fpix not defined", procName, 1);
660 
661  fpixGetDimensions(fpix, &w, &h);
662  if (x < 0 || x >= w || y < 0 || y >= h)
663  return 2;
664 
665  *pval = *(fpix->data + y * w + x);
666  return 0;
667 }
668 
669 
682 l_ok
684  l_int32 x,
685  l_int32 y,
686  l_float32 val)
687 {
688 l_int32 w, h;
689 
690  PROCNAME("fpixSetPixel");
691 
692  if (!fpix)
693  return ERROR_INT("fpix not defined", procName, 1);
694 
695  fpixGetDimensions(fpix, &w, &h);
696  if (x < 0 || x >= w || y < 0 || y >= h)
697  return 2;
698 
699  *(fpix->data + y * w + x) = val;
700  return 0;
701 }
702 
703 
704 /*--------------------------------------------------------------------*
705  * FPixa Create/copy/destroy *
706  *--------------------------------------------------------------------*/
713 FPIXA *
714 fpixaCreate(l_int32 n)
715 {
716 FPIXA *fpixa;
717 
718  PROCNAME("fpixaCreate");
719 
720  if (n <= 0)
721  n = INITIAL_PTR_ARRAYSIZE;
722 
723  if ((fpixa = (FPIXA *)LEPT_CALLOC(1, sizeof(FPIXA))) == NULL)
724  return (FPIXA *)ERROR_PTR("fpixa not made", procName, NULL);
725  fpixa->n = 0;
726  fpixa->nalloc = n;
727  fpixa->refcount = 1;
728 
729  if ((fpixa->fpix = (FPIX **)LEPT_CALLOC(n, sizeof(FPIX *))) == NULL) {
730  fpixaDestroy(&fpixa);
731  return (FPIXA *)ERROR_PTR("fpixa ptrs not made", procName, NULL);
732  }
733 
734  return fpixa;
735 }
736 
737 
753 FPIXA *
755  l_int32 copyflag)
756 {
757 l_int32 i;
758 FPIX *fpixc;
759 FPIXA *fpixac;
760 
761  PROCNAME("fpixaCopy");
762 
763  if (!fpixa)
764  return (FPIXA *)ERROR_PTR("fpixa not defined", procName, NULL);
765 
766  if (copyflag == L_CLONE) {
767  fpixaChangeRefcount(fpixa, 1);
768  return fpixa;
769  }
770 
771  if (copyflag != L_COPY && copyflag != L_COPY_CLONE)
772  return (FPIXA *)ERROR_PTR("invalid copyflag", procName, NULL);
773 
774  if ((fpixac = fpixaCreate(fpixa->n)) == NULL)
775  return (FPIXA *)ERROR_PTR("fpixac not made", procName, NULL);
776  for (i = 0; i < fpixa->n; i++) {
777  if (copyflag == L_COPY)
778  fpixc = fpixaGetFPix(fpixa, i, L_COPY);
779  else /* copy-clone */
780  fpixc = fpixaGetFPix(fpixa, i, L_CLONE);
781  fpixaAddFPix(fpixac, fpixc, L_INSERT);
782  }
783 
784  return fpixac;
785 }
786 
787 
800 void
802 {
803 l_int32 i;
804 FPIXA *fpixa;
805 
806  PROCNAME("fpixaDestroy");
807 
808  if (pfpixa == NULL) {
809  L_WARNING("ptr address is NULL!\n", procName);
810  return;
811  }
812 
813  if ((fpixa = *pfpixa) == NULL)
814  return;
815 
816  /* Decrement the refcount. If it is 0, destroy the pixa. */
817  fpixaChangeRefcount(fpixa, -1);
818  if (fpixa->refcount <= 0) {
819  for (i = 0; i < fpixa->n; i++)
820  fpixDestroy(&fpixa->fpix[i]);
821  LEPT_FREE(fpixa->fpix);
822  LEPT_FREE(fpixa);
823  }
824 
825  *pfpixa = NULL;
826  return;
827 }
828 
829 
830 /*--------------------------------------------------------------------*
831  * FPixa addition *
832  *--------------------------------------------------------------------*/
841 l_ok
843  FPIX *fpix,
844  l_int32 copyflag)
845 {
846 l_int32 n;
847 FPIX *fpixc;
848 
849  PROCNAME("fpixaAddFPix");
850 
851  if (!fpixa)
852  return ERROR_INT("fpixa not defined", procName, 1);
853  if (!fpix)
854  return ERROR_INT("fpix not defined", procName, 1);
855 
856  if (copyflag == L_INSERT)
857  fpixc = fpix;
858  else if (copyflag == L_COPY)
859  fpixc = fpixCopy(NULL, fpix);
860  else if (copyflag == L_CLONE)
861  fpixc = fpixClone(fpix);
862  else
863  return ERROR_INT("invalid copyflag", procName, 1);
864  if (!fpixc)
865  return ERROR_INT("fpixc not made", procName, 1);
866 
867  n = fpixaGetCount(fpixa);
868  if (n >= fpixa->nalloc)
869  fpixaExtendArray(fpixa);
870  fpixa->fpix[n] = fpixc;
871  fpixa->n++;
872 
873  return 0;
874 }
875 
876 
888 static l_int32
890 {
891  PROCNAME("fpixaExtendArray");
892 
893  if (!fpixa)
894  return ERROR_INT("fpixa not defined", procName, 1);
895 
896  return fpixaExtendArrayToSize(fpixa, 2 * fpixa->nalloc);
897 }
898 
899 
912 static l_int32
914  l_int32 size)
915 {
916  PROCNAME("fpixaExtendArrayToSize");
917 
918  if (!fpixa)
919  return ERROR_INT("fpixa not defined", procName, 1);
920 
921  if (size > fpixa->nalloc) {
922  if ((fpixa->fpix = (FPIX **)reallocNew((void **)&fpixa->fpix,
923  sizeof(FPIX *) * fpixa->nalloc,
924  size * sizeof(FPIX *))) == NULL)
925  return ERROR_INT("new ptr array not returned", procName, 1);
926  fpixa->nalloc = size;
927  }
928  return 0;
929 }
930 
931 
932 /*--------------------------------------------------------------------*
933  * FPixa accessors *
934  *--------------------------------------------------------------------*/
941 l_int32
943 {
944  PROCNAME("fpixaGetCount");
945 
946  if (!fpixa)
947  return ERROR_INT("fpixa not defined", procName, 0);
948 
949  return fpixa->n;
950 }
951 
952 
960 l_ok
962  l_int32 delta)
963 {
964  PROCNAME("fpixaChangeRefcount");
965 
966  if (!fpixa)
967  return ERROR_INT("fpixa not defined", procName, 1);
968 
969  fpixa->refcount += delta;
970  return 0;
971 }
972 
973 
982 FPIX *
984  l_int32 index,
985  l_int32 accesstype)
986 {
987  PROCNAME("fpixaGetFPix");
988 
989  if (!fpixa)
990  return (FPIX *)ERROR_PTR("fpixa not defined", procName, NULL);
991  if (index < 0 || index >= fpixa->n)
992  return (FPIX *)ERROR_PTR("index not valid", procName, NULL);
993 
994  if (accesstype == L_COPY)
995  return fpixCopy(NULL, fpixa->fpix[index]);
996  else if (accesstype == L_CLONE)
997  return fpixClone(fpixa->fpix[index]);
998  else
999  return (FPIX *)ERROR_PTR("invalid accesstype", procName, NULL);
1000 }
1001 
1002 
1011 l_ok
1013  l_int32 index,
1014  l_int32 *pw,
1015  l_int32 *ph)
1016 {
1017 FPIX *fpix;
1018 
1019  PROCNAME("fpixaGetFPixDimensions");
1020 
1021  if (!pw && !ph)
1022  return ERROR_INT("no return val requested", procName, 1);
1023  if (pw) *pw = 0;
1024  if (ph) *ph = 0;
1025  if (!fpixa)
1026  return ERROR_INT("fpixa not defined", procName, 1);
1027  if (index < 0 || index >= fpixa->n)
1028  return ERROR_INT("index not valid", procName, 1);
1029 
1030  if ((fpix = fpixaGetFPix(fpixa, index, L_CLONE)) == NULL)
1031  return ERROR_INT("fpix not found!", procName, 1);
1032  fpixGetDimensions(fpix, pw, ph);
1033  fpixDestroy(&fpix);
1034  return 0;
1035 }
1036 
1037 
1045 l_float32 *
1047  l_int32 index)
1048 {
1049 l_int32 n;
1050 l_float32 *data;
1051 FPIX *fpix;
1052 
1053  PROCNAME("fpixaGetData");
1054 
1055  if (!fpixa)
1056  return (l_float32 *)ERROR_PTR("fpixa not defined", procName, NULL);
1057  n = fpixaGetCount(fpixa);
1058  if (index < 0 || index >= n)
1059  return (l_float32 *)ERROR_PTR("invalid index", procName, NULL);
1060 
1061  fpix = fpixaGetFPix(fpixa, index, L_CLONE);
1062  data = fpixGetData(fpix);
1063  fpixDestroy(&fpix);
1064  return data;
1065 }
1066 
1067 
1077 l_ok
1079  l_int32 index,
1080  l_int32 x,
1081  l_int32 y,
1082  l_float32 *pval)
1083 {
1084 l_int32 n, ret;
1085 FPIX *fpix;
1086 
1087  PROCNAME("fpixaGetPixel");
1088 
1089  if (!pval)
1090  return ERROR_INT("pval not defined", procName, 1);
1091  *pval = 0.0;
1092  if (!fpixa)
1093  return ERROR_INT("fpixa not defined", procName, 1);
1094  n = fpixaGetCount(fpixa);
1095  if (index < 0 || index >= n)
1096  return ERROR_INT("invalid index into fpixa", procName, 1);
1097 
1098  fpix = fpixaGetFPix(fpixa, index, L_CLONE);
1099  ret = fpixGetPixel(fpix, x, y, pval);
1100  fpixDestroy(&fpix);
1101  return ret;
1102 }
1103 
1104 
1114 l_ok
1116  l_int32 index,
1117  l_int32 x,
1118  l_int32 y,
1119  l_float32 val)
1120 {
1121 l_int32 n, ret;
1122 FPIX *fpix;
1123 
1124  PROCNAME("fpixaSetPixel");
1125 
1126  if (!fpixa)
1127  return ERROR_INT("fpixa not defined", procName, 1);
1128  n = fpixaGetCount(fpixa);
1129  if (index < 0 || index >= n)
1130  return ERROR_INT("invalid index into fpixa", procName, 1);
1131 
1132  fpix = fpixaGetFPix(fpixa, index, L_CLONE);
1133  ret = fpixSetPixel(fpix, x, y, val);
1134  fpixDestroy(&fpix);
1135  return ret;
1136 }
1137 
1138 
1139 /*--------------------------------------------------------------------*
1140  * DPix Create/copy/destroy *
1141  *--------------------------------------------------------------------*/
1155 DPIX *
1156 dpixCreate(l_int32 width,
1157  l_int32 height)
1158 {
1159 l_float64 *data;
1160 l_uint64 npix64;
1161 DPIX *dpix;
1162 
1163  PROCNAME("dpixCreate");
1164 
1165  if (width <= 0)
1166  return (DPIX *)ERROR_PTR("width must be > 0", procName, NULL);
1167  if (height <= 0)
1168  return (DPIX *)ERROR_PTR("height must be > 0", procName, NULL);
1169 
1170  /* Avoid overflow in malloc arg, malicious or otherwise */
1171  npix64 = (l_uint64)width * (l_uint64)height; /* # of 8 byte pixels */
1172  if (npix64 >= (1LL << 28)) {
1173  L_ERROR("requested w = %d, h = %d\n", procName, width, height);
1174  return (DPIX *)ERROR_PTR("requested bytes >= 2^31", procName, NULL);
1175  }
1176 
1177  dpix = (DPIX *)LEPT_CALLOC(1, sizeof(DPIX));
1178  dpixSetDimensions(dpix, width, height);
1179  dpixSetWpl(dpix, width); /* 8 byte words */
1180  dpix->refcount = 1;
1181 
1182  data = (l_float64 *)LEPT_CALLOC((size_t)width * height, sizeof(l_float64));
1183  if (!data) {
1184  dpixDestroy(&dpix);
1185  return (DPIX *)ERROR_PTR("calloc fail for data", procName, NULL);
1186  }
1187  dpixSetData(dpix, data);
1188  return dpix;
1189 }
1190 
1191 
1205 DPIX *
1207 {
1208 l_int32 w, h;
1209 DPIX *dpixd;
1210 
1211  PROCNAME("dpixCreateTemplate");
1212 
1213  if (!dpixs)
1214  return (DPIX *)ERROR_PTR("dpixs not defined", procName, NULL);
1215 
1216  dpixGetDimensions(dpixs, &w, &h);
1217  dpixd = dpixCreate(w, h);
1218  dpixCopyResolution(dpixd, dpixs);
1219  return dpixd;
1220 }
1221 
1222 
1234 DPIX *
1236 {
1237  PROCNAME("dpixClone");
1238 
1239  if (!dpix)
1240  return (DPIX *)ERROR_PTR("dpix not defined", procName, NULL);
1241  dpixChangeRefcount(dpix, 1);
1242 
1243  return dpix;
1244 }
1245 
1246 
1277 DPIX *
1278 dpixCopy(DPIX *dpixd, /* can be null */
1279  DPIX *dpixs)
1280 {
1281 l_int32 w, h, bytes;
1282 l_float64 *datas, *datad;
1283 
1284  PROCNAME("dpixCopy");
1285 
1286  if (!dpixs)
1287  return (DPIX *)ERROR_PTR("dpixs not defined", procName, NULL);
1288  if (dpixs == dpixd)
1289  return dpixd;
1290 
1291  /* Total bytes in image data */
1292  dpixGetDimensions(dpixs, &w, &h);
1293  bytes = 8 * w * h;
1294 
1295  /* If we're making a new dpix ... */
1296  if (!dpixd) {
1297  if ((dpixd = dpixCreateTemplate(dpixs)) == NULL)
1298  return (DPIX *)ERROR_PTR("dpixd not made", procName, NULL);
1299  datas = dpixGetData(dpixs);
1300  datad = dpixGetData(dpixd);
1301  memcpy(datad, datas, bytes);
1302  return dpixd;
1303  }
1304 
1305  /* Reallocate image data if sizes are different */
1306  dpixResizeImageData(dpixd, dpixs);
1307 
1308  /* Copy data */
1309  dpixCopyResolution(dpixd, dpixs);
1310  datas = dpixGetData(dpixs);
1311  datad = dpixGetData(dpixd);
1312  memcpy(datad, datas, bytes);
1313  return dpixd;
1314 }
1315 
1316 
1323 l_ok
1325  DPIX *dpixs)
1326 {
1327 l_int32 ws, hs, wd, hd, bytes;
1328 l_float64 *data;
1329 
1330  PROCNAME("dpixResizeImageData");
1331 
1332  if (!dpixs)
1333  return ERROR_INT("dpixs not defined", procName, 1);
1334  if (!dpixd)
1335  return ERROR_INT("dpixd not defined", procName, 1);
1336 
1337  dpixGetDimensions(dpixs, &ws, &hs);
1338  dpixGetDimensions(dpixd, &wd, &hd);
1339  if (ws == wd && hs == hd) /* nothing to do */
1340  return 0;
1341 
1342  dpixSetDimensions(dpixd, ws, hs);
1343  dpixSetWpl(dpixd, ws); /* 8 byte words */
1344  bytes = 8 * ws * hs;
1345  data = dpixGetData(dpixd);
1346  if (data) LEPT_FREE(data);
1347  if ((data = (l_float64 *)LEPT_MALLOC(bytes)) == NULL)
1348  return ERROR_INT("LEPT_MALLOC fail for data", procName, 1);
1349  dpixSetData(dpixd, data);
1350  return 0;
1351 }
1352 
1353 
1366 void
1368 {
1369 l_float64 *data;
1370 DPIX *dpix;
1371 
1372  PROCNAME("dpixDestroy");
1373 
1374  if (!pdpix) {
1375  L_WARNING("ptr address is null!\n", procName);
1376  return;
1377  }
1378 
1379  if ((dpix = *pdpix) == NULL)
1380  return;
1381 
1382  /* Decrement the ref count. If it is 0, destroy the dpix. */
1383  dpixChangeRefcount(dpix, -1);
1384  if (dpixGetRefcount(dpix) <= 0) {
1385  if ((data = dpixGetData(dpix)) != NULL)
1386  LEPT_FREE(data);
1387  LEPT_FREE(dpix);
1388  }
1389 
1390  *pdpix = NULL;
1391  return;
1392 }
1393 
1394 
1395 /*--------------------------------------------------------------------*
1396  * DPix Accessors *
1397  *--------------------------------------------------------------------*/
1405 l_ok
1407  l_int32 *pw,
1408  l_int32 *ph)
1409 {
1410  PROCNAME("dpixGetDimensions");
1411 
1412  if (!pw && !ph)
1413  return ERROR_INT("no return val requested", procName, 1);
1414  if (pw) *pw = 0;
1415  if (ph) *ph = 0;
1416  if (!dpix)
1417  return ERROR_INT("dpix not defined", procName, 1);
1418  if (pw) *pw = dpix->w;
1419  if (ph) *ph = dpix->h;
1420  return 0;
1421 }
1422 
1423 
1431 l_ok
1433  l_int32 w,
1434  l_int32 h)
1435 {
1436  PROCNAME("dpixSetDimensions");
1437 
1438  if (!dpix)
1439  return ERROR_INT("dpix not defined", procName, 1);
1440  dpix->w = w;
1441  dpix->h = h;
1442  return 0;
1443 }
1444 
1445 
1452 l_int32
1454 {
1455  PROCNAME("dpixGetWpl");
1456 
1457  if (!dpix)
1458  return ERROR_INT("dpix not defined", procName, 1);
1459  return dpix->wpl;
1460 }
1461 
1462 
1470 l_ok
1472  l_int32 wpl)
1473 {
1474  PROCNAME("dpixSetWpl");
1475 
1476  if (!dpix)
1477  return ERROR_INT("dpix not defined", procName, 1);
1478 
1479  dpix->wpl = wpl;
1480  return 0;
1481 }
1482 
1483 
1490 l_int32
1492 {
1493  PROCNAME("dpixGetRefcount");
1494 
1495  if (!dpix)
1496  return ERROR_INT("dpix not defined", procName, UNDEF);
1497  return dpix->refcount;
1498 }
1499 
1500 
1508 l_ok
1510  l_int32 delta)
1511 {
1512  PROCNAME("dpixChangeRefcount");
1513 
1514  if (!dpix)
1515  return ERROR_INT("dpix not defined", procName, 1);
1516 
1517  dpix->refcount += delta;
1518  return 0;
1519 }
1520 
1521 
1529 l_ok
1531  l_int32 *pxres,
1532  l_int32 *pyres)
1533 {
1534  PROCNAME("dpixGetResolution");
1535 
1536  if (!dpix)
1537  return ERROR_INT("dpix not defined", procName, 1);
1538  if (pxres) *pxres = dpix->xres;
1539  if (pyres) *pyres = dpix->yres;
1540  return 0;
1541 }
1542 
1543 
1551 l_ok
1553  l_int32 xres,
1554  l_int32 yres)
1555 {
1556  PROCNAME("dpixSetResolution");
1557 
1558  if (!dpix)
1559  return ERROR_INT("dpix not defined", procName, 1);
1560 
1561  dpix->xres = xres;
1562  dpix->yres = yres;
1563  return 0;
1564 }
1565 
1566 
1573 l_ok
1575  DPIX *dpixs)
1576 {
1577 l_int32 xres, yres;
1578  PROCNAME("dpixCopyResolution");
1579 
1580  if (!dpixs || !dpixd)
1581  return ERROR_INT("dpixs and dpixd not both defined", procName, 1);
1582 
1583  dpixGetResolution(dpixs, &xres, &yres);
1584  dpixSetResolution(dpixd, xres, yres);
1585  return 0;
1586 }
1587 
1588 
1595 l_float64 *
1597 {
1598  PROCNAME("dpixGetData");
1599 
1600  if (!dpix)
1601  return (l_float64 *)ERROR_PTR("dpix not defined", procName, NULL);
1602  return dpix->data;
1603 }
1604 
1605 
1613 l_ok
1615  l_float64 *data)
1616 {
1617  PROCNAME("dpixSetData");
1618 
1619  if (!dpix)
1620  return ERROR_INT("dpix not defined", procName, 1);
1621 
1622  dpix->data = data;
1623  return 0;
1624 }
1625 
1626 
1639 l_ok
1641  l_int32 x,
1642  l_int32 y,
1643  l_float64 *pval)
1644 {
1645 l_int32 w, h;
1646 
1647  PROCNAME("dpixGetPixel");
1648 
1649  if (!pval)
1650  return ERROR_INT("pval not defined", procName, 1);
1651  *pval = 0.0;
1652  if (!dpix)
1653  return ERROR_INT("dpix not defined", procName, 1);
1654 
1655  dpixGetDimensions(dpix, &w, &h);
1656  if (x < 0 || x >= w || y < 0 || y >= h)
1657  return 2;
1658 
1659  *pval = *(dpix->data + y * w + x);
1660  return 0;
1661 }
1662 
1663 
1676 l_ok
1678  l_int32 x,
1679  l_int32 y,
1680  l_float64 val)
1681 {
1682 l_int32 w, h;
1683 
1684  PROCNAME("dpixSetPixel");
1685 
1686  if (!dpix)
1687  return ERROR_INT("dpix not defined", procName, 1);
1688 
1689  dpixGetDimensions(dpix, &w, &h);
1690  if (x < 0 || x >= w || y < 0 || y >= h)
1691  return 2;
1692 
1693  *(dpix->data + y * w + x) = val;
1694  return 0;
1695 }
1696 
1697 
1698 /*--------------------------------------------------------------------*
1699  * FPix serialized I/O *
1700  *--------------------------------------------------------------------*/
1707 FPIX *
1708 fpixRead(const char *filename)
1709 {
1710 FILE *fp;
1711 FPIX *fpix;
1712 
1713  PROCNAME("fpixRead");
1714 
1715  if (!filename)
1716  return (FPIX *)ERROR_PTR("filename not defined", procName, NULL);
1717 
1718  if ((fp = fopenReadStream(filename)) == NULL)
1719  return (FPIX *)ERROR_PTR("stream not opened", procName, NULL);
1720  fpix = fpixReadStream(fp);
1721  fclose(fp);
1722  if (!fpix)
1723  return (FPIX *)ERROR_PTR("fpix not read", procName, NULL);
1724  return fpix;
1725 }
1726 
1727 
1734 FPIX *
1736 {
1737 char buf[256];
1738 l_int32 w, h, nbytes, xres, yres, version;
1739 l_float32 *data;
1740 FPIX *fpix;
1741 
1742  PROCNAME("fpixReadStream");
1743 
1744  if (!fp)
1745  return (FPIX *)ERROR_PTR("stream not defined", procName, NULL);
1746 
1747  if (fscanf(fp, "\nFPix Version %d\n", &version) != 1)
1748  return (FPIX *)ERROR_PTR("not a fpix file", procName, NULL);
1749  if (version != FPIX_VERSION_NUMBER)
1750  return (FPIX *)ERROR_PTR("invalid fpix version", procName, NULL);
1751  if (fscanf(fp, "w = %d, h = %d, nbytes = %d\n", &w, &h, &nbytes) != 3)
1752  return (FPIX *)ERROR_PTR("read fail for data size", procName, NULL);
1753 
1754  /* Use fgets() and sscanf(); not fscanf(), for the last
1755  * bit of header data before the float data. The reason is
1756  * that fscanf throws away white space, and if the float data
1757  * happens to begin with ascii character(s) that are white
1758  * space, it will swallow them and all will be lost! */
1759  if (fgets(buf, sizeof(buf), fp) == NULL)
1760  return (FPIX *)ERROR_PTR("fgets read fail", procName, NULL);
1761  if (sscanf(buf, "xres = %d, yres = %d\n", &xres, &yres) != 2)
1762  return (FPIX *)ERROR_PTR("read fail for xres, yres", procName, NULL);
1763 
1764  if ((fpix = fpixCreate(w, h)) == NULL)
1765  return (FPIX *)ERROR_PTR("fpix not made", procName, NULL);
1766  fpixSetResolution(fpix, xres, yres);
1767  data = fpixGetData(fpix);
1768  if (fread(data, 1, nbytes, fp) != nbytes) {
1769  fpixDestroy(&fpix);
1770  return (FPIX *)ERROR_PTR("read error for nbytes", procName, NULL);
1771  }
1772  fgetc(fp); /* ending nl */
1773 
1774  /* Convert to little-endian if necessary */
1775  fpixEndianByteSwap(fpix, fpix);
1776  return fpix;
1777 }
1778 
1779 
1787 FPIX *
1788 fpixReadMem(const l_uint8 *data,
1789  size_t size)
1790 {
1791 FILE *fp;
1792 FPIX *fpix;
1793 
1794  PROCNAME("fpixReadMem");
1795 
1796  if (!data)
1797  return (FPIX *)ERROR_PTR("data not defined", procName, NULL);
1798  if ((fp = fopenReadFromMemory(data, size)) == NULL)
1799  return (FPIX *)ERROR_PTR("stream not opened", procName, NULL);
1800 
1801  fpix = fpixReadStream(fp);
1802  fclose(fp);
1803  if (!fpix) L_ERROR("fpix not read\n", procName);
1804  return fpix;
1805 }
1806 
1807 
1815 l_ok
1816 fpixWrite(const char *filename,
1817  FPIX *fpix)
1818 {
1819 l_int32 ret;
1820 FILE *fp;
1821 
1822  PROCNAME("fpixWrite");
1823 
1824  if (!filename)
1825  return ERROR_INT("filename not defined", procName, 1);
1826  if (!fpix)
1827  return ERROR_INT("fpix not defined", procName, 1);
1828 
1829  if ((fp = fopenWriteStream(filename, "wb")) == NULL)
1830  return ERROR_INT("stream not opened", procName, 1);
1831  ret = fpixWriteStream(fp, fpix);
1832  fclose(fp);
1833  if (ret)
1834  return ERROR_INT("fpix not written to stream", procName, 1);
1835  return 0;
1836 }
1837 
1838 
1846 l_ok
1848  FPIX *fpix)
1849 {
1850 l_int32 w, h, xres, yres;
1851 l_uint32 nbytes;
1852 l_float32 *data;
1853 FPIX *fpixt;
1854 
1855  PROCNAME("fpixWriteStream");
1856 
1857  if (!fp)
1858  return ERROR_INT("stream not defined", procName, 1);
1859  if (!fpix)
1860  return ERROR_INT("fpix not defined", procName, 1);
1861 
1862  /* Convert to little-endian if necessary */
1863  fpixt = fpixEndianByteSwap(NULL, fpix);
1864 
1865  fpixGetDimensions(fpixt, &w, &h);
1866  data = fpixGetData(fpixt);
1867  nbytes = sizeof(l_float32) * w * h;
1868  fpixGetResolution(fpixt, &xres, &yres);
1869  fprintf(fp, "\nFPix Version %d\n", FPIX_VERSION_NUMBER);
1870  fprintf(fp, "w = %d, h = %d, nbytes = %u\n", w, h, nbytes);
1871  fprintf(fp, "xres = %d, yres = %d\n", xres, yres);
1872  fwrite(data, 1, nbytes, fp);
1873  fprintf(fp, "\n");
1874 
1875  fpixDestroy(&fpixt);
1876  return 0;
1877 }
1878 
1879 
1893 l_ok
1894 fpixWriteMem(l_uint8 **pdata,
1895  size_t *psize,
1896  FPIX *fpix)
1897 {
1898 l_int32 ret;
1899 FILE *fp;
1900 
1901  PROCNAME("fpixWriteMem");
1902 
1903  if (pdata) *pdata = NULL;
1904  if (psize) *psize = 0;
1905  if (!pdata)
1906  return ERROR_INT("&data not defined", procName, 1);
1907  if (!psize)
1908  return ERROR_INT("&size not defined", procName, 1);
1909  if (!fpix)
1910  return ERROR_INT("fpix not defined", procName, 1);
1911 
1912 #if HAVE_FMEMOPEN
1913  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
1914  return ERROR_INT("stream not opened", procName, 1);
1915  ret = fpixWriteStream(fp, fpix);
1916 #else
1917  L_INFO("work-around: writing to a temp file\n", procName);
1918  #ifdef _WIN32
1919  if ((fp = fopenWriteWinTempfile()) == NULL)
1920  return ERROR_INT("tmpfile stream not opened", procName, 1);
1921  #else
1922  if ((fp = tmpfile()) == NULL)
1923  return ERROR_INT("tmpfile stream not opened", procName, 1);
1924  #endif /* _WIN32 */
1925  ret = fpixWriteStream(fp, fpix);
1926  rewind(fp);
1927  *pdata = l_binaryReadStream(fp, psize);
1928 #endif /* HAVE_FMEMOPEN */
1929  fclose(fp);
1930  return ret;
1931 }
1932 
1933 
1953 FPIX *
1955  FPIX *fpixs)
1956 {
1957  PROCNAME("fpixEndianByteSwap");
1958 
1959  if (!fpixs)
1960  return (FPIX *)ERROR_PTR("fpixs not defined", procName, fpixd);
1961  if (fpixd && (fpixs != fpixd))
1962  return (FPIX *)ERROR_PTR("fpixd != fpixs", procName, fpixd);
1963 
1964 #ifdef L_BIG_ENDIAN
1965  {
1966  l_uint32 *data;
1967  l_int32 i, j, w, h;
1968  l_uint32 word;
1969 
1970  fpixGetDimensions(fpixs, &w, &h);
1971  fpixd = fpixCopy(fpixd, fpixs); /* no copy if fpixd == fpixs */
1972 
1973  data = (l_uint32 *)fpixGetData(fpixd);
1974  for (i = 0; i < h; i++) {
1975  for (j = 0; j < w; j++, data++) {
1976  word = *data;
1977  *data = (word >> 24) |
1978  ((word >> 8) & 0x0000ff00) |
1979  ((word << 8) & 0x00ff0000) |
1980  (word << 24);
1981  }
1982  }
1983  return fpixd;
1984  }
1985 #else /* L_LITTLE_ENDIAN */
1986 
1987  if (fpixd)
1988  return fpixd; /* no-op */
1989  else
1990  return fpixClone(fpixs);
1991 
1992 #endif /* L_BIG_ENDIAN */
1993 }
1994 
1995 
1996 /*--------------------------------------------------------------------*
1997  * DPix serialized I/O *
1998  *--------------------------------------------------------------------*/
2005 DPIX *
2006 dpixRead(const char *filename)
2007 {
2008 FILE *fp;
2009 DPIX *dpix;
2010 
2011  PROCNAME("dpixRead");
2012 
2013  if (!filename)
2014  return (DPIX *)ERROR_PTR("filename not defined", procName, NULL);
2015 
2016  if ((fp = fopenReadStream(filename)) == NULL)
2017  return (DPIX *)ERROR_PTR("stream not opened", procName, NULL);
2018  dpix = dpixReadStream(fp);
2019  fclose(fp);
2020  if (!dpix)
2021  return (DPIX *)ERROR_PTR("dpix not read", procName, NULL);
2022  return dpix;
2023 }
2024 
2025 
2032 DPIX *
2034 {
2035 char buf[256];
2036 l_int32 w, h, nbytes, version, xres, yres;
2037 l_float64 *data;
2038 DPIX *dpix;
2039 
2040  PROCNAME("dpixReadStream");
2041 
2042  if (!fp)
2043  return (DPIX *)ERROR_PTR("stream not defined", procName, NULL);
2044 
2045  if (fscanf(fp, "\nDPix Version %d\n", &version) != 1)
2046  return (DPIX *)ERROR_PTR("not a dpix file", procName, NULL);
2047  if (version != DPIX_VERSION_NUMBER)
2048  return (DPIX *)ERROR_PTR("invalid dpix version", procName, NULL);
2049  if (fscanf(fp, "w = %d, h = %d, nbytes = %d\n", &w, &h, &nbytes) != 3)
2050  return (DPIX *)ERROR_PTR("read fail for data size", procName, NULL);
2051 
2052  /* Use fgets() and sscanf(); not fscanf(), for the last
2053  * bit of header data before the float data. The reason is
2054  * that fscanf throws away white space, and if the float data
2055  * happens to begin with ascii character(s) that are white
2056  * space, it will swallow them and all will be lost! */
2057  if (fgets(buf, sizeof(buf), fp) == NULL)
2058  return (DPIX *)ERROR_PTR("fgets read fail", procName, NULL);
2059  if (sscanf(buf, "xres = %d, yres = %d\n", &xres, &yres) != 2)
2060  return (DPIX *)ERROR_PTR("read fail for xres, yres", procName, NULL);
2061 
2062  if ((dpix = dpixCreate(w, h)) == NULL)
2063  return (DPIX *)ERROR_PTR("dpix not made", procName, NULL);
2064  dpixSetResolution(dpix, xres, yres);
2065  data = dpixGetData(dpix);
2066  if (fread(data, 1, nbytes, fp) != nbytes) {
2067  dpixDestroy(&dpix);
2068  return (DPIX *)ERROR_PTR("read error for nbytes", procName, NULL);
2069  }
2070  fgetc(fp); /* ending nl */
2071 
2072  /* Convert to little-endian if necessary */
2073  dpixEndianByteSwap(dpix, dpix);
2074  return dpix;
2075 }
2076 
2077 
2085 DPIX *
2086 dpixReadMem(const l_uint8 *data,
2087  size_t size)
2088 {
2089 FILE *fp;
2090 DPIX *dpix;
2091 
2092  PROCNAME("dpixReadMem");
2093 
2094  if (!data)
2095  return (DPIX *)ERROR_PTR("data not defined", procName, NULL);
2096  if ((fp = fopenReadFromMemory(data, size)) == NULL)
2097  return (DPIX *)ERROR_PTR("stream not opened", procName, NULL);
2098 
2099  dpix = dpixReadStream(fp);
2100  fclose(fp);
2101  if (!dpix) L_ERROR("dpix not read\n", procName);
2102  return dpix;
2103 }
2104 
2105 
2113 l_ok
2114 dpixWrite(const char *filename,
2115  DPIX *dpix)
2116 {
2117 l_int32 ret;
2118 FILE *fp;
2119 
2120  PROCNAME("dpixWrite");
2121 
2122  if (!filename)
2123  return ERROR_INT("filename not defined", procName, 1);
2124  if (!dpix)
2125  return ERROR_INT("dpix not defined", procName, 1);
2126 
2127  if ((fp = fopenWriteStream(filename, "wb")) == NULL)
2128  return ERROR_INT("stream not opened", procName, 1);
2129  ret = dpixWriteStream(fp, dpix);
2130  fclose(fp);
2131  if (ret)
2132  return ERROR_INT("dpix not written to stream", procName, 1);
2133  return 0;
2134 }
2135 
2136 
2144 l_ok
2146  DPIX *dpix)
2147 {
2148 l_int32 w, h, xres, yres;
2149 l_uint32 nbytes;
2150 l_float64 *data;
2151 DPIX *dpixt;
2152 
2153  PROCNAME("dpixWriteStream");
2154 
2155  if (!fp)
2156  return ERROR_INT("stream not defined", procName, 1);
2157  if (!dpix)
2158  return ERROR_INT("dpix not defined", procName, 1);
2159 
2160  /* Convert to little-endian if necessary */
2161  dpixt = dpixEndianByteSwap(NULL, dpix);
2162 
2163  dpixGetDimensions(dpixt, &w, &h);
2164  dpixGetResolution(dpixt, &xres, &yres);
2165  data = dpixGetData(dpixt);
2166  nbytes = sizeof(l_float64) * w * h;
2167  fprintf(fp, "\nDPix Version %d\n", DPIX_VERSION_NUMBER);
2168  fprintf(fp, "w = %d, h = %d, nbytes = %u\n", w, h, nbytes);
2169  fprintf(fp, "xres = %d, yres = %d\n", xres, yres);
2170  fwrite(data, 1, nbytes, fp);
2171  fprintf(fp, "\n");
2172 
2173  dpixDestroy(&dpixt);
2174  return 0;
2175 }
2176 
2177 
2191 l_ok
2192 dpixWriteMem(l_uint8 **pdata,
2193  size_t *psize,
2194  DPIX *dpix)
2195 {
2196 l_int32 ret;
2197 FILE *fp;
2198 
2199  PROCNAME("dpixWriteMem");
2200 
2201  if (pdata) *pdata = NULL;
2202  if (psize) *psize = 0;
2203  if (!pdata)
2204  return ERROR_INT("&data not defined", procName, 1);
2205  if (!psize)
2206  return ERROR_INT("&size not defined", procName, 1);
2207  if (!dpix)
2208  return ERROR_INT("dpix not defined", procName, 1);
2209 
2210 #if HAVE_FMEMOPEN
2211  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
2212  return ERROR_INT("stream not opened", procName, 1);
2213  ret = dpixWriteStream(fp, dpix);
2214 #else
2215  L_INFO("work-around: writing to a temp file\n", procName);
2216  #ifdef _WIN32
2217  if ((fp = fopenWriteWinTempfile()) == NULL)
2218  return ERROR_INT("tmpfile stream not opened", procName, 1);
2219  #else
2220  if ((fp = tmpfile()) == NULL)
2221  return ERROR_INT("tmpfile stream not opened", procName, 1);
2222  #endif /* _WIN32 */
2223  ret = dpixWriteStream(fp, dpix);
2224  rewind(fp);
2225  *pdata = l_binaryReadStream(fp, psize);
2226 #endif /* HAVE_FMEMOPEN */
2227  fclose(fp);
2228  return ret;
2229 }
2230 
2231 
2251 DPIX *
2253  DPIX *dpixs)
2254 {
2255  PROCNAME("dpixEndianByteSwap");
2256 
2257  if (!dpixs)
2258  return (DPIX *)ERROR_PTR("dpixs not defined", procName, dpixd);
2259  if (dpixd && (dpixs != dpixd))
2260  return (DPIX *)ERROR_PTR("dpixd != dpixs", procName, dpixd);
2261 
2262 #ifdef L_BIG_ENDIAN
2263  {
2264  l_uint32 *data;
2265  l_int32 i, j, w, h;
2266  l_uint32 word;
2267 
2268  dpixGetDimensions(dpixs, &w, &h);
2269  dpixd = dpixCopy(dpixd, dpixs); /* no copy if dpixd == dpixs */
2270 
2271  data = (l_uint32 *)dpixGetData(dpixd);
2272  for (i = 0; i < h; i++) {
2273  for (j = 0; j < 2 * w; j++, data++) {
2274  word = *data;
2275  *data = (word >> 24) |
2276  ((word >> 8) & 0x0000ff00) |
2277  ((word << 8) & 0x00ff0000) |
2278  (word << 24);
2279  }
2280  }
2281  return dpixd;
2282  }
2283 #else /* L_LITTLE_ENDIAN */
2284 
2285  if (dpixd)
2286  return dpixd; /* no-op */
2287  else
2288  return dpixClone(dpixs);
2289 
2290 #endif /* L_BIG_ENDIAN */
2291 }
2292 
2293 
2294 /*--------------------------------------------------------------------*
2295  * Print FPix (subsampled, for debugging) *
2296  *--------------------------------------------------------------------*/
2310 l_ok
2312  FPIX *fpix,
2313  l_int32 factor)
2314 {
2315 l_int32 i, j, w, h, count;
2316 l_float32 val;
2317 
2318  PROCNAME("fpixPrintStream");
2319 
2320  if (!fp)
2321  return ERROR_INT("stream not defined", procName, 1);
2322  if (!fpix)
2323  return ERROR_INT("fpix not defined", procName, 1);
2324  if (factor < 1)
2325  return ERROR_INT("sampling factor < 1f", procName, 1);
2326 
2327  fpixGetDimensions(fpix, &w, &h);
2328  fprintf(fp, "\nFPix: w = %d, h = %d\n", w, h);
2329  for (i = 0; i < h; i += factor) {
2330  for (count = 0, j = 0; j < w; j += factor, count++) {
2331  fpixGetPixel(fpix, j, i, &val);
2332  fprintf(fp, "val[%d, %d] = %f ", i, j, val);
2333  if ((count + 1) % 3 == 0) fprintf(fp, "\n");
2334  }
2335  if (count % 3) fprintf(fp, "\n");
2336  }
2337  fprintf(fp, "\n");
2338  return 0;
2339 }
l_ok fpixCopyResolution(FPIX *fpixd, FPIX *fpixs)
fpixCopyResolution()
Definition: fpix1.c:580
DPIX * dpixClone(DPIX *dpix)
dpixClone()
Definition: fpix1.c:1235
FPIXA * fpixaCopy(FPIXA *fpixa, l_int32 copyflag)
fpixaCopy()
Definition: fpix1.c:754
void dpixDestroy(DPIX **pdpix)
dpixDestroy()
Definition: fpix1.c:1367
l_uint32 refcount
Definition: pix.h:618
Definition: pix.h:717
struct FPix ** fpix
Definition: pix.h:602
l_ok fpixaGetPixel(FPIXA *fpixa, l_int32 index, l_int32 x, l_int32 y, l_float32 *pval)
fpixaGetPixel()
Definition: fpix1.c:1078
l_ok fpixGetPixel(FPIX *fpix, l_int32 x, l_int32 y, l_float32 *pval)
fpixGetPixel()
Definition: fpix1.c:646
DPIX * dpixCreate(l_int32 width, l_int32 height)
dpixCreate()
Definition: fpix1.c:1156
l_ok dpixChangeRefcount(DPIX *dpix, l_int32 delta)
dpixChangeRefcount()
Definition: fpix1.c:1509
l_ok dpixCopyResolution(DPIX *dpixd, DPIX *dpixs)
dpixCopyResolution()
Definition: fpix1.c:1574
l_int32 yres
Definition: pix.h:621
l_uint32 refcount
Definition: pix.h:587
l_int32 fpixaGetCount(FPIXA *fpixa)
fpixaGetCount()
Definition: fpix1.c:942
l_float64 * dpixGetData(DPIX *dpix)
dpixGetData()
Definition: fpix1.c:1596
l_ok fpixaChangeRefcount(FPIXA *fpixa, l_int32 delta)
fpixaChangeRefcount()
Definition: fpix1.c:961
l_ok fpixWriteStream(FILE *fp, FPIX *fpix)
fpixWriteStream()
Definition: fpix1.c:1847
l_int32 dpixGetWpl(DPIX *dpix)
dpixGetWpl()
Definition: fpix1.c:1453
l_ok dpixSetPixel(DPIX *dpix, l_int32 x, l_int32 y, l_float64 val)
dpixSetPixel()
Definition: fpix1.c:1677
l_ok dpixSetDimensions(DPIX *dpix, l_int32 w, l_int32 h)
dpixSetDimensions()
Definition: fpix1.c:1432
l_ok fpixResizeImageData(FPIX *fpixd, FPIX *fpixs)
fpixResizeImageData()
Definition: fpix1.c:330
DPIX * dpixRead(const char *filename)
dpixRead()
Definition: fpix1.c:2006
l_ok dpixWrite(const char *filename, DPIX *dpix)
dpixWrite()
Definition: fpix1.c:2114
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:1734
l_ok fpixGetDimensions(FPIX *fpix, l_int32 *pw, l_int32 *ph)
fpixGetDimensions()
Definition: fpix1.c:412
FPIX * fpixEndianByteSwap(FPIX *fpixd, FPIX *fpixs)
fpixEndianByteSwap()
Definition: fpix1.c:1954
Definition: pix.h:597
l_ok dpixSetResolution(DPIX *dpix, l_int32 xres, l_int32 yres)
dpixSetResolution()
Definition: fpix1.c:1552
FPIXA * fpixaCreate(l_int32 n)
fpixaCreate()
Definition: fpix1.c:714
void fpixaDestroy(FPIXA **pfpixa)
fpixaDestroy()
Definition: fpix1.c:801
DPIX * dpixCopy(DPIX *dpixd, DPIX *dpixs)
dpixCopy()
Definition: fpix1.c:1278
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
Definition: utils2.c:1161
l_int32 nalloc
Definition: pix.h:600
DPIX * dpixCreateTemplate(DPIX *dpixs)
dpixCreateTemplate()
Definition: fpix1.c:1206
l_ok dpixResizeImageData(DPIX *dpixd, DPIX *dpixs)
dpixResizeImageData()
Definition: fpix1.c:1324
l_ok fpixSetResolution(FPIX *fpix, l_int32 xres, l_int32 yres)
fpixSetResolution()
Definition: fpix1.c:558
l_ok fpixSetData(FPIX *fpix, l_float32 *data)
fpixSetData()
Definition: fpix1.c:620
l_ok fpixSetPixel(FPIX *fpix, l_int32 x, l_int32 y, l_float32 val)
fpixSetPixel()
Definition: fpix1.c:683
l_ok fpixaGetFPixDimensions(FPIXA *fpixa, l_int32 index, l_int32 *pw, l_int32 *ph)
fpixaGetFPixDimensions()
Definition: fpix1.c:1012
l_ok dpixSetWpl(DPIX *dpix, l_int32 wpl)
dpixSetWpl()
Definition: fpix1.c:1471
l_ok fpixWrite(const char *filename, FPIX *fpix)
fpixWrite()
Definition: fpix1.c:1816
l_int32 wpl
Definition: pix.h:586
l_ok dpixGetResolution(DPIX *dpix, l_int32 *pxres, l_int32 *pyres)
dpixGetResolution()
Definition: fpix1.c:1530
FPIX * fpixReadMem(const l_uint8 *data, size_t size)
fpixReadMem()
Definition: fpix1.c:1788
DPIX * dpixEndianByteSwap(DPIX *dpixd, DPIX *dpixs)
dpixEndianByteSwap()
Definition: fpix1.c:2252
l_ok fpixGetResolution(FPIX *fpix, l_int32 *pxres, l_int32 *pyres)
fpixGetResolution()
Definition: fpix1.c:536
l_ok fpixSetDimensions(FPIX *fpix, l_int32 w, l_int32 h)
fpixSetDimensions()
Definition: fpix1.c:438
l_int32 yres
Definition: pix.h:590
l_ok fpixaSetPixel(FPIXA *fpixa, l_int32 index, l_int32 x, l_int32 y, l_float32 val)
fpixaSetPixel()
Definition: fpix1.c:1115
l_ok fpixPrintStream(FILE *fp, FPIX *fpix, l_int32 factor)
fpixPrintStream()
Definition: fpix1.c:2311
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
Definition: utils2.c:1780
l_int32 fpixGetWpl(FPIX *fpix)
fpixGetWpl()
Definition: fpix1.c:459
FPIX * fpixClone(FPIX *fpix)
fpixClone()
Definition: fpix1.c:233
l_float32 * fpixaGetData(FPIXA *fpixa, l_int32 index)
fpixaGetData()
Definition: fpix1.c:1046
l_ok dpixGetPixel(DPIX *dpix, l_int32 x, l_int32 y, l_float64 *pval)
dpixGetPixel()
Definition: fpix1.c:1640
l_ok dpixWriteMem(l_uint8 **pdata, size_t *psize, DPIX *dpix)
dpixWriteMem()
Definition: fpix1.c:2192
DPIX * dpixReadMem(const l_uint8 *data, size_t size)
dpixReadMem()
Definition: fpix1.c:2086
l_int32 w
Definition: pix.h:615
l_int32 xres
Definition: pix.h:588
l_float32 * fpixGetData(FPIX *fpix)
fpixGetData()
Definition: fpix1.c:602
l_ok fpixChangeRefcount(FPIX *fpix, l_int32 delta)
fpixChangeRefcount()
Definition: fpix1.c:515
l_float64 * data
Definition: pix.h:623
l_ok fpixWriteMem(l_uint8 **pdata, size_t *psize, FPIX *fpix)
fpixWriteMem()
Definition: fpix1.c:1894
l_int32 h
Definition: pix.h:616
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1700
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1657
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
Definition: utils2.c:1262
l_ok dpixGetDimensions(DPIX *dpix, l_int32 *pw, l_int32 *ph)
dpixGetDimensions()
Definition: fpix1.c:1406
FPIX * fpixCreateTemplate(FPIX *fpixs)
fpixCreateTemplate()
Definition: fpix1.c:203
FPIX * fpixCreate(l_int32 width, l_int32 height)
fpixCreate()
Definition: fpix1.c:153
static l_int32 fpixaExtendArrayToSize(FPIXA *fpixa, l_int32 size)
fpixaExtendArrayToSize()
Definition: fpix1.c:913
l_ok dpixWriteStream(FILE *fp, DPIX *dpix)
dpixWriteStream()
Definition: fpix1.c:2145
l_int32 w
Definition: pix.h:584
#define DPIX_VERSION_NUMBER
Definition: pix.h:610
l_int32 h
Definition: pix.h:585
Definition: pix.h:718
l_float32 * data
Definition: pix.h:592
Definition: pix.h:719
FPIX * fpixCopy(FPIX *fpixd, FPIX *fpixs)
fpixCopy()
Definition: fpix1.c:276
FPIX * fpixRead(const char *filename)
fpixRead()
Definition: fpix1.c:1708
l_int32 wpl
Definition: pix.h:617
FPIX * fpixReadStream(FILE *fp)
fpixReadStream()
Definition: fpix1.c:1735
static l_int32 fpixaExtendArray(FPIXA *fpixa)
fpixaExtendArray()
Definition: fpix1.c:889
void fpixDestroy(FPIX **pfpix)
fpixDestroy()
Definition: fpix1.c:373
l_int32 fpixGetRefcount(FPIX *fpix)
fpixGetRefcount()
Definition: fpix1.c:497
Definition: pix.h:613
l_int32 n
Definition: pix.h:599
l_int32 dpixGetRefcount(DPIX *dpix)
dpixGetRefcount()
Definition: fpix1.c:1491
l_ok fpixSetWpl(FPIX *fpix, l_int32 wpl)
fpixSetWpl()
Definition: fpix1.c:477
l_ok dpixSetData(DPIX *dpix, l_float64 *data)
dpixSetData()
Definition: fpix1.c:1614
FPIX * fpixaGetFPix(FPIXA *fpixa, l_int32 index, l_int32 accesstype)
fpixaGetFPix()
Definition: fpix1.c:983
DPIX * dpixReadStream(FILE *fp)
dpixReadStream()
Definition: fpix1.c:2033
l_ok fpixaAddFPix(FPIXA *fpixa, FPIX *fpix, l_int32 copyflag)
fpixaAddFPix()
Definition: fpix1.c:842
#define FPIX_VERSION_NUMBER
Definition: pix.h:579
Definition: pix.h:582