📄 imgmem.c
字号:
/* * -*- Mode ANSI C -*- * $Id: imgmem.c,v 1.6 1996/09/17 16:10:50 fernande Exp $ * $Source: /sgi.acct/sweldens/cvs/liftpack/Util/imgmem.c,v $ * Author: Gabriel Fernandez, Senthil Periaswamy * * Image Allocation routines. *//* do not edit anyhting above this line *//* FLWT header files */#include <imgmem.h>#include <flwterr.h>#include <memchk.h>/********************************************************************************** The high level routines are: **** **** Image IMG_InitMem ( long width, long height, int colorPlanes, int bpp ); **** void IMG_FreeMem ( Image *img ); **** **** The lower level routines are: **** **** Volume MEM_Init3DC( int depth, long width, long height ); **** Volume MEM_Init3D( int depth, long width, long height ); **** Matrix MEM_Init2D( long width, long height ); **** Vector MEM_Init1D( long width ); **** void MEM_Free2D( Matrix buff ); **** void MEM_Free1D( Vector buff ); **** **** and the lowest level routine are malloc and free. **** ***********************************************************************************//* code *//* * IMG_InitMem function: allocates memory for an image, guaranteeing * continuity in the bands. */booleanIMG_InitMem ( Image *img, const long width, const long height, const int colorPlanes, const int bpp ){ int i; if ( colorPlanes > MAX_COLOR_PLANES || colorPlanes <= 0 ) { Error ("IMG_InitMem", INVALID_VALUE_INT, ABORT, "color planes", 1, MAX_COLOR_PLANES); } if ( width == 0 || height == 0 ) { Error ("IMG_Initmem", INVALID_RELATIONAL_INT, ABORT, "Dimensions", ">", 0); /* NOTREACHED */ } /* Everything should be fine now */ img->width = (int)width; img->height = (int)height; img->colorPlanes = colorPlanes; img->bpp = bpp; for ( i=0; i<colorPlanes ; i++ ) { img->band[i] = MEM_Init2D ( width, height ); if (!img->band[i]) { Error ("IMG_InitMem", MEMORY_MATRIX, RETURN); return FALSE; } } return TRUE;}/* * IMG_FreeMem function: given the image allocated in IMG_InitMem, the * function deallocates the occupied memory. */voidIMG_FreeMem ( Image img ){ int i; /* Free each band */ for ( i=0 ; i<img.colorPlanes ; i++ ) { if ( img.band[i] ) { MEM_Free2D ( (Matrix)img.band[i] ); img.band[i] = (Matrix)NULL; } if ( img.comment ) { free ( (char *)img.comment ); img.comment = NULL; } if ( img.levels ) { free ( (int *)img.levels ); img.levels = NULL; } }}/* * MEM_Init3DC function: initialize memory for a contiguous 3D volume. */VolumeMEM_Init3DC ( int depth, long width, long height ){ Volume buff; Vector ptrData; long y, z; /* Allocate the whole 3D image */ ptrData = MEM_Init1D( (long)(depth*width*height) ); if ( ptrData == NULL ) { return NULL; } /* Allocate memory for the array of double pointers */ buff = (Volume)malloc( (size_t)(depth*sizeof(Matrix)) ); if ( buff == NULL ) { return NULL; } /* Assign pointers of the matrices */ for( z=0 ; z<depth ; z++ ) { buff[z] = (Matrix)malloc( (size_t)(height*sizeof(Vector)) ); if ( buff[z] == NULL ) { return NULL; } for( y=0 ; y<height ; y++, ptrData += width ) { buff[z][y] = ptrData; } } return buff;}/* * MEM_Free3DC function: frees a contiguos 3D volume. */voidMEM_Free3DC ( Volume buff ){ if ( buff == NULL ) return; MEM_Free1D ( buff[0][0] ); free ( (Matrix)buff[0] ); free ( (Volume)buff );}/* * MEM_Init3D function: initialize memory for an array of matrices. */Matrix *MEM_Init3D ( int depth, long width, long height ){ Matrix *buff; long i; /* Allocate memory for a row of pointers */ buff = (Matrix *)malloc( (size_t)(depth*sizeof(Matrix)) ); if ( !buff ) return NULL; /* Allocate each matrix with Init2D */ for ( i=0 ; i<(long)depth ; i++ ) { buff[i] = MEM_Init2D( (long)width, (long)height ); if ( !buff[i] ) return NULL; } return buff;}/* * MEM_Free3D function: frees an array of matrices. */voidMEM_Free3D ( Matrix *buff, int depth ){ int i; if ( buff == NULL ) return; for ( i=0 ; i<depth ; i++ ) { MEM_Free2D ( buff[i] ); } free ( (Matrix *)buff );}/* * MEM_Init2D function: initialize memory for a 2D vector. */MatrixMEM_Init2D ( long width, long height ){ Matrix buff; long i, length = width*height; /* First allocate a row of pointers */ buff = (Matrix)malloc( (size_t)(height*sizeof(Vector)) ); if ( buff == NULL ) { return NULL; } /* Allocate entire 2D vector */ buff[0] = MEM_Init1D ( length ); if ( buff[0] == NULL ) { return NULL; } /* Set the row-pointers to point correctly into the 2D vector */ for ( i=1 ; i<height ; ++i ) { buff[i] = buff[i-1] + width; } return buff;}/* * MEM_Free2D function: frees a 2D vector. */voidMEM_Free2D ( Matrix buff ){ if ( buff == NULL ) return; MEM_Free1D ( buff[0] ); free ( (Matrix)buff );}/* * MEM_InitVecs function: initialize memory for an array of vectors. */Vector *MEM_InitVecs ( int depth, long width ){ Vector *buff; long i; /* Allocate memory for a row of pointers */ buff = (Vector *)malloc( (size_t)(depth*sizeof(Vector)) ); if ( !buff ) return NULL; /* Allocate each vector with MEM_Init1D */ for ( i=0 ; i<(long)depth ; i++ ) { buff[i] = MEM_Init1D( (long)width ); if ( !buff[i] ) return NULL; } return buff;}/* * MEM_FreeVecs function: frees an array of vectors. */voidMEM_FreeVecs ( Vector *buff, int depth ){ int i; if ( buff == NULL ) return; for ( i=0 ; i<depth ; i++ ) { MEM_Free1D( buff[i] ); } free ( (Vector *)buff );}/* * MEM_Init1D function: initializes a 1D vector. */VectorMEM_Init1D ( long n ){ return (Vector)malloc( (size_t)(n*sizeof(Flt)) );}/* * MEM_Free1D function: frees a 1D vector. */voidMEM_Free1D ( Vector buff ){ free ( (Vector)buff );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -