/* * The Npic library * * Copyright (C) 2003 Edouard Thiel * * This library is free software under the terms of the * GNU Lesser General Public License (LGPL) version 2.1. */ /* DO NOT EDIT !!! Generated by npic-templa from "mask_creat.ct" */ /* * mask_creat.c - 16/12/2008 * * Masks for distance transforms, medial axis, convolutions, etc. */ #include /*--------------------- P U B L I C - I N T E R F A C E ----------------------*/ /* * Creation of a distance mask. * Allocate space for a number (tot) of vector, set the number * of used vectors (nb) to 0. * Return the mask on success, else return NULL. Verbose. * * USAGE : * Npic_mask *m; * m = NpicCreateMask (NPIC_MASK_XX); * if (m == NULL) ... * .... * NpicDestroyMask (m); */ Npic_mask *NpicCreateMask (int ntype) { Npic_mask *m; int size, tot, step; switch (ntype) { case NPIC_MASK_2L : size = sizeof(Npic_weighting_2l); tot = 4; step = 32; break; case NPIC_MASK_2D : size = sizeof(Npic_weighting_2d); tot = 4; step = 32; break; case NPIC_MASK_3L : size = sizeof(Npic_weighting_3l); tot = 13; step = 48; break; case NPIC_MASK_3D : size = sizeof(Npic_weighting_3d); tot = 13; step = 48; break; case NPIC_MASK_4L : size = sizeof(Npic_weighting_4l); tot = 40; step = 96; break; case NPIC_MASK_4D : size = sizeof(Npic_weighting_4d); tot = 40; step = 96; break; case NPIC_MASK_5L : size = sizeof(Npic_weighting_5l); tot = 121; step = 192; break; case NPIC_MASK_5D : size = sizeof(Npic_weighting_5d); tot = 121; step = 192; break; case NPIC_MASK_6L : size = sizeof(Npic_weighting_6l); tot = 364; step = 384; break; case NPIC_MASK_6D : size = sizeof(Npic_weighting_6d); tot = 364; step = 384; break; default : NpicError (__func__, NPIC_ERR_UNEX_TYPE, ""); return NULL; } m = malloc (sizeof(Npic_mask)); if (m == NULL) { NpicError (__func__, NPIC_ERR_MALLOC, ""); return NULL; } memset (m, 0, sizeof(Npic_mask)); m->type = ntype; m->gen.ok = NPIC_SUCCESS; m->gen.size = size; m->gen.nb = 0; m->gen.tot = tot; m->gen.step = step; NpicInitProps (&m->gen.props); m->gen.v = malloc (m->gen.size * m->gen.tot); if (m->gen.v == NULL) { NpicError (__func__, NPIC_ERR_MALLOC, ""); free(m); return NULL; } return m; } /* * Creation of a distance mask of dimension dim in pixel type pixeltype. * Allocate space for a number (tot) of vector, set the number * of used vectors (nb) to 0. * Return the mask on success, else return NULL. Verbose. */ Npic_mask *NpicCreateMaskDP (int dim, int pixeltype) { return NpicCreateMask (NpicMaskNtype (dim, pixeltype)); } /* * Free memory of a mask (even if it is not ok). Silent. */ void NpicDestroyMask (Npic_mask *m) { if (m == NULL) return ; if (m->type < NPIC_MASK_BB || m->type > NPIC_MASK_EE) return; NpicFreeProps (&m->gen.props); if (m->gen.v) free (m->gen.v); memset (m, 0, sizeof(Npic_mask)); free(m); } /* * Copy a mask src to a mask dest. Do nothing if src is not ok. * Do not copy text properties; for that, use NpicMaskCopyProps. * Return the mask on success, else return NULL. Verbose. */ Npic_mask *NpicCopyMask (Npic_mask *src) { Npic_mask *dest; if (NpicMaskIsOK (src, __func__) != NPIC_SUCCESS) return NULL; dest = malloc (sizeof(Npic_mask)); if (dest == NULL) { NpicError (__func__, NPIC_ERR_MALLOC, ""); return NULL; } memcpy (dest, src, sizeof (Npic_mask)); NpicInitProps (&dest->gen.props); dest->gen.v = malloc (src->gen.size * src->gen.tot); if (dest->gen.v == NULL) { NpicError (__func__, NPIC_ERR_MALLOC, ""); free (dest); return NULL; } memcpy (dest->gen.v, src->gen.v, src->gen.size * src->gen.tot); return dest; } /* * Insert a vector in the mask, and expand the list if necessary. * Do nothing if m is not ok. set not ok on error. Verbose. */ void NpicMaskInsert_2l (Npic_mask *m, int y, int x, Npic_l h) { Npic_mask_2l *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } void NpicMaskInsert_2d (Npic_mask *m, int y, int x, Npic_d h) { Npic_mask_2d *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } void NpicMaskInsert_3l (Npic_mask *m, int z, int y, int x, Npic_l h) { Npic_mask_3l *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].z = z; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } void NpicMaskInsert_3d (Npic_mask *m, int z, int y, int x, Npic_d h) { Npic_mask_3d *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].z = z; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } void NpicMaskInsert_4l (Npic_mask *m, int t, int z, int y, int x, Npic_l h) { Npic_mask_4l *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].t = t; dm->v[ dm->nb ].z = z; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } void NpicMaskInsert_4d (Npic_mask *m, int t, int z, int y, int x, Npic_d h) { Npic_mask_4d *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].t = t; dm->v[ dm->nb ].z = z; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } void NpicMaskInsert_5l (Npic_mask *m, int s, int t, int z, int y, int x, Npic_l h) { Npic_mask_5l *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].s = s; dm->v[ dm->nb ].t = t; dm->v[ dm->nb ].z = z; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } void NpicMaskInsert_5d (Npic_mask *m, int s, int t, int z, int y, int x, Npic_d h) { Npic_mask_5d *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].s = s; dm->v[ dm->nb ].t = t; dm->v[ dm->nb ].z = z; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } void NpicMaskInsert_6l (Npic_mask *m, int r, int s, int t, int z, int y, int x, Npic_l h) { Npic_mask_6l *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].r = r; dm->v[ dm->nb ].s = s; dm->v[ dm->nb ].t = t; dm->v[ dm->nb ].z = z; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } void NpicMaskInsert_6d (Npic_mask *m, int r, int s, int t, int z, int y, int x, Npic_d h) { Npic_mask_6d *dm = NpicCastMask (m); if (NpicExpandMask (m, __func__) != NPIC_SUCCESS) return; dm->v[ dm->nb ].r = r; dm->v[ dm->nb ].s = s; dm->v[ dm->nb ].t = t; dm->v[ dm->nb ].z = z; dm->v[ dm->nb ].y = y; dm->v[ dm->nb ].x = x; dm->v[ dm->nb ].h = h; dm->nb++; } /* * Returns the mask which is inside the Npic_mask structure (even if not ok). * Silent. See comments of NpicCastImage in image_creat.c */ void *NpicCastMask (Npic_mask *m) { return m; } /* * Print a mask to stdout. Do not print if not ok. Verbose. */ void NpicPrintMask (Npic_mask *m) { int i; if (NpicMaskIsOK (m, __func__) != NPIC_SUCCESS) return; printf ("Mask type: %s\n", NpicMaskTypeName (m->type)); printf ("Mask properties: %d\n", m->gen.props.nb); for (i = 0; i < m->gen.props.nb; i++) printf (" %s: %s\n", m->gen.props.list[i].key, m->gen.props.list[i].val); printf ("Mask cardinal: %d\n", m->dm_2l.nb); switch (m->type) { case NPIC_MASK_2L: { Npic_mask_2l *dm = NpicCastMask (m); printf ("Mask vectors: i ( y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d ; %5" NPIC_PL " )\n", i, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } case NPIC_MASK_2D: { Npic_mask_2d *dm = NpicCastMask (m); printf ("Mask vectors: i ( y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d ; %lf )\n", i, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } case NPIC_MASK_3L: { Npic_mask_3l *dm = NpicCastMask (m); printf ("Mask vectors: i ( z , y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d , %5d ; %5" NPIC_PL " )\n", i, dm->v[i].z, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } case NPIC_MASK_3D: { Npic_mask_3d *dm = NpicCastMask (m); printf ("Mask vectors: i ( z , y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d , %5d ; %lf )\n", i, dm->v[i].z, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } case NPIC_MASK_4L: { Npic_mask_4l *dm = NpicCastMask (m); printf ("Mask vectors: i ( t , z , y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d , %5d , %5d ; %5" NPIC_PL " )\n", i, dm->v[i].t, dm->v[i].z, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } case NPIC_MASK_4D: { Npic_mask_4d *dm = NpicCastMask (m); printf ("Mask vectors: i ( t , z , y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d , %5d , %5d ; %lf )\n", i, dm->v[i].t, dm->v[i].z, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } case NPIC_MASK_5L: { Npic_mask_5l *dm = NpicCastMask (m); printf ("Mask vectors: i ( s , t , z , y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d , %5d , %5d , %5d ; %5" NPIC_PL " )\n", i, dm->v[i].s, dm->v[i].t, dm->v[i].z, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } case NPIC_MASK_5D: { Npic_mask_5d *dm = NpicCastMask (m); printf ("Mask vectors: i ( s , t , z , y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d , %5d , %5d , %5d ; %lf )\n", i, dm->v[i].s, dm->v[i].t, dm->v[i].z, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } case NPIC_MASK_6L: { Npic_mask_6l *dm = NpicCastMask (m); printf ("Mask vectors: i ( r , s , t , z , y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d , %5d , %5d , %5d , %5d ; %5" NPIC_PL " )\n", i, dm->v[i].r, dm->v[i].s, dm->v[i].t, dm->v[i].z, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } case NPIC_MASK_6D: { Npic_mask_6d *dm = NpicCastMask (m); printf ("Mask vectors: i ( r , s , t , z , y , x ; h )\n"); for (i = 0; i < dm->nb; i++) printf ("%5d ( %5d , %5d , %5d , %5d , %5d , %5d ; %lf )\n", i, dm->v[i].r, dm->v[i].s, dm->v[i].t, dm->v[i].z, dm->v[i].y, dm->v[i].x, dm->v[i].h); printf ("\n"); break; } default: NpicError (__func__, NPIC_ERR_UNEX_TYPE, ""); } } /*-------------------- P R I V A T E - F U N C T I O N S ---------------------*/ /* * Check if there is sufficient place to insert one vector in m, * else expand list using realloc. * Do nothing if m is not ok. set not ok on error. * Return NPIC_SUCCESS, else error code. Verbose. */ int NpicExpandMask (Npic_mask *m, const char *funcname) { void *tmp; int tot, err; err = NpicMaskIsOK (m, funcname); if (err != NPIC_SUCCESS) return err; if (m->gen.v == NULL) return m->gen.ok = NpicError (funcname, NPIC_ERR_NULL_PTR, ""); /* Is there at least 1 empty place in v ? */ if (m->gen.nb < m->gen.tot) return NPIC_SUCCESS; /* Try to expand v with realloc */ tot = m->gen.tot + m->gen.step; tmp = realloc (m->gen.v, tot * m->gen.size); if (tmp == NULL) return m->gen.ok = NpicError (funcname, NPIC_ERR_REALLOC, ""); m->gen.v = tmp; m->gen.tot = tot; return NPIC_SUCCESS; } /*----------------------------------------------------------------------------*/