Leptonica  1.73
Image processing and image analysis suite
pix1.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 
192 #include <string.h>
193 #include "allheaders.h"
194 
195 static void pixFree(PIX *pix);
196 
197 
198 /*-------------------------------------------------------------------------*
199  * Pix Memory Management *
200  * *
201  * These functions give you the freedom to specify at compile or run *
202  * time the allocator and deallocator to be used for pix. It has no *
203  * effect on memory management for other data structs, which are *
204  * controlled by the #defines in environ.h. Likewise, the #defines *
205  * in environ.h have no effect on the pix memory management. *
206  * The default functions are malloc and free. Use setPixMemoryManager() *
207  * to specify other functions to use. *
208  *-------------------------------------------------------------------------*/
209 
211  /*
212  * <pre>
213  * Notes:
214  * (1) The allocator and deallocator function types,
215  * alloc_fn and dealloc_fn, are defined in pix.h.
216  * </pre>
217  */
219 {
220  alloc_fn allocator;
221  dealloc_fn deallocator;
222 };
223 
226  &malloc,
227  &free
228 };
229 
230 static void *
231 pix_malloc(size_t size)
232 {
233 #ifndef _MSC_VER
234  return (*pix_mem_manager.allocator)(size);
235 #else /* _MSC_VER */
236  /* Under MSVC++, pix_mem_manager is initialized after a call
237  * to pix_malloc. Just ignore the custom allocator feature. */
238  return malloc(size);
239 #endif /* _MSC_VER */
240 }
241 
242 static void
243 pix_free(void *ptr)
244 {
245 #ifndef _MSC_VER
246  (*pix_mem_manager.deallocator)(ptr);
247  return;
248 #else /* _MSC_VER */
249  /* Under MSVC++, pix_mem_manager is initialized after a call
250  * to pix_malloc. Just ignore the custom allocator feature. */
251  free(ptr);
252  return;
253 #endif /* _MSC_VER */
254 }
255 
281 void
283  dealloc_fn deallocator)
284 {
285  if (allocator) pix_mem_manager.allocator = allocator;
286  if (deallocator) pix_mem_manager.deallocator = deallocator;
287  return;
288 }
289 
290 
291 /*--------------------------------------------------------------------*
292  * Pix Creation *
293  *--------------------------------------------------------------------*/
301 PIX *
302 pixCreate(l_int32 width,
303  l_int32 height,
304  l_int32 depth)
305 {
306 PIX *pixd;
307 
308  PROCNAME("pixCreate");
309 
310  if ((pixd = pixCreateNoInit(width, height, depth)) == NULL)
311  return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
312  memset(pixd->data, 0, 4 * pixd->wpl * pixd->h);
313  return pixd;
314 }
315 
316 
330 PIX *
331 pixCreateNoInit(l_int32 width,
332  l_int32 height,
333  l_int32 depth)
334 {
335 l_int32 wpl;
336 PIX *pixd;
337 l_uint32 *data;
338 
339  PROCNAME("pixCreateNoInit");
340  if ((pixd = pixCreateHeader(width, height, depth)) == NULL)
341  return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
342  wpl = pixGetWpl(pixd);
343  if ((data = (l_uint32 *)pix_malloc(4LL * wpl * height)) == NULL) {
344  pixDestroy(&pixd);
345  return (PIX *)ERROR_PTR("pix_malloc fail for data", procName, NULL);
346  }
347  pixSetData(pixd, data);
348  pixSetPadBits(pixd, 0);
349  return pixd;
350 }
351 
352 
366 PIX *
368 {
369 PIX *pixd;
370 
371  PROCNAME("pixCreateTemplate");
372 
373  if (!pixs)
374  return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
375 
376  if ((pixd = pixCreateTemplateNoInit(pixs)) == NULL)
377  return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
378  memset(pixd->data, 0, 4 * pixd->wpl * pixd->h);
379  return pixd;
380 }
381 
382 
396 PIX *
398 {
399 l_int32 w, h, d;
400 PIX *pixd;
401 
402  PROCNAME("pixCreateTemplateNoInit");
403 
404  if (!pixs)
405  return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
406 
407  pixGetDimensions(pixs, &w, &h, &d);
408  if ((pixd = pixCreateNoInit(w, h, d)) == NULL)
409  return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
410  pixCopySpp(pixd, pixs);
411  pixCopyResolution(pixd, pixs);
412  pixCopyColormap(pixd, pixs);
413  pixCopyText(pixd, pixs);
414  pixCopyInputFormat(pixd, pixs);
415  return pixd;
416 }
417 
418 
438 PIX *
439 pixCreateHeader(l_int32 width,
440  l_int32 height,
441  l_int32 depth)
442 {
443 l_int32 wpl;
444 l_uint64 wpl64, bignum;
445 PIX *pixd;
446 
447  PROCNAME("pixCreateHeader");
448 
449  if ((depth != 1) && (depth != 2) && (depth != 4) && (depth != 8)
450  && (depth != 16) && (depth != 24) && (depth != 32))
451  return (PIX *)ERROR_PTR("depth must be {1, 2, 4, 8, 16, 24, 32}",
452  procName, NULL);
453  if (width <= 0)
454  return (PIX *)ERROR_PTR("width must be > 0", procName, NULL);
455  if (height <= 0)
456  return (PIX *)ERROR_PTR("height must be > 0", procName, NULL);
457 
458  /* Avoid overflow in malloc arg, malicious or otherwise */
459  wpl = 0;
460  wpl64 = ((l_uint64)width * (l_uint64)depth + 31) / 32;
461  if (wpl64 > ((1LL << 29) - 1)) {
462  L_ERROR("requested w = %d, h = %d, d = %d\n",
463  procName, width, height, depth);
464  return (PIX *)ERROR_PTR("wpl >= 2^29", procName, NULL);
465  } else {
466  wpl = (l_int32)wpl64;
467  }
468  bignum = 4L * wpl * height; /* number of bytes to be requested */
469  if (bignum > ((1LL << 31) - 1)) {
470  L_ERROR("requested w = %d, h = %d, d = %d\n",
471  procName, width, height, depth);
472  return (PIX *)ERROR_PTR("requested bytes >= 2^31", procName, NULL);
473  }
474 
475  if ((pixd = (PIX *)LEPT_CALLOC(1, sizeof(PIX))) == NULL)
476  return (PIX *)ERROR_PTR("LEPT_CALLOC fail for pixd", procName, NULL);
477  pixSetWidth(pixd, width);
478  pixSetHeight(pixd, height);
479  pixSetDepth(pixd, depth);
480  pixSetWpl(pixd, wpl);
481  if (depth == 24 || depth == 32)
482  pixSetSpp(pixd, 3);
483  else
484  pixSetSpp(pixd, 1);
485 
486  pixd->refcount = 1;
487  pixd->informat = IFF_UNKNOWN;
488  return pixd;
489 }
490 
491 
516 PIX *
517 pixClone(PIX *pixs)
518 {
519  PROCNAME("pixClone");
520 
521  if (!pixs)
522  return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
523  pixChangeRefcount(pixs, 1);
524 
525  return pixs;
526 }
527 
528 
529 /*--------------------------------------------------------------------*
530  * Pix Destruction *
531  *--------------------------------------------------------------------*/
544 void
546 {
547 PIX *pix;
548 
549  PROCNAME("pixDestroy");
550 
551  if (!ppix) {
552  L_WARNING("ptr address is null!\n", procName);
553  return;
554  }
555 
556  if ((pix = *ppix) == NULL)
557  return;
558  pixFree(pix);
559  *ppix = NULL;
560  return;
561 }
562 
563 
575 static void
577 {
578 l_uint32 *data;
579 char *text;
580 
581  if (!pix) return;
582 
583  pixChangeRefcount(pix, -1);
584  if (pixGetRefcount(pix) <= 0) {
585  if ((data = pixGetData(pix)) != NULL)
586  pix_free(data);
587  if ((text = pixGetText(pix)) != NULL)
588  LEPT_FREE(text);
589  pixDestroyColormap(pix);
590  LEPT_FREE(pix);
591  }
592  return;
593 }
594 
595 
596 /*-------------------------------------------------------------------------*
597  * Pix Copy *
598  *-------------------------------------------------------------------------*/
629 PIX *
630 pixCopy(PIX *pixd, /* can be null */
631  PIX *pixs)
632 {
633 l_int32 bytes;
634 l_uint32 *datas, *datad;
635 
636  PROCNAME("pixCopy");
637 
638  if (!pixs)
639  return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
640  if (pixs == pixd)
641  return pixd;
642 
643  /* Total bytes in image data */
644  bytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
645 
646  /* If we're making a new pix ... */
647  if (!pixd) {
648  if ((pixd = pixCreateTemplate(pixs)) == NULL)
649  return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
650  datas = pixGetData(pixs);
651  datad = pixGetData(pixd);
652  memcpy((char *)datad, (char *)datas, bytes);
653  return pixd;
654  }
655 
656  /* Reallocate image data if sizes are different */
657  if (pixResizeImageData(pixd, pixs) == 1)
658  return (PIX *)ERROR_PTR("reallocation of data failed", procName, NULL);
659 
660  /* Copy non-image data fields */
661  pixCopyColormap(pixd, pixs);
662  pixCopySpp(pixd, pixs);
663  pixCopyResolution(pixd, pixs);
664  pixCopyInputFormat(pixd, pixs);
665  pixCopyText(pixd, pixs);
666 
667  /* Copy image data */
668  datas = pixGetData(pixs);
669  datad = pixGetData(pixd);
670  memcpy((char*)datad, (char*)datas, bytes);
671  return pixd;
672 }
673 
674 
689 l_int32
691  PIX *pixs)
692 {
693 l_int32 w, h, d, wpl, bytes;
694 l_uint32 *data;
695 
696  PROCNAME("pixResizeImageData");
697 
698  if (!pixs)
699  return ERROR_INT("pixs not defined", procName, 1);
700  if (!pixd)
701  return ERROR_INT("pixd not defined", procName, 1);
702 
703  if (pixSizesEqual(pixs, pixd)) /* nothing to do */
704  return 0;
705 
706  pixGetDimensions(pixs, &w, &h, &d);
707  wpl = pixGetWpl(pixs);
708  pixSetWidth(pixd, w);
709  pixSetHeight(pixd, h);
710  pixSetDepth(pixd, d);
711  pixSetWpl(pixd, wpl);
712  bytes = 4 * wpl * h;
713  pixFreeData(pixd); /* free any existing image data */
714  if ((data = (l_uint32 *)pix_malloc(bytes)) == NULL)
715  return ERROR_INT("pix_malloc fail for data", procName, 1);
716  pixSetData(pixd, data);
717  pixCopyResolution(pixd, pixs);
718  return 0;
719 }
720 
721 
734 l_int32
736  PIX *pixs)
737 {
738 PIXCMAP *cmaps, *cmapd;
739 
740  PROCNAME("pixCopyColormap");
741 
742  if (!pixs)
743  return ERROR_INT("pixs not defined", procName, 1);
744  if (!pixd)
745  return ERROR_INT("pixd not defined", procName, 1);
746  if (pixs == pixd)
747  return 0; /* no-op */
748 
749  pixDestroyColormap(pixd);
750  if ((cmaps = pixGetColormap(pixs)) == NULL) /* not an error */
751  return 0;
752 
753  if ((cmapd = pixcmapCopy(cmaps)) == NULL)
754  return ERROR_INT("cmapd not made", procName, 1);
755  pixSetColormap(pixd, cmapd);
756 
757  return 0;
758 }
759 
760 
767 l_int32
769  PIX *pix2)
770 {
771  PROCNAME("pixSizesEqual");
772 
773  if (!pix1 || !pix2)
774  return ERROR_INT("pix1 and pix2 not both defined", procName, 0);
775 
776  if (pix1 == pix2)
777  return 1;
778 
779  if ((pixGetWidth(pix1) != pixGetWidth(pix2)) ||
780  (pixGetHeight(pix1) != pixGetHeight(pix2)) ||
781  (pixGetDepth(pix1) != pixGetDepth(pix2)))
782  return 0;
783  else
784  return 1;
785 }
786 
787 
841 l_int32
843  PIX **ppixs,
844  l_int32 copytext,
845  l_int32 copyformat)
846 {
847 l_int32 nbytes;
848 PIX *pixs;
849 
850  PROCNAME("pixTransferAllData");
851 
852  if (!ppixs)
853  return ERROR_INT("&pixs not defined", procName, 1);
854  if ((pixs = *ppixs) == NULL)
855  return ERROR_INT("pixs not defined", procName, 1);
856  if (!pixd)
857  return ERROR_INT("pixd not defined", procName, 1);
858  if (pixs == pixd) /* no-op */
859  return ERROR_INT("pixd == pixs", procName, 1);
860 
861  if (pixGetRefcount(pixs) == 1) { /* transfer the data, cmap, text */
862  pixFreeData(pixd); /* dealloc any existing data */
863  pixSetData(pixd, pixGetData(pixs)); /* transfer new data from pixs */
864  pixs->data = NULL; /* pixs no longer owns data */
865  pixSetColormap(pixd, pixGetColormap(pixs)); /* frees old; sets new */
866  pixs->colormap = NULL; /* pixs no longer owns colormap */
867  if (copytext) {
868  pixSetText(pixd, pixGetText(pixs));
869  pixSetText(pixs, NULL);
870  }
871  } else { /* preserve pixs by making a copy of the data, cmap, text */
872  pixResizeImageData(pixd, pixs);
873  nbytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
874  memcpy((char *)pixGetData(pixd), (char *)pixGetData(pixs), nbytes);
875  pixCopyColormap(pixd, pixs);
876  if (copytext)
877  pixCopyText(pixd, pixs);
878  }
879 
880  pixCopySpp(pixd, pixs);
881  pixCopyResolution(pixd, pixs);
882  pixCopyDimensions(pixd, pixs);
883  if (copyformat)
884  pixCopyInputFormat(pixd, pixs);
885 
886  /* This will destroy pixs if data was transferred;
887  * otherwise, it just decrements its refcount. */
888  pixDestroy(ppixs);
889  return 0;
890 }
891 
892 
931 l_int32
933  PIX **ppixs)
934 {
935  PROCNAME("pixSwapAndDestroy");
936 
937  if (!ppixd)
938  return ERROR_INT("&pixd not defined", procName, 1);
939  if (!ppixs)
940  return ERROR_INT("&pixs not defined", procName, 1);
941  if (*ppixs == NULL)
942  return ERROR_INT("pixs not defined", procName, 1);
943  if (ppixs == ppixd) /* no-op */
944  return ERROR_INT("&pixd == &pixs", procName, 1);
945 
946  pixDestroy(ppixd);
947  *ppixd = pixClone(*ppixs);
948  pixDestroy(ppixs);
949  return 0;
950 }
951 
952 
953 /*--------------------------------------------------------------------*
954  * Accessors *
955  *--------------------------------------------------------------------*/
956 l_int32
957 pixGetWidth(PIX *pix)
958 {
959  PROCNAME("pixGetWidth");
960 
961  if (!pix)
962  return ERROR_INT("pix not defined", procName, 0);
963 
964  return pix->w;
965 }
966 
967 
968 l_int32
969 pixSetWidth(PIX *pix,
970  l_int32 width)
971 {
972  PROCNAME("pixSetWidth");
973 
974  if (!pix)
975  return ERROR_INT("pix not defined", procName, 1);
976  if (width < 0) {
977  pix->w = 0;
978  return ERROR_INT("width must be >= 0", procName, 1);
979  }
980 
981  pix->w = width;
982  return 0;
983 }
984 
985 
986 l_int32
987 pixGetHeight(PIX *pix)
988 {
989  PROCNAME("pixGetHeight");
990 
991  if (!pix)
992  return ERROR_INT("pix not defined", procName, 0);
993 
994  return pix->h;
995 }
996 
997 
998 l_int32
999 pixSetHeight(PIX *pix,
1000  l_int32 height)
1001 {
1002  PROCNAME("pixSetHeight");
1003 
1004  if (!pix)
1005  return ERROR_INT("pix not defined", procName, 1);
1006  if (height < 0) {
1007  pix->h = 0;
1008  return ERROR_INT("h must be >= 0", procName, 1);
1009  }
1010 
1011  pix->h = height;
1012  return 0;
1013 }
1014 
1015 
1016 l_int32
1017 pixGetDepth(PIX *pix)
1018 {
1019  PROCNAME("pixGetDepth");
1020 
1021  if (!pix)
1022  return ERROR_INT("pix not defined", procName, 0);
1023 
1024  return pix->d;
1025 }
1026 
1027 
1028 l_int32
1029 pixSetDepth(PIX *pix,
1030  l_int32 depth)
1031 {
1032  PROCNAME("pixSetDepth");
1033 
1034  if (!pix)
1035  return ERROR_INT("pix not defined", procName, 1);
1036  if (depth < 1)
1037  return ERROR_INT("d must be >= 1", procName, 1);
1038 
1039  pix->d = depth;
1040  return 0;
1041 }
1042 
1043 
1051 l_int32
1053  l_int32 *pw,
1054  l_int32 *ph,
1055  l_int32 *pd)
1056 {
1057  PROCNAME("pixGetDimensions");
1058 
1059  if (pw) *pw = 0;
1060  if (ph) *ph = 0;
1061  if (pd) *pd = 0;
1062  if (!pix)
1063  return ERROR_INT("pix not defined", procName, 1);
1064  if (pw) *pw = pix->w;
1065  if (ph) *ph = pix->h;
1066  if (pd) *pd = pix->d;
1067  return 0;
1068 }
1069 
1070 
1078 l_int32
1080  l_int32 w,
1081  l_int32 h,
1082  l_int32 d)
1083 {
1084  PROCNAME("pixSetDimensions");
1085 
1086  if (!pix)
1087  return ERROR_INT("pix not defined", procName, 1);
1088  if (w > 0) pixSetWidth(pix, w);
1089  if (h > 0) pixSetHeight(pix, h);
1090  if (d > 0) pixSetDepth(pix, d);
1091  return 0;
1092 }
1093 
1094 
1102 l_int32
1104  PIX *pixs)
1105 {
1106  PROCNAME("pixCopyDimensions");
1107 
1108  if (!pixd)
1109  return ERROR_INT("pixd not defined", procName, 1);
1110  if (!pixs)
1111  return ERROR_INT("pixs not defined", procName, 1);
1112  if (pixs == pixd)
1113  return 0; /* no-op */
1114 
1115  pixSetWidth(pixd, pixGetWidth(pixs));
1116  pixSetHeight(pixd, pixGetHeight(pixs));
1117  pixSetDepth(pixd, pixGetDepth(pixs));
1118  pixSetWpl(pixd, pixGetWpl(pixs));
1119  return 0;
1120 }
1121 
1122 
1123 l_int32
1124 pixGetSpp(PIX *pix)
1125 {
1126  PROCNAME("pixGetSpp");
1127 
1128  if (!pix)
1129  return ERROR_INT("pix not defined", procName, 0);
1130 
1131  return pix->spp;
1132 }
1133 
1134 
1135 /*
1136  * pixSetSpp()
1137  * Input: pix
1138  * spp (1, 3 or 4)
1139  * Return: 0 if OK, 1 on error
1140  *
1141  * Notes:
1142  * (1) For a 32 bpp pix, this can be used to ignore the
1143  * alpha sample (spp == 3) or to use it (spp == 4).
1144  * For example, to write a spp == 4 image without the alpha
1145  * sample (as an rgb pix), call pixSetSpp(pix, 3) and
1146  * then write it out as a png.
1147  */
1148 l_int32
1149 pixSetSpp(PIX *pix,
1150  l_int32 spp)
1151 {
1152  PROCNAME("pixSetSpp");
1153 
1154  if (!pix)
1155  return ERROR_INT("pix not defined", procName, 1);
1156  if (spp < 1)
1157  return ERROR_INT("spp must be >= 1", procName, 1);
1158 
1159  pix->spp = spp;
1160  return 0;
1161 }
1162 
1163 
1171 l_int32
1173  PIX *pixs)
1174 {
1175  PROCNAME("pixCopySpp");
1176 
1177  if (!pixd)
1178  return ERROR_INT("pixd not defined", procName, 1);
1179  if (!pixs)
1180  return ERROR_INT("pixs not defined", procName, 1);
1181  if (pixs == pixd)
1182  return 0; /* no-op */
1183 
1184  pixSetSpp(pixd, pixGetSpp(pixs));
1185  return 0;
1186 }
1187 
1188 
1189 l_int32
1190 pixGetWpl(PIX *pix)
1191 {
1192  PROCNAME("pixGetWpl");
1193 
1194  if (!pix)
1195  return ERROR_INT("pix not defined", procName, 0);
1196  return pix->wpl;
1197 }
1198 
1199 
1200 l_int32
1201 pixSetWpl(PIX *pix,
1202  l_int32 wpl)
1203 {
1204  PROCNAME("pixSetWpl");
1205 
1206  if (!pix)
1207  return ERROR_INT("pix not defined", procName, 1);
1208 
1209  pix->wpl = wpl;
1210  return 0;
1211 }
1212 
1213 
1214 l_int32
1215 pixGetRefcount(PIX *pix)
1216 {
1217  PROCNAME("pixGetRefcount");
1218 
1219  if (!pix)
1220  return ERROR_INT("pix not defined", procName, 0);
1221  return pix->refcount;
1222 }
1223 
1224 
1225 l_int32
1226 pixChangeRefcount(PIX *pix,
1227  l_int32 delta)
1228 {
1229  PROCNAME("pixChangeRefcount");
1230 
1231  if (!pix)
1232  return ERROR_INT("pix not defined", procName, 1);
1233 
1234  pix->refcount += delta;
1235  return 0;
1236 }
1237 
1238 
1239 l_int32
1240 pixGetXRes(PIX *pix)
1241 {
1242  PROCNAME("pixGetXRes");
1243 
1244  if (!pix)
1245  return ERROR_INT("pix not defined", procName, 0);
1246  return pix->xres;
1247 }
1248 
1249 
1250 l_int32
1251 pixSetXRes(PIX *pix,
1252  l_int32 res)
1253 {
1254  PROCNAME("pixSetXRes");
1255 
1256  if (!pix)
1257  return ERROR_INT("pix not defined", procName, 1);
1258 
1259  pix->xres = res;
1260  return 0;
1261 }
1262 
1263 
1264 l_int32
1265 pixGetYRes(PIX *pix)
1266 {
1267  PROCNAME("pixGetYRes");
1268 
1269  if (!pix)
1270  return ERROR_INT("pix not defined", procName, 0);
1271  return pix->yres;
1272 }
1273 
1274 
1275 l_int32
1276 pixSetYRes(PIX *pix,
1277  l_int32 res)
1278 {
1279  PROCNAME("pixSetYRes");
1280 
1281  if (!pix)
1282  return ERROR_INT("pix not defined", procName, 1);
1283 
1284  pix->yres = res;
1285  return 0;
1286 }
1287 
1288 
1296 l_int32
1298  l_int32 *pxres,
1299  l_int32 *pyres)
1300 {
1301  PROCNAME("pixGetResolution");
1302 
1303  if (pxres) *pxres = 0;
1304  if (pyres) *pyres = 0;
1305  if (!pxres && !pyres)
1306  return ERROR_INT("no output requested", procName, 1);
1307  if (!pix)
1308  return ERROR_INT("pix not defined", procName, 1);
1309  if (pxres) *pxres = pix->xres;
1310  if (pyres) *pyres = pix->yres;
1311  return 0;
1312 }
1313 
1314 
1322 l_int32
1324  l_int32 xres,
1325  l_int32 yres)
1326 {
1327  PROCNAME("pixSetResolution");
1328 
1329  if (!pix)
1330  return ERROR_INT("pix not defined", procName, 1);
1331  if (xres > 0) pix->xres = xres;
1332  if (yres > 0) pix->yres = yres;
1333  return 0;
1334 }
1335 
1336 
1337 l_int32
1338 pixCopyResolution(PIX *pixd,
1339  PIX *pixs)
1340 {
1341  PROCNAME("pixCopyResolution");
1342 
1343  if (!pixs)
1344  return ERROR_INT("pixs not defined", procName, 1);
1345  if (!pixd)
1346  return ERROR_INT("pixd not defined", procName, 1);
1347  if (pixs == pixd)
1348  return 0; /* no-op */
1349 
1350  pixSetXRes(pixd, pixGetXRes(pixs));
1351  pixSetYRes(pixd, pixGetYRes(pixs));
1352  return 0;
1353 }
1354 
1355 
1356 l_int32
1357 pixScaleResolution(PIX *pix,
1358  l_float32 xscale,
1359  l_float32 yscale)
1360 {
1361  PROCNAME("pixScaleResolution");
1362 
1363  if (!pix)
1364  return ERROR_INT("pix not defined", procName, 1);
1365 
1366  if (pix->xres != 0 && pix->yres != 0) {
1367  pix->xres = (l_uint32)(xscale * (l_float32)(pix->xres) + 0.5);
1368  pix->yres = (l_uint32)(yscale * (l_float32)(pix->yres) + 0.5);
1369  }
1370  return 0;
1371 }
1372 
1373 
1374 l_int32
1375 pixGetInputFormat(PIX *pix)
1376 {
1377  PROCNAME("pixGetInputFormat");
1378 
1379  if (!pix)
1380  return ERROR_INT("pix not defined", procName, 0);
1381  return pix->informat;
1382 }
1383 
1384 
1385 l_int32
1386 pixSetInputFormat(PIX *pix,
1387  l_int32 informat)
1388 {
1389  PROCNAME("pixSetInputFormat");
1390 
1391  if (!pix)
1392  return ERROR_INT("pix not defined", procName, 1);
1393  pix->informat = informat;
1394  return 0;
1395 }
1396 
1397 
1398 l_int32
1399 pixCopyInputFormat(PIX *pixd,
1400  PIX *pixs)
1401 {
1402  PROCNAME("pixCopyInputFormat");
1403 
1404  if (!pixs)
1405  return ERROR_INT("pixs not defined", procName, 1);
1406  if (!pixd)
1407  return ERROR_INT("pixd not defined", procName, 1);
1408  if (pixs == pixd)
1409  return 0; /* no-op */
1410 
1411  pixSetInputFormat(pixd, pixGetInputFormat(pixs));
1412  return 0;
1413 }
1414 
1415 
1416 l_int32
1417 pixSetSpecial(PIX *pix,
1418  l_int32 special)
1419 {
1420  PROCNAME("pixSetSpecial");
1421 
1422  if (!pix)
1423  return ERROR_INT("pix not defined", procName, 1);
1424  pix->special = special;
1425  return 0;
1426 }
1427 
1428 
1441 char *
1443 {
1444  PROCNAME("pixGetText");
1445 
1446  if (!pix)
1447  return (char *)ERROR_PTR("pix not defined", procName, NULL);
1448  return pix->text;
1449 }
1450 
1451 
1465 l_int32
1467  const char *textstring)
1468 {
1469  PROCNAME("pixSetText");
1470 
1471  if (!pix)
1472  return ERROR_INT("pix not defined", procName, 1);
1473 
1474  stringReplace(&pix->text, textstring);
1475  return 0;
1476 }
1477 
1478 
1493 l_int32
1495  const char *textstring)
1496 {
1497 char *newstring;
1498 
1499  PROCNAME("pixAddText");
1500 
1501  if (!pix)
1502  return ERROR_INT("pix not defined", procName, 1);
1503 
1504  newstring = stringJoin(pixGetText(pix), textstring);
1505  stringReplace(&pix->text, newstring);
1506  LEPT_FREE(newstring);
1507  return 0;
1508 }
1509 
1510 
1511 l_int32
1512 pixCopyText(PIX *pixd,
1513  PIX *pixs)
1514 {
1515  PROCNAME("pixCopyText");
1516 
1517  if (!pixs)
1518  return ERROR_INT("pixs not defined", procName, 1);
1519  if (!pixd)
1520  return ERROR_INT("pixd not defined", procName, 1);
1521  if (pixs == pixd)
1522  return 0; /* no-op */
1523 
1524  pixSetText(pixd, pixGetText(pixs));
1525  return 0;
1526 }
1527 
1528 
1529 PIXCMAP *
1530 pixGetColormap(PIX *pix)
1531 {
1532  PROCNAME("pixGetColormap");
1533 
1534  if (!pix)
1535  return (PIXCMAP *)ERROR_PTR("pix not defined", procName, NULL);
1536  return pix->colormap;
1537 }
1538 
1539 
1555 l_int32
1557  PIXCMAP *colormap)
1558 {
1559  PROCNAME("pixSetColormap");
1560 
1561  if (!pix)
1562  return ERROR_INT("pix not defined", procName, 1);
1563 
1564  pixDestroyColormap(pix);
1565  pix->colormap = colormap;
1566  return 0;
1567 }
1568 
1569 
1576 l_int32
1578 {
1579 PIXCMAP *cmap;
1580 
1581  PROCNAME("pixDestroyColormap");
1582 
1583  if (!pix)
1584  return ERROR_INT("pix not defined", procName, 1);
1585 
1586  if ((cmap = pix->colormap) != NULL) {
1587  pixcmapDestroy(&cmap);
1588  pix->colormap = NULL;
1589  }
1590  return 0;
1591 }
1592 
1593 
1601 l_uint32 *
1603 {
1604  PROCNAME("pixGetData");
1605 
1606  if (!pix)
1607  return (l_uint32 *)ERROR_PTR("pix not defined", procName, NULL);
1608  return pix->data;
1609 }
1610 
1611 
1619 l_int32
1621  l_uint32 *data)
1622 {
1623  PROCNAME("pixSetData");
1624 
1625  if (!pix)
1626  return ERROR_INT("pix not defined", procName, 1);
1627 
1628  pix->data = data;
1629  return 0;
1630 }
1631 
1632 
1644 l_uint32 *
1646 {
1647 l_int32 count, bytes;
1648 l_uint32 *data, *datas;
1649 
1650  PROCNAME("pixExtractData");
1651 
1652  if (!pixs)
1653  return (l_uint32 *)ERROR_PTR("pixs not defined", procName, NULL);
1654 
1655  count = pixGetRefcount(pixs);
1656  if (count == 1) { /* extract */
1657  data = pixGetData(pixs);
1658  pixSetData(pixs, NULL);
1659  } else { /* refcount > 1; copy */
1660  bytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
1661  datas = pixGetData(pixs);
1662  if ((data = (l_uint32 *)pix_malloc(bytes)) == NULL)
1663  return (l_uint32 *)ERROR_PTR("data not made", procName, NULL);
1664  memcpy((char *)data, (char *)datas, bytes);
1665  }
1666 
1667  return data;
1668 }
1669 
1670 
1680 l_int32
1682 {
1683 l_uint32 *data;
1684 
1685  PROCNAME("pixFreeData");
1686 
1687  if (!pix)
1688  return ERROR_INT("pix not defined", procName, 1);
1689 
1690  if ((data = pixGetData(pix)) != NULL) {
1691  pix_free(data);
1692  pix->data = NULL;
1693  }
1694  return 0;
1695 }
1696 
1697 
1698 /*--------------------------------------------------------------------*
1699  * Pix line ptrs *
1700  *--------------------------------------------------------------------*/
1771 void **
1773  l_int32 *psize)
1774 {
1775 l_int32 i, h, wpl;
1776 l_uint32 *data;
1777 void **lines;
1778 
1779  PROCNAME("pixGetLinePtrs");
1780 
1781  if (psize) *psize = 0;
1782  if (!pix)
1783  return (void **)ERROR_PTR("pix not defined", procName, NULL);
1784 
1785  h = pixGetHeight(pix);
1786  if (psize) *psize = h;
1787  if ((lines = (void **)LEPT_CALLOC(h, sizeof(void *))) == NULL)
1788  return (void **)ERROR_PTR("lines not made", procName, NULL);
1789  wpl = pixGetWpl(pix);
1790  data = pixGetData(pix);
1791  for (i = 0; i < h; i++)
1792  lines[i] = (void *)(data + i * wpl);
1793 
1794  return lines;
1795 }
1796 
1797 
1798 /*--------------------------------------------------------------------*
1799  * Print output for debugging *
1800  *--------------------------------------------------------------------*/
1801 extern const char *ImageFileFormatExtensions[];
1802 
1811 l_int32
1813  PIX *pix,
1814  const char *text)
1815 {
1816 char *textdata;
1817 l_int32 informat;
1818 PIXCMAP *cmap;
1819 
1820  PROCNAME("pixPrintStreamInfo");
1821 
1822  if (!fp)
1823  return ERROR_INT("fp not defined", procName, 1);
1824  if (!pix)
1825  return ERROR_INT("pix not defined", procName, 1);
1826 
1827  if (text)
1828  fprintf(fp, " Pix Info for %s:\n", text);
1829  fprintf(fp, " width = %d, height = %d, depth = %d, spp = %d\n",
1830  pixGetWidth(pix), pixGetHeight(pix), pixGetDepth(pix),
1831  pixGetSpp(pix));
1832  fprintf(fp, " wpl = %d, data = %p, refcount = %d\n",
1833  pixGetWpl(pix), pixGetData(pix), pixGetRefcount(pix));
1834  fprintf(fp, " xres = %d, yres = %d\n", pixGetXRes(pix), pixGetYRes(pix));
1835  if ((cmap = pixGetColormap(pix)) != NULL)
1836  pixcmapWriteStream(fp, cmap);
1837  else
1838  fprintf(fp, " no colormap\n");
1839  informat = pixGetInputFormat(pix);
1840  fprintf(fp, " input format: %d (%s)\n", informat,
1841  ImageFileFormatExtensions[informat]);
1842  if ((textdata = pixGetText(pix)) != NULL)
1843  fprintf(fp, " text: %s\n", textdata);
1844 
1845  return 0;
1846 }
l_int32 pixCopyDimensions(PIX *pixd, PIX *pixs)
pixCopyDimensions()
Definition: pix1.c:1103
l_int32 pixCopyColormap(PIX *pixd, PIX *pixs)
pixCopyColormap()
Definition: pix1.c:735
l_int32 xres
Definition: pix.h:142
l_uint32 * data
Definition: pix.h:150
l_uint32 w
Definition: pix.h:136
l_int32 pixFreeData(PIX *pix)
pixFreeData()
Definition: pix1.c:1681
l_int32 stringReplace(char **pdest, const char *src)
stringReplace()
Definition: utils2.c:279
l_int32 special
Definition: pix.h:147
l_int32 informat
Definition: pix.h:146
l_int32 pixResizeImageData(PIX *pixd, PIX *pixs)
pixResizeImageData()
Definition: pix1.c:690
l_int32 pixSetData(PIX *pix, l_uint32 *data)
pixSetData()
Definition: pix1.c:1620
l_uint32 refcount
Definition: pix.h:141
l_int32 yres
Definition: pix.h:144
struct PixColormap * colormap
Definition: pix.h:149
void ** pixGetLinePtrs(PIX *pix, l_int32 *psize)
pixGetLinePtrs()
Definition: pix1.c:1772
void(* dealloc_fn)(void *)
Definition: pix.h:1311
l_uint32 * pixExtractData(PIX *pixs)
pixExtractData()
Definition: pix1.c:1645
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
Definition: pix1.c:302
void pixcmapDestroy(PIXCMAP **pcmap)
pixcmapDestroy()
Definition: colormap.c:263
l_uint32 * pixGetData(PIX *pix)
pixGetData()
Definition: pix1.c:1602
l_int32 pixPrintStreamInfo(FILE *fp, PIX *pix, const char *text)
pixPrintStreamInfo()
Definition: pix1.c:1812
PIX * pixCreateTemplate(PIX *pixs)
pixCreateTemplate()
Definition: pix1.c:367
PIX * pixCreateTemplateNoInit(PIX *pixs)
pixCreateTemplateNoInit()
Definition: pix1.c:397
static struct PixMemoryManager pix_mem_manager
Definition: pix1.c:225
l_int32 pixDestroyColormap(PIX *pix)
pixDestroyColormap()
Definition: pix1.c:1577
l_int32 pixSwapAndDestroy(PIX **ppixd, PIX **ppixs)
pixSwapAndDestroy()
Definition: pix1.c:932
static void pixFree(PIX *pix)
pixFree()
Definition: pix1.c:576
l_int32 pixcmapWriteStream(FILE *fp, PIXCMAP *cmap)
pixcmapWriteStream()
Definition: colormap.c:1715
l_uint32 d
Definition: pix.h:138
l_int32 pixCopySpp(PIX *pixd, PIX *pixs)
pixCopySpp()
Definition: pix1.c:1172
l_uint32 h
Definition: pix.h:137
char * text
Definition: pix.h:148
l_int32 pixAddText(PIX *pix, const char *textstring)
pixAddText()
Definition: pix1.c:1494
l_int32 pixSetPadBits(PIX *pix, l_int32 val)
pixSetPadBits()
Definition: pix2.c:1295
PIX * pixCreateHeader(l_int32 width, l_int32 height, l_int32 depth)
pixCreateHeader()
Definition: pix1.c:439
l_int32 pixSetColormap(PIX *pix, PIXCMAP *colormap)
pixSetColormap()
Definition: pix1.c:1556
l_int32 pixSizesEqual(PIX *pix1, PIX *pix2)
pixSizesEqual()
Definition: pix1.c:768
l_int32 pixTransferAllData(PIX *pixd, PIX **ppixs, l_int32 copytext, l_int32 copyformat)
pixTransferAllData()
Definition: pix1.c:842
PIX * pixCreateNoInit(l_int32 width, l_int32 height, l_int32 depth)
pixCreateNoInit()
Definition: pix1.c:331
PIX * pixClone(PIX *pixs)
pixClone()
Definition: pix1.c:517
l_int32 pixGetResolution(PIX *pix, l_int32 *pxres, l_int32 *pyres)
pixGetResolution()
Definition: pix1.c:1297
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:545
l_int32 pixSetResolution(PIX *pix, l_int32 xres, l_int32 yres)
pixSetResolution()
Definition: pix1.c:1323
char * pixGetText(PIX *pix)
pixGetText()
Definition: pix1.c:1442
char * stringJoin(const char *src1, const char *src2)
stringJoin()
Definition: utils2.c:451
Definition: pix.h:134
void *(* alloc_fn)(size_t)
Definition: pix.h:1308
PIX * pixCopy(PIX *pixd, PIX *pixs)
pixCopy()
Definition: pix1.c:630
l_uint32 wpl
Definition: pix.h:140
PIXCMAP * pixcmapCopy(PIXCMAP *cmaps)
pixcmapCopy()
Definition: colormap.c:233
l_int32 pixGetDimensions(PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1052
l_int32 pixSetDimensions(PIX *pix, l_int32 w, l_int32 h, l_int32 d)
pixSetDimensions()
Definition: pix1.c:1079
void setPixMemoryManager(alloc_fn allocator, dealloc_fn deallocator)
setPixMemoryManager()
Definition: pix1.c:282
l_uint32 spp
Definition: pix.h:139
l_int32 pixSetText(PIX *pix, const char *textstring)
pixSetText()
Definition: pix1.c:1466