📄 lut.cpp
字号:
#include "mex.h"#include <math.h>#include <stdlib.h>#include <string.h>#include <tmwtypes.h>/* * performs LUT on an array: B[k] = A[LUT[k]] * * Usage: * B = mylut(A, LUT) * * Inputs: * A: indexing into the LUT, MATLAB indexing starting from 1. * values less than one or greater than length(LUT) are IGNORED. * A may be of class single/double - non integer values are cast to int (fractions are ignored). * LUT a one dimensional vector * Output: * B: A after the LUT, same class as LUT. * * * * Copyright (c) Bagon Shai * Department of Computer Science and Applied Mathmatics * Wiezmann Institute of Science * http://www.wisdom.weizmann.ac.il/ * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, subject to the following conditions: * * 1. The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * 2. No commercial use will be done with this software. * 3. If used in an academic framework - a proper citation must be included. * * The Software is provided "as is", without warranty of any kind. * * Mar. 2007 * *//* steps in bytes for each type */int GetStep(const mxArray* x){ switch (mxGetClassID(x)) { case mxCHAR_CLASS: case mxINT8_CLASS: case mxUINT8_CLASS: return 1; case mxUINT16_CLASS: case mxINT16_CLASS: return 2; case mxINT32_CLASS: case mxUINT32_CLASS: case mxSINGLE_CLASS: return 4; case mxINT64_CLASS: case mxUINT64_CLASS: case mxDOUBLE_CLASS: return 8; default: mexErrMsgIdAndTxt("LUT:GetStep","unsupported data type"); }}inlinevoid GetScalar(const mxArray* x, int offset, int& scalar){ if (( offset < 0 ) || ( mxGetNumberOfElements(x) <= offset )) mexErrMsgIdAndTxt("LUT:GetScalar","illegal offset %d", offset); char *p = (char*)mxGetData(x); switch (mxGetClassID(x)) { case mxCHAR_CLASS: // TETSU: a CHAR should be 2 bytes. scalar = *(char*)(p+offset); break; case mxDOUBLE_CLASS: scalar = *(double*)(p+8*offset); break; case mxSINGLE_CLASS: scalar = *(float*)(p+4*offset); break; case mxINT8_CLASS: scalar = *(char*)(p+offset); break; case mxUINT8_CLASS: scalar = *(unsigned char*)(p+offset); break; case mxINT16_CLASS: scalar = *(short*)(p+2*offset); break; case mxUINT16_CLASS: scalar = *(unsigned short*)(p+2*offset); break; case mxINT32_CLASS: scalar = *(int*)(p+4*offset); break; case mxUINT32_CLASS: scalar = *(unsigned int*)(p+4*offset); break; case mxINT64_CLASS: scalar = *(int64_T*)(p+8*offset); break; case mxUINT64_CLASS: scalar = *(uint64_T*)(p+8*offset); break; default: mexErrMsgIdAndTxt("LUT:GetScalar","unsupported data type"); }}/* * main enterance point */void mexFunction( int nlhs, /* number of expected outputs */ mxArray *plhs[], /* mxArray output pointer array */ int nrhs, /* number of inputs */ const mxArray *prhs[] /* mxArray input pointer array */ ){ /* check arguments */ if (nrhs != 2) mexErrMsgIdAndTxt("LUT:main","wrong number of inputs - should be two"); if (nlhs != 1) mexErrMsgIdAndTxt("LUT:main","wrong number of outputs - must be one"); char * LUT = (char*)mxGetData(prhs[1]); plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[0]), mxGetDimensions(prhs[0]), mxGetClassID(prhs[1]), mxREAL); char * B = (char*)mxGetData(plhs[0]); int step = GetStep(prhs[1]); int maxLUT = mxGetNumberOfElements(prhs[1]); // maximal LUT value int tmp(0); for (int j(0); j < mxGetNumberOfElements(plhs[0]) ; j++) { GetScalar(prhs[0], j, tmp); /* A = prhs[0] cast to int to prevent fractions */ tmp--; /* tmp = A[j] */ if ( ( tmp < 0 ) || ( tmp >= maxLUT ) ) continue; /* skip and ignore... */ //mexErrMsgIdAndTxt("LUT:main","A value exceeds LUT size"); memcpy(B + j*step, LUT + tmp*step, step); /* LUT+tmp*step = LUT[A[j]] */ } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -