?? image_alloc.c
字號(hào):
/*..........................................................................*//* *//* L a s t W a v e P a c k a g e 'image' 2.1 *//* *//* Copyright (C) 1998-2003 Emmanuel Bacry, Jerome Fraleu. *//* emails : fraleu@cmap.polytechnique.fr *//* lastwave@cmap.polytechnique.fr *//* *//*..........................................................................*//* *//* This program is a free software, you can redistribute it and/or *//* modify it under the terms of the GNU General Public License as *//* published by the Free Software Foundation; either version 2 of the *//* License, or (at your option) any later version *//* *//* This program is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *//* GNU General Public License for more details. *//* *//* You should have received a copy of the GNU General Public License *//* along with this program (in a file named COPYRIGHT); *//* if not, write to the Free Software Foundation, Inc., *//* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* *//*..........................................................................*//****************************************************************************//* *//* image_alloc.c Functions which deal with the dynamical *//* allocation of memory for IMAGE 's *//* *//****************************************************************************/#include "lastwave.h"#include "images.h"#include "int_fsilist.h"/********************************************** * * Send a message to an image * **********************************************//* Default name for an image */static char defaultName[] = "";char *imageType = "&image";char *imageiType = "&imagei"; /********************************************/extern int flagOn;IMAGE NewImage(){ extern TypeStruct tsImage; IMAGE image; #ifdef DEBUGALLOCDebugType = "Image";#endif image = (IMAGE) (Malloc(sizeof(struct image))); InitValue(image,&tsImage); image->pixels = NULL; image->nrow = image->ncol = 0; image->sizeMalloc = 0; image->border_ver =0; image->border_hor =0; image->name = defaultName; if (flagOn) Printf("** New Image %p\n",image); return (image);}IMAGE TNewImage(void){ IMAGE image; image = NewImage(); TempValue(image); return(image);}void DeleteImage(image) IMAGE image;{ if (image) { if (image->nRef==0) { Warningf("DeleteImage() : Trying to delete a temporary image\n"); return; } RemoveRefValue(image); if (image->nRef > 0) return; if (flagOn) Printf("** Delete Image %p\n",image); if (image->name != defaultName) Free(image->name); image->name=NULL; if (image->pixels) Free(image->pixels); image->pixels = NULL;#ifdef DEBUGALLOCDebugType = "Image";#endif Free(image); image=NULL; }}/* Reinitialization of structure and desallocate the array */void ClearImage(IMAGE image){ if (image->pixels) Free(image->pixels); image->pixels = NULL; image->nrow = image->ncol = 0; image->sizeMalloc = 0;} void SizeImage(IMAGE image, int ncol, int nrow){ extern LWFLOAT *FloatCAlloc(int size); int s; int nrow1= nrow+ (1-nrow%2); int ncol1= ncol+ (1-ncol%2); if (image == NULL) Errorf("SizeImage() : Try to change the size of an NULL image. \n"); s = nrow1*ncol1; if (s > image->sizeMalloc) { if (image->pixels) Free(image->pixels); image->pixels = FloatCAlloc(s); image->sizeMalloc = s; } image->nrow = nrow; image->ncol = ncol;}IMAGE CopyImage(IMAGE in,IMAGE out){ int nrow1= in->nrow+ (1-in->nrow%2); int ncol1= in->ncol+ (1-in->ncol%2); if (in == out) return(out); if (out == NULL) out = NewImage(); SizeImage(out,in->ncol,in->nrow); CopyFieldsImage(in,out); memcpy(out->pixels,in->pixels,nrow1*ncol1*sizeof(LWFLOAT)); return(out);}void CopyFieldsImage(IMAGE in,IMAGE out){ out->border_hor = in->border_hor; out->border_ver = in->border_ver;}/*********************************************************************************** * * Image and variables * ***********************************************************************************//* * Getting an image variable at a given level * (Set the error message and returns NULL if there is an error) */ IMAGE GetImageVariableLevel(LEVEL level,char *name){ IMAGE image; char *type; VALUE value; value = GetVariableContentLevel(level,name,NULL); type = GetTypeValue(value); if (type != imageType && type != imageiType) { SetErrorf("GetImageVariableLevel() : Variable '%s' has not the expected type '&image' or '&imagei'",name); return(NULL); } image = (IMAGE) value; return(image);} IMAGE GetImageVariable(char *name){ return(GetImageVariableLevel(levelCur,name));}/* * Setting an image variable at a given level (creates it if it does not exist) */ void SetImageVariableLevel(LEVEL level,char *name,IMAGE image){ SetVariableLevel(level,name,(VALUE) image);}void SetImageVariable(char *name,IMAGE image){ SetVariable(name,(VALUE) image);}/*********************************************************************************** * * Parsing an image * ***********************************************************************************/ /* * Parse an output Image */char ParseImageLevel_(LEVEL level, char *arg, IMAGE defVal, IMAGE *im){ char *type; LWFLOAT f; type = TTEvalExpressionLevel_(level,arg,&f,(VALUE *) im,ImageType,NO,NO,AnyType,YES); if (type == NULL) { *im = defVal; if (defVal != NULL) (*im)->nRef++; return(NO); } return(YES);}char ParseImage_(char *arg, IMAGE defVal, IMAGE *im){ return(ParseImageLevel_(levelCur,arg,defVal,im));}void ParseImageLevel(LEVEL level,char *arg,IMAGE *im){ if (ParseImageLevel_(level,arg,NULL,im) == NO) Errorf1("");}void ParseImage(char *arg, IMAGE *im){ ParseImageLevel(levelCur,arg,im);}/* * Parse an input Image */char ParseImageILevel_(LEVEL level, char *arg, IMAGE defVal, IMAGE *im){ char *type; LWFLOAT f; type = TTEvalExpressionLevel_(level,arg,&f,(VALUE *) im,ImageType,NO,NO,AnyType,NO); if (type == NULL || (*im)->nrow == 0 || (*im)->ncol == 0) { *im = defVal; if (defVal != NULL) { if (defVal->nrow == 0 || defVal->ncol == 0) Errorf("ParseImageILevel_() : default signal is empty"); (*im)->nRef++; } return(NO); } return(YES);}char ParseImageI_(char *arg, IMAGE defVal, IMAGE *im){ return(ParseImageILevel_(levelCur,arg,defVal,im));}void ParseImageILevel(LEVEL level, char *arg, IMAGE *im){ if (ParseImageILevel_(level,arg,NULL,im) == NO) Errorf1("");}void ParseImageI(char *arg, IMAGE *im){ ParseImageILevel(levelCur,arg,im);}void C_InfoImage (char **argv){ IMAGE image; argv = ParseArgv(argv,tIMAGEI,&image,0); if (image == NULL) Errorf(" NULL image"); Printf(" Image '%s' [%d]\n",image->name,image->nRef); Printf(" ncol x nrow : %d x %d [%d]\n",image->ncol,image->nrow,image->sizeMalloc); Printf("\n"); }void C_CopyImage (char **argv){ IMAGE input,output; argv = ParseArgv(argv,tIMAGEI,&input,tIMAGE,&output,0); CopyImage(input,output);}/* * Function to get the type of an image */static char * GetTypeImage(VALUE value){ IMAGE i = (IMAGE) value; if (i->nrow == 0 || i->ncol == 0) return(imageType); return(imageiType);}/* * Print an image when it is a result */#define ImPrintLengthRow 6#define ImPrintLengthCol 10 void PrintImage(IMAGE im){ int r,c; if (im->ncol==0 || im->nrow==0) { Printf("<nrow=%d;ncol=%d>",im->nrow,im->ncol); return; } for (r = 0; r < im->nrow; r++) { for (c = 0; c < im->ncol; c++) { Printf(LWDoubleFormat, im->pixels[im->ncol*r+c]); Printf(" "); } Printf("\n"); }}/* * String conversion */char *ToStrImage(IMAGE im, char flagShort){ static char strShort[50]; int nAlloc; char *str; int r,c; if (flagShort || im->ncol==0 || im->nrow==0 || im->ncol > ImPrintLengthCol || im->nrow > ImPrintLengthRow) { sprintf(strShort,"<nrow=%d;ncol=%d>",im->nrow,im->ncol); return(strShort); } nAlloc = 300; str = CharAlloc(nAlloc); TempPtr(str); sprintf(str,"<nrow=%d;ncol=%d>\n",im->nrow,im->ncol); for (r = 0; r < im->nrow; r++) { for (c = 0; c < im->ncol; c++) { if (strlen(str) > nAlloc-40) { nAlloc+=300; str = CharAlloc(nAlloc); TempPtr(str); } sprintf(strShort,LWDoubleFormat,im->pixels[im->ncol*r+c]); strcat(strShort," "); strcat(str,strShort); } if (r!= im->nrow-1) strcat(str,"\n"); } return(str);}/* * Print the info of a signal */void PrintInfoImage(IMAGE im){ Printf(" nrow x ncol : %d x %d [%d]\n",im->nrow,im->ncol,im->sizeMalloc);}/* * The main routine that returns a pixel value given (col,row) indexes and a border type. */LWFLOAT CR2PixIm(IMAGE im,int row, int col, BorderType btc, BorderType btr){ switch (btc) { case BorderNone : break; case BorderCon : /* ?? a faire ?? */ break;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -