/* * 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 "image_creat.ct" */ /* * image_creat.c - 16/12/2008 * * Manage memory in struct Npic_image */ #include /*--------------------- P U B L I C - I N T E R F A C E ----------------------*/ /* * Full creation of an image. * All pixels are set to 0, including border pixels. * * Return image on success, else return NULL. Verbose. * * USAGE : * Npic_image *np; * np = NpicCreateImage_xx (ymax, xmax, ...); * if (np == NULL) ... * .... * NpicDestroyImage (np); */ Npic_image *NpicCreateImage_2c (int ymax, int xmax, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_2C, sizeof(Npic_c), 2, 0, 0, 0, 0, ymax, xmax, 0, 0, 0, 0, ybor, xbor); } Npic_image *NpicCreateImage_2l (int ymax, int xmax, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_2L, sizeof(Npic_l), 2, 0, 0, 0, 0, ymax, xmax, 0, 0, 0, 0, ybor, xbor); } Npic_image *NpicCreateImage_2d (int ymax, int xmax, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_2D, sizeof(Npic_d), 2, 0, 0, 0, 0, ymax, xmax, 0, 0, 0, 0, ybor, xbor); } Npic_image *NpicCreateImage_2q (int ymax, int xmax, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_2Q, sizeof(Npic_q), 2, 0, 0, 0, 0, ymax, xmax, 0, 0, 0, 0, ybor, xbor); } Npic_image *NpicCreateImage_3c (int zmax, int ymax, int xmax, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_3C, sizeof(Npic_c), 3, 0, 0, 0, zmax, ymax, xmax, 0, 0, 0, zbor, ybor, xbor); } Npic_image *NpicCreateImage_3l (int zmax, int ymax, int xmax, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_3L, sizeof(Npic_l), 3, 0, 0, 0, zmax, ymax, xmax, 0, 0, 0, zbor, ybor, xbor); } Npic_image *NpicCreateImage_3d (int zmax, int ymax, int xmax, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_3D, sizeof(Npic_d), 3, 0, 0, 0, zmax, ymax, xmax, 0, 0, 0, zbor, ybor, xbor); } Npic_image *NpicCreateImage_3q (int zmax, int ymax, int xmax, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_3Q, sizeof(Npic_q), 3, 0, 0, 0, zmax, ymax, xmax, 0, 0, 0, zbor, ybor, xbor); } Npic_image *NpicCreateImage_4c (int tmax, int zmax, int ymax, int xmax, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_4C, sizeof(Npic_c), 4, 0, 0, tmax, zmax, ymax, xmax, 0, 0, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_4l (int tmax, int zmax, int ymax, int xmax, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_4L, sizeof(Npic_l), 4, 0, 0, tmax, zmax, ymax, xmax, 0, 0, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_4d (int tmax, int zmax, int ymax, int xmax, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_4D, sizeof(Npic_d), 4, 0, 0, tmax, zmax, ymax, xmax, 0, 0, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_4q (int tmax, int zmax, int ymax, int xmax, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_4Q, sizeof(Npic_q), 4, 0, 0, tmax, zmax, ymax, xmax, 0, 0, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_5c (int smax, int tmax, int zmax, int ymax, int xmax, int sbor, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_5C, sizeof(Npic_c), 5, 0, smax, tmax, zmax, ymax, xmax, 0, sbor, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_5l (int smax, int tmax, int zmax, int ymax, int xmax, int sbor, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_5L, sizeof(Npic_l), 5, 0, smax, tmax, zmax, ymax, xmax, 0, sbor, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_5d (int smax, int tmax, int zmax, int ymax, int xmax, int sbor, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_5D, sizeof(Npic_d), 5, 0, smax, tmax, zmax, ymax, xmax, 0, sbor, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_5q (int smax, int tmax, int zmax, int ymax, int xmax, int sbor, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_5Q, sizeof(Npic_q), 5, 0, smax, tmax, zmax, ymax, xmax, 0, sbor, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_6c (int rmax, int smax, int tmax, int zmax, int ymax, int xmax, int rbor, int sbor, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_6C, sizeof(Npic_c), 6, rmax, smax, tmax, zmax, ymax, xmax, rbor, sbor, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_6l (int rmax, int smax, int tmax, int zmax, int ymax, int xmax, int rbor, int sbor, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_6L, sizeof(Npic_l), 6, rmax, smax, tmax, zmax, ymax, xmax, rbor, sbor, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_6d (int rmax, int smax, int tmax, int zmax, int ymax, int xmax, int rbor, int sbor, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_6D, sizeof(Npic_d), 6, rmax, smax, tmax, zmax, ymax, xmax, rbor, sbor, tbor, zbor, ybor, xbor); } Npic_image *NpicCreateImage_6q (int rmax, int smax, int tmax, int zmax, int ymax, int xmax, int rbor, int sbor, int tbor, int zbor, int ybor, int xbor) { return NpicCreateImages (__func__, NPIC_IMAGE_6Q, sizeof(Npic_q), 6, rmax, smax, tmax, zmax, ymax, xmax, rbor, sbor, tbor, zbor, ybor, xbor); } /* * Creation of an image without borders, from image ntype and size. * All pixels are set to 0. * * Return image on success, else return NULL. Verbose. * * USAGE : * Npic_image *np; * Npic_vec vs; vs.x = 100; vs.y = 200; * * np = NpicCreateImageNS (NPIC_IMAGE_2C, &vs); * if (np == NULL) ... * .... * NpicDestroyImage (np); */ Npic_image *NpicCreateImageNS (int ntype, Npic_vec *size) { if (size == NULL) { NpicError (__func__, NPIC_ERR_BAD_SIZE, ""); return NULL; } switch (ntype) { case NPIC_IMAGE_2C : return NpicCreateImage_2c ( size->y, size->x, 0, 0); case NPIC_IMAGE_2L : return NpicCreateImage_2l ( size->y, size->x, 0, 0); case NPIC_IMAGE_2D : return NpicCreateImage_2d ( size->y, size->x, 0, 0); case NPIC_IMAGE_2Q : return NpicCreateImage_2q ( size->y, size->x, 0, 0); case NPIC_IMAGE_3C : return NpicCreateImage_3c ( size->z, size->y, size->x, 0, 0, 0); case NPIC_IMAGE_3L : return NpicCreateImage_3l ( size->z, size->y, size->x, 0, 0, 0); case NPIC_IMAGE_3D : return NpicCreateImage_3d ( size->z, size->y, size->x, 0, 0, 0); case NPIC_IMAGE_3Q : return NpicCreateImage_3q ( size->z, size->y, size->x, 0, 0, 0); case NPIC_IMAGE_4C : return NpicCreateImage_4c ( size->t, size->z, size->y, size->x, 0, 0, 0, 0); case NPIC_IMAGE_4L : return NpicCreateImage_4l ( size->t, size->z, size->y, size->x, 0, 0, 0, 0); case NPIC_IMAGE_4D : return NpicCreateImage_4d ( size->t, size->z, size->y, size->x, 0, 0, 0, 0); case NPIC_IMAGE_4Q : return NpicCreateImage_4q ( size->t, size->z, size->y, size->x, 0, 0, 0, 0); case NPIC_IMAGE_5C : return NpicCreateImage_5c ( size->s, size->t, size->z, size->y, size->x, 0, 0, 0, 0, 0); case NPIC_IMAGE_5L : return NpicCreateImage_5l ( size->s, size->t, size->z, size->y, size->x, 0, 0, 0, 0, 0); case NPIC_IMAGE_5D : return NpicCreateImage_5d ( size->s, size->t, size->z, size->y, size->x, 0, 0, 0, 0, 0); case NPIC_IMAGE_5Q : return NpicCreateImage_5q ( size->s, size->t, size->z, size->y, size->x, 0, 0, 0, 0, 0); case NPIC_IMAGE_6C : return NpicCreateImage_6c ( size->r, size->s, size->t, size->z, size->y, size->x, 0, 0, 0, 0, 0, 0); case NPIC_IMAGE_6L : return NpicCreateImage_6l ( size->r, size->s, size->t, size->z, size->y, size->x, 0, 0, 0, 0, 0, 0); case NPIC_IMAGE_6D : return NpicCreateImage_6d ( size->r, size->s, size->t, size->z, size->y, size->x, 0, 0, 0, 0, 0, 0); case NPIC_IMAGE_6Q : return NpicCreateImage_6q ( size->r, size->s, size->t, size->z, size->y, size->x, 0, 0, 0, 0, 0, 0); } NpicError (__func__, NPIC_ERR_UNEX_NPIC, ""); return NULL; } /* * Creation of an image without borders, from dimension, pixel type, and size. * All pixels are set to 0. * * Return image on success, else return NULL. Verbose. * * USAGE : * Npic_image *np; * Npic_vec vs; vs.x = 100; vs.y = 200; * * np = NpicCreateImageDPS (2, NPIC_C, &vs); * if (np == NULL) ... * .... * NpicDestroyImage (np); */ Npic_image *NpicCreateImageDPS (int dim, int pixeltype, Npic_vec *size) { return NpicCreateImageNS (NpicImageNtype (dim, pixeltype), size); } /* * Create an image dest of same type and size than src, with other border sizes. * All pixels of dest are set to 0, including border pixels. * rbor, sbor, tbor or zbor are ignored depending on dimension. * Do nothing if src is not ok. * Return dest on success, else return NULL. Verbose. */ Npic_image *NpicDupImageB (Npic_image *src, int tbor, int zbor, int ybor, int xbor) { return NpicDupImageB6 (src, 0, 0, tbor, zbor, ybor, xbor); } Npic_image *NpicDupImageB6 (Npic_image *src, int rbor, int sbor, int tbor, int zbor, int ybor, int xbor) { if (NpicImageIsOK (src, __func__) != NPIC_SUCCESS) return NULL; if (rbor < 0 || sbor < 0 || tbor < 0 || zbor < 0 || ybor < 0 || xbor < 0) { NpicError (__func__, NPIC_ERR_BAD_SIZE, ""); return NULL; } switch (src->type) { case NPIC_IMAGE_2C : return NpicCreateImage_2c ( src->gen.ymax, src->gen.xmax, ybor, xbor); break; case NPIC_IMAGE_2L : return NpicCreateImage_2l ( src->gen.ymax, src->gen.xmax, ybor, xbor); break; case NPIC_IMAGE_2D : return NpicCreateImage_2d ( src->gen.ymax, src->gen.xmax, ybor, xbor); break; case NPIC_IMAGE_2Q : return NpicCreateImage_2q ( src->gen.ymax, src->gen.xmax, ybor, xbor); break; case NPIC_IMAGE_3C : return NpicCreateImage_3c ( src->gen.zmax, src->gen.ymax, src->gen.xmax, zbor, ybor, xbor); break; case NPIC_IMAGE_3L : return NpicCreateImage_3l ( src->gen.zmax, src->gen.ymax, src->gen.xmax, zbor, ybor, xbor); break; case NPIC_IMAGE_3D : return NpicCreateImage_3d ( src->gen.zmax, src->gen.ymax, src->gen.xmax, zbor, ybor, xbor); break; case NPIC_IMAGE_3Q : return NpicCreateImage_3q ( src->gen.zmax, src->gen.ymax, src->gen.xmax, zbor, ybor, xbor); break; case NPIC_IMAGE_4C : return NpicCreateImage_4c ( src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_4L : return NpicCreateImage_4l ( src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_4D : return NpicCreateImage_4d ( src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_4Q : return NpicCreateImage_4q ( src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_5C : return NpicCreateImage_5c ( src->gen.smax, src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, sbor, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_5L : return NpicCreateImage_5l ( src->gen.smax, src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, sbor, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_5D : return NpicCreateImage_5d ( src->gen.smax, src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, sbor, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_5Q : return NpicCreateImage_5q ( src->gen.smax, src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, sbor, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_6C : return NpicCreateImage_6c ( src->gen.rmax, src->gen.smax, src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, rbor, sbor, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_6L : return NpicCreateImage_6l ( src->gen.rmax, src->gen.smax, src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, rbor, sbor, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_6D : return NpicCreateImage_6d ( src->gen.rmax, src->gen.smax, src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, rbor, sbor, tbor, zbor, ybor, xbor); break; case NPIC_IMAGE_6Q : return NpicCreateImage_6q ( src->gen.rmax, src->gen.smax, src->gen.tmax, src->gen.zmax, src->gen.ymax, src->gen.xmax, rbor, sbor, tbor, zbor, ybor, xbor); break; } NpicError (__func__, NPIC_ERR_UNEX_NPIC, ""); return NULL; } /* * Create an image dest of same type, size and border than src. * All pixels of dest are set to 0, including border pixels. * Do nothing if src is not ok. * Return dest on success, else return NULL. Verbose. */ Npic_image *NpicDupImage (Npic_image *src) { if (NpicImageIsOK (src, __func__) != NPIC_SUCCESS) return NULL; return NpicDupImageB6 (src, src->gen.rbor, src->gen.sbor, src->gen.tbor, src->gen.zbor, src->gen.ybor, src->gen.xbor); } /* * Free memory of an image (even if not ok). * * This function can be called after * NpicCreateImage_xx(), NpicDupImage(), NpicDupImageB() * even if creation failed. Silent. */ void NpicDestroyImage (Npic_image *np) { if (np == NULL) return ; if (np->type < NPIC_IMAGE_BB || np->type > NPIC_IMAGE_EE) return; NpicFreeProps (&np->gen.props); if (np->gen.vec_) free (np->gen.vec_); if (np->gen.vy_) free (np->gen.vy_); if (np->gen.vz_) free (np->gen.vz_); if (np->gen.vt_) free (np->gen.vt_); if (np->gen.vs_) free (np->gen.vs_); if (np->gen.vr_) free (np->gen.vr_); memset (np, 0, sizeof(Npic_image)); free (np); } /* * Swap all fields between np1 and np2 (even if not ok). Silent. */ void NpicSwapImage (Npic_image *np1, Npic_image *np2) { Npic_image tmp; if (np1 == NULL || np2 == NULL || np1 == np2) return; memcpy (&tmp, np1, sizeof(Npic_image)); memcpy (np1, np2, sizeof(Npic_image)); memcpy (np2, &tmp, sizeof(Npic_image)); } /* * Returns the image which is inside the Npic_image structure (even if not ok). * Silent. * * This is actually a simple cast of the Npic_image* type to void*, * which allows to avoid using the members of the union (something * borring like Npic_image_xx *I = &np->im_xx; ). * * USAGE : * Npic_image *np; * Npic_image_2c *I; * ... * np = NpicCreateImage_xx (..., ymax, xmax, ...); * if (np == NULL) return; * I = NpicCastImage (np); * * for (y = 0; y < I->ymax; y++) I->tab[y][0] = 0; */ void *NpicCastImage (Npic_image *np) { return np; } /*-------------------- P R I V A T E - F U N C T I O N S ---------------------*/ /* * Full creation of an image of any type. * All pixels are set to 0, including border pixels. * Return image on success, else return NULL. Verbose. * Called by NpicCreateImage_...() */ Npic_image *NpicCreateImages (const char *funcname, int type, int pixsize, int dim, int rmax, int smax, int tmax, int zmax, int ymax, int xmax, int rbor, int sbor, int tbor, int zbor, int ybor, int xbor) { Npic_image *np; size_t i, tot_r, tot_rs, tot_rst, tot_rstz, tot_rstzy, tot_rstzyx; if (dim < 2 || dim > 6) { NpicError (funcname, NPIC_ERR_BAD_SIZE, ""); return NULL; } if ( ( dim >= 6 && (rmax <= 0 || rbor < 0) ) || ( dim >= 5 && (smax <= 0 || sbor < 0) ) || ( dim >= 4 && (tmax <= 0 || tbor < 0) ) || ( dim >= 3 && (zmax <= 0 || zbor < 0) ) || ymax <= 0 || xmax <= 0 || ybor < 0 || xbor < 0 ) { NpicError (funcname, NPIC_ERR_BAD_SIZE, ""); return NULL; } np = malloc (sizeof(Npic_image)); if (np == NULL) { NpicError (funcname, NPIC_ERR_MALLOC, ""); return NULL; } memset (np, 0, sizeof(Npic_image)); np->type = type; np->gen.ok = NPIC_SUCCESS; np->gen.pixsize = pixsize; np->gen.dim = dim; np->gen.rbor = rbor; np->gen.sbor = sbor; np->gen.tbor = tbor; np->gen.zbor = zbor; np->gen.ybor = ybor; np->gen.xbor = xbor; NpicInitProps (&np->gen.props); /* Compute bounds ; set rtot, stot, ztot or ttot to 1 by default */ np->gen.xmax = xmax; np->gen.xtot = xmax+xbor*2; np->gen.ymax = ymax; np->gen.ytot = ymax+ybor*2; if (dim >= 3) { np->gen.zmax = zmax; np->gen.ztot = zmax+zbor*2; } else np->gen.zmax = np->gen.ztot = 1; if (dim >= 4) { np->gen.tmax = tmax; np->gen.ttot = tmax+tbor*2; } else np->gen.tmax = np->gen.ttot = 1; if (dim >= 5) { np->gen.smax = smax; np->gen.stot = smax+sbor*2; } else np->gen.smax = np->gen.stot = 1; if (dim >= 6) { np->gen.rmax = rmax; np->gen.rtot = rmax+rbor*2; } else np->gen.rmax = np->gen.rtot = 1; /* The total number of pixels might be huge --> use size_t instead of int */ tot_r = np->gen.rtot; tot_rs = tot_r * np->gen.stot; tot_rst = tot_rs * np->gen.ttot; tot_rstz = tot_rst * np->gen.ztot; tot_rstzy = tot_rstz * np->gen.ytot; tot_rstzyx = tot_rstzy * np->gen.xtot; np->gen.pixtot = tot_rstzyx; /* Table of pixels, on linear form * Remark: the size of vy, vz, vt, vs, vr is negligible compared to vec. */ np->gen.vec_ = malloc (np->gen.pixtot * np->gen.pixsize); if (np->gen.vec_ == NULL) { NpicError (funcname, NPIC_ERR_MALLOC, ""); NpicDestroyImage (np); return NULL; } /* Address row of lines, plans, volumes, etc, starting */ np->gen.vy_ = malloc (sizeof(void *) * tot_rstzy); if (dim >= 3) np->gen.vz_ = malloc (sizeof(void *) * tot_rstz); if (dim >= 4) np->gen.vt_ = malloc (sizeof(void *) * tot_rst); if (dim >= 5) np->gen.vs_ = malloc (sizeof(void *) * tot_rs); if (dim >= 6) np->gen.vr_ = malloc (sizeof(void *) * tot_r); if ( np->gen.vy_ == NULL || (dim >= 3 && np->gen.vz_ == NULL) || (dim >= 4 && np->gen.vt_ == NULL) || (dim >= 5 && np->gen.vs_ == NULL) || (dim >= 6 && np->gen.vr_ == NULL) ) { NpicError (funcname, NPIC_ERR_MALLOC, ""); NpicDestroyImage (np); return NULL; } /* Compute addresses tab[y][x] or .. or tab[r][s][t][z][y][x]) * of pixels with offset for external border. * The first internal pixel in image is [0][0] or .. or [0][0][0][0][0][0]). */ for (i = 0; i < tot_rstzy; i++) np->gen.vy_[i] = (char *) np->gen.vec_ + (i*np->gen.xtot + np->gen.xbor) * pixsize; np->gen.pix_ = np->gen.vy_ + np->gen.ybor; if (dim >= 3) { for (i = 0; i < tot_rstz; i++) np->gen.vz_[i] = np->gen.vy_ + i*np->gen.ytot + np->gen.ybor; np->gen.pix_ = np->gen.vz_ + np->gen.zbor; } if (dim >= 4) { for (i = 0; i < tot_rst; i++) np->gen.vt_[i] = np->gen.vz_ + i*np->gen.ztot + np->gen.zbor; np->gen.pix_ = np->gen.vt_ + np->gen.tbor; } if (dim >= 5) { for (i = 0; i < tot_rs; i++) np->gen.vs_[i] = np->gen.vt_ + i*np->gen.ttot + np->gen.tbor; np->gen.pix_ = np->gen.vs_ + np->gen.sbor; } if (dim >= 6) { for (i = 0; i < tot_r; i++) np->gen.vr_[i] = np->gen.vs_ + i*np->gen.stot + np->gen.sbor; np->gen.pix_ = np->gen.vr_ + np->gen.rbor; } /* Initialise pixels to 0 */ NpicFillWholeZero (np); return np; } /*---------------------------------------------------------------------------*/