示例#1
0
Var* ff_cinterp(vfuncptr func, Var* arg)
{
	Var* v[3]    = {NULL, NULL, NULL};
	float ignore = FLT_MIN;
	char* type   = NULL;

	Alist alist[6];
	alist[0]      = make_alist("object", ID_VAL, NULL, &v[0]);
	alist[1]      = make_alist("from", ID_VAL, NULL, &v[1]);
	alist[2]      = make_alist("to", ID_VAL, NULL, &v[2]);
	alist[3]      = make_alist("type", ID_ENUM, NULL, type);
	alist[4]      = make_alist("ignore", DV_FLOAT, NULL, &ignore);
	alist[5].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (v[0] == NULL || v[1] == NULL || v[2] == NULL) {
		parse_error("error, usage: cinterp(y1,x1,x2)\n");
		return (NULL);
	}

	if (V_DSIZE(v[0]) != V_DSIZE(v[1])) {
		parse_error("error: x1 and y1 must be the same size\n");
		return (NULL);
	}
	return (cubic_interp(v[0], v[1], v[2], type, ignore));
}
示例#2
0
Var* ff_grassfire(vfuncptr func, Var* arg)
{
	Var* obj              = NULL;
	int ignore            = INT_MAX;
	const char* options[] = {"euclidian", "manhattan", "bounding", NULL};
	char* type            = (char*)options[0];

	Alist alist[4];
	alist[0]      = make_alist("obj", ID_VAL, NULL, &obj);
	alist[1]      = make_alist("ignore", DV_INT32, NULL, &ignore);
	alist[2]      = make_alist("type", ID_ENUM, options, &type);
	alist[3].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No value specified for keyword: object.", func->name);
		return (NULL);
	}

	if (!strcmp(type, "euclidian")) {
		return (saito_grassfire(obj, ignore));
	} else if (!strcmp(type, "manhattan")) {
		return (vw_grassfire(obj, ignore));
	} else if (!strcmp(type, "bounding")) {
		return (bounding_box(obj, ignore));
	} else {
		parse_error("%s: Unknown algorithm.", func->name);
		return (NULL);
	}
}
示例#3
0
文件: ff_fft.c 项目: robwink/davinci
Var* ff_fft(vfuncptr func, Var* arg)
{
	Var *real = NULL, *img = NULL;
	double* data;
	int i, j, n, x, y, z;
	COMPLEX *in, *out;

	Alist alist[4];
	alist[0]      = make_alist("real", ID_VAL, NULL, &real);
	alist[1]      = make_alist("img", ID_VAL, NULL, &img);
	alist[2].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (real == NULL && img == NULL) {
		parse_error("%s: No real or imaginary objects specified\n", func->name);
		return (NULL);
	}
	x = GetSamples(V_SIZE(real), V_ORG(real));
	y = GetLines(V_SIZE(real), V_ORG(real));
	z = GetBands(V_SIZE(real), V_ORG(real));

	if (img == NULL && x == 2) {
		n   = y * z;
		in  = (COMPLEX*)calloc(n, sizeof(COMPLEX));
		out = (COMPLEX*)calloc(n, sizeof(COMPLEX));
		for (i = 0; i < y; i++) {
			for (j = 0; j < z; j++) {
				in[i].re = extract_double(real, cpos(0, i, j, real));
				in[i].im = extract_double(real, cpos(1, i, j, real));
			}
		}
	} else {
		n   = V_DSIZE(real);
		in  = (COMPLEX*)calloc(n, sizeof(COMPLEX));
		out = (COMPLEX*)calloc(n, sizeof(COMPLEX));
		for (i = 0; i < n; i++) {
			in[i].re = extract_double(real, i);
			in[i].im = (img == NULL ? 0.0 : extract_double(img, i));
		}
	}

	if (func->fdata == (void*)1) {
		fft(in, n, out);
	} else {
		rft(in, n, out);
	}

	data = (double*)calloc(n * 2, sizeof(double));

	for (i = 0; i < n; i++) {
		data[i * 2]     = out[i].re;
		data[i * 2 + 1] = out[i].im;
	}
	return (newVal(BSQ, 2, n, 1, DV_DOUBLE, data));
}
示例#4
0
Var* ff_radial_symmetry2(vfuncptr func, Var* arg)
{
	Var *obj = NULL, *rval = NULL;
	float ignore = FLT_MIN;
	float* out;
	int x, y, z, i, j;
	int size  = 0;
	int width = 0, height = 0;
	Window* w;

	Alist alist[9];
	alist[0]      = make_alist("object", ID_VAL, NULL, &obj);
	alist[1]      = make_alist("x", DV_INT32, NULL, &width);
	alist[2]      = make_alist("y", DV_INT32, NULL, &height);
	alist[3]      = make_alist("size", DV_INT32, NULL, &size);
	alist[4]      = make_alist("ignore", DV_FLOAT, NULL, &ignore);
	alist[5].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No object specified\n", func->name);
		return (NULL);
	}

	if (size) {
		width  = size;
		height = size;
	}

	if (width <= 0 || height <= 0) {
		parse_error("%s: Invalid size specified (%dx%d)\n", func->name, width, height);
		return (NULL);
	}

	x = GetX(obj);
	y = GetY(obj);
	z = GetZ(obj);
	w = create_window(width, height, DV_FLOAT);

	out  = calloc((size_t)x * (size_t)y, sizeof(float));
	rval = newVal(BSQ, x, y, 1, DV_FLOAT, out);

	for (i = 0; i < x; i += 1) {
		load_window(w, obj, i, 0, ignore);
		for (j = 0; j < y; j += 1) {
			if (j) roll_window(w, obj, i, j, ignore);

			out[cpos(i, j, 0, rval)] = radial_symmetry2(w->row, width, height, ignore);
		}
	}

	free_window(w);
	return (rval);
}
示例#5
0
文件: ff_fft.c 项目: robwink/davinci
Var* ff_realfft(vfuncptr func, Var* arg)
{
	Var* obj = NULL;
	int i, n;
	double *in, *out;

	Alist alist[3];
	alist[0]      = make_alist("obj", ID_VAL, NULL, &obj);
	alist[1].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No object specified\n", func->name);
		return (NULL);
	}

	n   = V_DSIZE(obj);
	in  = (double*)calloc(n, sizeof(double));
	out = (double*)calloc(n, sizeof(double));

	for (i = 0; i < n; i++) {
		in[i] = extract_double(obj, i);
	}

	if (func->fdata == (void*)1) {
		realfft(in, n, out);
	} else {
		realrft(in, n, out);
	}
	return (newVal(BSQ, 1, n, 1, DV_DOUBLE, out));
}
示例#6
0
Var* ff_unload_dv_module(vfuncptr func, Var* args)
{
	int ac;
	Var** av;
	char* module_name = NULL;

	Alist alist[2]; /* arguments list */

	alist[0]      = make_alist("mod", ID_STRING, NULL, &module_name);
	alist[1].name = NULL;

	if (parse_args(func, args, alist) == 0) {
		parse_error("%s(): argument parsing failed.", func->name);
		return NULL;
	}

	if (module_name == NULL) {
		parse_error("%s(): \"%s\" must be specified.", func->name, alist[0].name);
		return NULL;
	}

	unload_dv_module(module_name);

	return NULL;
}
示例#7
0
Var* ff_local_maximum(vfuncptr func, Var* arg)
{
	Var *obj = NULL, *rval = NULL;
	float ignore = FLT_MIN;
	float* out;
	int x, y, i, j;
	int size        = 3;
	float threshold = FLT_MIN;
	Window* w;

	Alist alist[9];
	alist[0]      = make_alist("object", ID_VAL, NULL, &obj);
	alist[1]      = make_alist("size", DV_INT32, NULL, &size);
	alist[2]      = make_alist("ignore", DV_FLOAT, NULL, &ignore);
	alist[3]      = make_alist("threshold", DV_FLOAT, NULL, &threshold);
	alist[4].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No object specified\n", func->name);
		return (NULL);
	}

	x = GetX(obj);
	y = GetY(obj);
	w = create_window(size, size, DV_FLOAT);

	out  = calloc(x * y, sizeof(float));
	rval = newVal(BSQ, x, y, 1, DV_FLOAT, out);

	for (i = 0; i < x; i += 1) {
		load_window(w, obj, i, 0, ignore);
		for (j = 0; j < y; j += 1) {
			if (j) roll_window(w, obj, i, j, ignore);

			out[cpos(i, j, 0, rval)] = local_maximum(w, threshold, ignore);
		}
	}

	free_window(w);
	return (rval);
}
示例#8
0
Var* ff_interp(vfuncptr func, Var* arg)
{
	Var* v[3]           = {NULL, NULL, NULL};
	float ignore        = FLT_MIN;
	const char* usage   = "usage: %s(y1,x1,x2,[type={'linear'|'cubic'}]";
	char* type          = (char*)"";
	const char* types[] = {"linear", "cubic", NULL};
	Var* out;

	Alist alist[9];
	alist[0]      = make_alist("object", ID_VAL, NULL, &v[0]);
	alist[1]      = make_alist("from", ID_VAL, NULL, &v[1]);
	alist[2]      = make_alist("to", ID_VAL, NULL, &v[2]);
	alist[3]      = make_alist("ignore", DV_FLOAT, NULL, &ignore);
	alist[4]      = make_alist("type", ID_ENUM, types, &type);
	alist[5]      = make_alist("y1", ID_VAL, NULL, &v[0]);
	alist[6]      = make_alist("x1", ID_VAL, NULL, &v[1]);
	alist[7]      = make_alist("x2", ID_VAL, NULL, &v[2]);
	alist[8].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (v[0] == NULL) {
		parse_error("%s: y1 not specified.", func->name);
		parse_error(usage, func->name);
		return (NULL);
	}
	if (v[1] == NULL) {
		parse_error("%s: x1 not specified.", func->name);
		parse_error(usage, func->name);
		return (NULL);
	}
	if (v[2] == NULL) {
		parse_error("%s: x2 not specified.", func->name);
		parse_error(usage, func->name);
		return (NULL);
	}

	if (V_DSIZE(v[0]) != V_DSIZE(v[1])) {
		parse_error("Object and From values must be same size\n");
	}

	if (type == NULL || strlen(type) == 0 || !strcasecmp(type, "linear")) {
		out = linear_interp(v[0], v[1], v[2], ignore);
	} else if (!strncasecmp(type, "cubic", 5)) {
		out = cubic_interp(v[0], v[1], v[2], type, ignore);
	} else {
		parse_error("%s: Unrecognized type: %s\n", func->name, type);
	}
	return (out);
}
示例#9
0
文件: lshd.c 项目: jeremyfrench/lsh
static struct lshd_context *
make_lshd_context(void)
{
  NEW(lshd_context, self);

  init_transport_context (&self->super, 1);
  self->service_config = make_service_config ();

  self->keys = make_alist(0, -1);

  return self;
}
示例#10
0
Var* ff_drawshape(vfuncptr func, Var* arg)
{
	Var *obj = NULL, *ovar = NULL;
	size_t x, y, z;
	char* out;
	float ignore = FLT_MAX;

	const char* options[] = {"cross", "box", "circle", NULL};
	const char* shape     = options[0];

	Alist alist[9];
	alist[0]      = make_alist("object", ID_VAL, NULL, &obj);
	alist[1]      = make_alist("shape", ID_ENUM, options, &shape);
	alist[2]      = make_alist("ignore", DV_FLOAT, NULL, &ignore);
	alist[3].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No object specified\n", func->name);
		return (NULL);
	}

	x    = GetX(obj);
	y    = GetY(obj);
	z    = GetZ(obj);
	out  = calloc(x * y, sizeof(char));
	ovar = newVal(BSQ, x, y, 1, DV_UINT8, out);

	if (!strcmp(shape, "cross")) {
		draw_cross(obj, x, y, ignore, out);
	} else if (!strcmp(shape, "box")) {
		draw_box(obj, x, y, ignore, out);
	} else if (!strcmp(shape, "circle")) {
		draw_circle(obj, x, y, ignore, out);
	}
	return (ovar);
}
示例#11
0
Var* ff_load_dv_module(vfuncptr func, Var* args)
{
	int ac;
	Var** av;
	Var* v_return     = NULL;
	char* module_name = NULL;

	Alist alist[2]; /* arguments list */

	alist[0]      = make_alist("mod", ID_STRING, NULL, &module_name);
	alist[1].name = NULL;

	if (parse_args(func, args, alist) == 0) {
		parse_error("%s(): argument parsing failed.", func->name);
		return NULL;
	}

	if (module_name == NULL) {
		parse_error("%s(): \"%s\" must be specified.", func->name, alist[0].name);
		return NULL;
	}

	if (get_global_sym(module_name)) {
		parse_error(
		    "%s(): Variable %s already exists in global space. "
		    "Module load aborted.",
		    func->name, module_name);
		return NULL;
	}

	v_return = new_module(module_name);
	if (v_return) {
		/*
		** Actually load the module into the davinci variable
		** just created.
		*/
		if (!load_dv_module(module_name, NULL, 1, &V_MODULE(v_return))) {
			free_var(v_return);
			return NULL;
		}
		/* stick the symbol into the symbol table */
		/* sym_put(global_scope(), v_return); */
		put_global_sym(v_return);
		V_MODULE(v_return).stage = MOD_VAR_ADDED;
	}

	return v_return;
	/* return NULL; */
}
示例#12
0
文件: ff_fft.c 项目: robwink/davinci
Var* ff_realfft3(vfuncptr func, Var* arg)
{
	Var* obj = NULL;
	size_t i, n;
	double* in;

	Alist alist[3];
	alist[0]      = make_alist("obj", ID_VAL, NULL, &obj);
	alist[1].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No object specified\n", func->name);
		return (NULL);
	}

	n = V_DSIZE(obj);
	if (n > INT_MAX) {
		parse_error("%s: fft function does not handle objects greater than %ld bytes.\n",
		            func->name, INT_MAX);
		return NULL;
	}

	in = (double*)calloc(n, sizeof(double));
	if (in == NULL) {
		parse_error("%s: Unable to alloc %ld bytes.\n", func->name, n * sizeof(double));
		return NULL;
	}

	for (i = 0; i < n; i++) {
		in[i] = extract_double(obj, i);
	}

	if (func->fdata == (void*)1) {
		mayer_realfft(n, in);
	} else {
		mayer_realifft(n, in);
	}

	return (newVal(BSQ, 1, n, 1, DV_DOUBLE, in));
}
示例#13
0
文件: ff_fft.c 项目: robwink/davinci
Var* ff_realfft2(vfuncptr func, Var* arg)
{
	Var* obj = NULL;
	int i, j, n, x;
	double* in;

	Alist alist[3];
	alist[0]      = make_alist("obj", ID_VAL, NULL, &obj);
	alist[1].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No object specified\n", func->name);
		return (NULL);
	}

	n = V_DSIZE(obj);
	for (x = n; (x & 1) == 0; x >>= 1)
		;
	if (x != 1) {
		parse_error("dimension not a power of 2. Use version 0.\n");
		return (NULL);
	}

	in = (double*)calloc(n, sizeof(double));

	for (i = 0; i < n; i++) {
		in[i] = extract_double(obj, i);
	}

	if (func->fdata == (void*)1) {
		rdft(n, cos(M_PI / n), sin(M_PI / n), in);
	} else {
		rdft(n, cos(M_PI / n), -sin(M_PI / n), in);
		for (j = 0; j <= n - 1; j++) {
			in[j] *= 2.0 / n;
		}
	}

	return (newVal(BSQ, 1, n, 1, DV_DOUBLE, in));
}
示例#14
0
文件: ff_rice.c 项目: robwink/davinci
Var* ff_unrice(vfuncptr func, Var* arg)
{
	Var* obj = NULL;
	int x, y, z, nbytes, i, start, len;
	int bits;
	unsigned char* in;
	int header = 1;
	int npts;
	unsigned short sx = x, sy = y, sz = z;
	unsigned char sbits = bits;
	char hdr[4];
	int format;
	int count = 0;
	int l;
	unsigned char *cout, *out;
	short *sout, *tmp;

	Alist alist[4];
	alist[0]      = make_alist("object", ID_VAL, NULL, &obj);
	alist[1]      = make_alist("header", ID_VAL, NULL, &header);
	alist[2].name = NULL;

	if (parse_args(func, arg, alist) == 0) return NULL;

	if (obj == NULL) {
		parse_error("%s: No object or size specified", func->name);
		return NULL;
	}

	len   = V_DSIZE(obj);
	in    = V_DATA(obj);
	start = 0;

	if (header) {
		/* read our header */
		memcpy(hdr, in, 4);
		start += 4;

		if (memcmp(hdr, "RICE", 4) != 0) {
			parse_error("Not a rice header");
			return NULL;
		}

		memcpy(&sx, in + start, 2);
		start += 2;
		memcpy(&sy, in + start, 2);
		start += 2;
		memcpy(&sz, in + start, 2);
		start += 2;
		memcpy(&sbits, in + start, 1);
		start += 1;

		x      = sx;
		y      = sy;
		z      = sz;
		bits   = sbits;
		nbytes = (bits + 7) / 8;
		npts   = x;

		out   = calloc(x * y * z, nbytes);
		cout  = out;
		sout  = (short*)out;
		tmp   = (short*)calloc(x, 2);
		count = 0;

		while (start < len) {
			/*
			** we assume that each rice compressed packet is a length of X
			**
			** We should do some error checking here somewhere.
			*/
			l = rice_unauto(in + start, len, npts, bits, tmp);
			if (l <= 0) {
				parse_error("Bad return code");
				return NULL;
			}
			for (i = 0; i < npts; i++) {
				if (nbytes == 1) {
					cout[i + count * x] = tmp[i];
				} else {
					sout[i + count * x] = tmp[i];
				}
			}
			start += l;
			count++;
		}
		format = (nbytes == 1 ? DV_UINT8 : (nbytes == 2 ? DV_INT16 : DV_INT32));
		return newVal(BSQ, x, y, z, format, out);
	} else {
		parse_error("Data without header not supported yet.");
		return NULL;
	}
}
示例#15
0
文件: ff_rice.c 项目: robwink/davinci
Var* ff_rice(vfuncptr func, Var* arg)
{
	Var* obj = NULL;
	int x, y, z, nbytes, i, j, k, pos, start, len;
	short* in;
	unsigned char* out;
	int header = 1;
	int bits   = -1;

	Alist alist[4];
	alist[0]      = make_alist("object", ID_VAL, NULL, &obj);
	alist[1]      = make_alist("header", DV_INT32, NULL, &header);
	alist[2]      = make_alist("bits", DV_INT32, NULL, &bits);
	alist[3].name = NULL;

	if (parse_args(func, arg, alist) == 0) return NULL;

	if (obj == NULL) {
		parse_error("%s: No object or size specified", func->name);
		return NULL;
	}

	x                    = GetX(obj);
	y                    = GetY(obj);
	z                    = GetZ(obj);
	nbytes               = NBYTES(V_FORMAT(obj));
	start                = 0;
	if (bits == -1) bits = nbytes * 8;

	if (x > 4096) {
		parse_error("%s: Max buffer length is 4096 elements", func->name);
		return NULL;
	}
	if (nbytes > 2) {
		parse_error("Only able to compress 2 byte words or less");
		return NULL;
	}
	if (nbytes == 2 && x > 511) {
		parse_error("Too many values in a row.  Split this up.");
		return NULL;
	}

	in  = calloc(x, 2);
	out = calloc(x * y * z, 2 * nbytes);

	if (header) {
		unsigned short sx = x, sy = y, sz = z;
		unsigned char sbits = bits;

		/* write out header */
		if (x > 65535 || y > 65535 || z > 65535) {
			parse_error("data block too big for header");
			return NULL;
		}

		memcpy(out, "RICE", 4);
		start += 4;
		memcpy(out + start, &sx, 2);
		start += 2;
		memcpy(out + start, &sy, 2);
		start += 2;
		memcpy(out + start, &sz, 2);
		start += 2;
		memcpy(out + start, &sbits, 1);
		start += 1;
	}

	for (k = 0; k < z; k++) {
		for (j = 0; j < y; j++) {
			/*
			** Pack a temporary array with a whole line
			*/
			for (i = 0; i < x; i++) {
				pos   = cpos(i, j, k, obj);
				in[i] = extract_int(obj, pos);
			}
			len = rice_auto(in, x, bits, out + start, 0);
			start += (len + 7) / 8;
		}
	}
	return newVal(BSQ, start, 1, 1, DV_UINT8, out);
}
示例#16
0
Var* ff_interp2d(vfuncptr func, Var* arg)
{

	Var* xdata = NULL;                    /* the orignial data */
	Var* ydata = NULL;                    /* the orignial data */
	Var* table = NULL;                    /* look up table */
	Var* out   = NULL;                    /* the output struture */
	int i, j;                             /* loop indices */
	float p1, p2;                         /* percentages */
	int xx, xy, xz, yx, yy, yz;           /* data size */
	float* wdata = NULL;                  /* working data */
	float sx = 1, dx = 1, sy = 1, dy = 1; /* start and delta values */
	float tvx, tvy;                       /* data values */
	int xi, yi;                           /* new x and y positions */
	float tv1, tv2;                       /* temporary values */

	Alist alist[8];
	alist[0]      = make_alist("table", ID_VAL, NULL, &table);
	alist[1]      = make_alist("xdata", ID_VAL, NULL, &xdata);
	alist[2]      = make_alist("ydata", ID_VAL, NULL, &ydata);
	alist[3]      = make_alist("startx", DV_FLOAT, NULL, &sx);
	alist[4]      = make_alist("deltax", DV_FLOAT, NULL, &dx);
	alist[5]      = make_alist("starty", DV_FLOAT, NULL, &sy);
	alist[6]      = make_alist("deltay", DV_FLOAT, NULL, &dy);
	alist[7].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (table == NULL) {
		parse_error("\ninterp2d()- Thu Apr 27 16:20:31 MST 2006");
		parse_error("Bilinear interpolation algorithm");
		parse_error("\nInputs and Outputs:");
		parse_error("table - table of values of a standard delta value for each axis");
		parse_error("xdata - the x data to interpolate");
		parse_error("ydata - the y data to interpolate");
		parse_error("startx - starting x value for the table");
		parse_error("deltax - delta  x value for the table");
		parse_error("starty - starting y value for the table");
		parse_error("deltay - delta y value for the table");
		parse_error("Returns a 1 d, array the size of x and y data\n");
		parse_error("c.edwards");
		return (NULL);
	}

	/*size of xdata*/
	xx = GetX(xdata);
	xy = GetY(xdata);
	xz = GetZ(xdata);

	/*size of ydata*/
	yx = GetX(ydata);
	yy = GetY(ydata);
	yz = GetZ(ydata);

	/*error handling, they must be the same size and one band*/
	if (xx != yx || xy != yy || xz != 1 || yz != 1) {
		parse_error("\nThe x and y data must have the same dimensions and only one band\n");
		return NULL;
	}

	/*memory allocation*/
	wdata = (float*)calloc((size_t)xx * (size_t)xy * 1, sizeof(float));

	for (i = 0; i < xx; i += 1) {
		for (j = 0; j < xy; j += 1) {

			/*extract values from original data*/
			tvx = extract_float(xdata, cpos(i, j, 0, xdata));
			tvy = extract_float(ydata, cpos(i, j, 0, ydata));

			/*apply start and delta to the extracted values*/
			tvx = (tvx - sx) / dx;
			tvy = (tvy - sy) / dy;

			/*calculate percentages */
			p1 = (float)(tvx - floor(tvx));
			p2 = (float)(tvy - floor(tvy));
			xi = (int)floor(tvx);
			yi = (int)floor(tvy);

			if (xi > GetX(table) || yi > GetY(table) || xi < 0 || yi < 0) {
				parse_error("Your interpolation values fall outside the range of the table\n");
				return (NULL);
			}

			/*   apply the bilinear interpolation algorithm                  **
			**   val=(f(1,1)*(1-p1)+f(2,1)*p1)*(1-p2)+(f(1,2)*(1-p1)+f(2,2)*p1)*p2    **
			*/

			tv1 = (extract_float(table, cpos(xi, yi, 0, table)) * (1 - p1) +
			       extract_float(table, cpos(xi + 1, yi, 0, table)) * (p1)) *
			      (1 - p2);
			tv2 = (extract_float(table, cpos(xi, yi + 1, 0, table)) * (1 - p1) +
			       extract_float(table, cpos(xi + 1, yi + 1, 0, table)) * (p1)) *
			      (p2);
			wdata[(size_t)xx * (size_t)j + (size_t)i] = (float)(tv1 + tv2);
		}
	}
	out = newVal(BSQ, xx, xy, 1, DV_FLOAT, wdata);
	return out;
}
示例#17
0
Var* ff_radial_symmetry3(vfuncptr func, Var* arg)
{
	Var* obj     = NULL;
	float ignore = FLT_MIN;
	int width = 0, height = 0;
	int end   = 1;
	Var* rval = NULL;
	int x, y;
	float* out;
	Window* w;

	int *distance, d;
	int dx, dy;
	double ssxx, ssyy, ssxy;
	int i, j, p, q, r;
	int h2, w2;
	float v1, v2;
	int total;
	float *r1, *r2;
	int start = 0, step = 1;

	struct dstore {
		double sumx;
		double sumy;
		double sumxx;
		double sumyy;
		double sumxy;
		int count;
	} * accum, *a1, *a2;

	Alist alist[6];
	alist[0]      = make_alist("object", ID_VAL, NULL, &obj);
	alist[1]      = make_alist("size", DV_INT32, NULL, &end);
	alist[2]      = make_alist("ignore", DV_FLOAT, NULL, &ignore);
	alist[3]      = make_alist("start", DV_INT32, NULL, &start);
	alist[4]      = make_alist("step", DV_INT32, NULL, &step);
	alist[5].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No object specified\n", func->name);
		return (NULL);
	}

	if (end <= 0) {
		parse_error("%s: Invalid end value specified\n", func->name);
		return (NULL);
	}

	x = GetX(obj);
	y = GetY(obj);

	width  = end * 2 + 1;
	height = end * 2 + 1;
	// width = end;
	// height = end;
	w = create_window(width, height, DV_FLOAT);

	/* cache some frequently used computed values */
	h2    = height / 2;
	w2    = width / 2;
	total = width * height;

	/* precompute the distance of every point in a given sized window
	** to the center of the window.
	**
	** In theory, this only needs to be 1 quadrant of the window, but that's
	** too hard to bookkeep, and doesn't save much.
	*/
	distance = calloc(total, sizeof(int));
	for (i = 0; i < width; i++) {
		dx = i - w2;
		for (j = 0; j < height; j++) {
			dy                      = j - h2;
			distance[i + j * width] = floor(sqrt(dx * dx + dy * dy));
			/* precheck for values outside the largest circle */
			if (distance[i + j * width] > end) distance[i + j * width] = 0;
		}
	}

	out   = calloc((size_t)x * (size_t)y * (size_t)((end - start) / step + 1), sizeof(float));
	rval  = newVal(BSQ, x, y, ((end - start) / step + 1), DV_FLOAT, out);
	accum = calloc(end + 1, sizeof(struct dstore));

	/* run a window over every pixel and do the math */
	for (i = 0; i < x; i += 1) {
		load_window(w, obj, i, 0, ignore);
		for (j = 0; j < y; j += 1) {
			if (j) roll_window(w, obj, i, j, ignore);

			v1 = ((float**)w->row)[h2][w2];

			if (v1 == ignore) {
				continue;
			}

			memset(accum, 0, (end + 1) * sizeof(struct dstore));

			/*
			** pick a pair of opposing points, and add them to the
			** accumulator for their given distance.  We'll sum all the
			** accumulators later for a complete result from dist 1..N.
			*/

			for (q = 0; q <= h2; q++) {
				/* one of the double-derefs moved to here for speed */
				r1 = ((float**)w->row)[q];
				r2 = ((float**)w->row)[(height - 1) - q];
				for (p = 0; p < width; p++) {
					if (q == h2 && p == w2) {
						/* We only run the window up to the center point  */
						break;
					}

					v1 = r1[p];
					v2 = r2[(width - 1) - p];

					if (v1 == ignore || v2 == ignore) continue;

					if ((d = distance[p + q * width]) != 0) {
						a1 = &(accum[d]);
						a1->sumx += v1;
						a1->sumy += v2;
						a1->sumxx += v1 * v1;
						a1->sumyy += v2 * v2;
						a1->sumxy += v1 * v2;
						a1->count++;
					}
				}
			}

			/* now compute correlation from per-radii accumulators */
			for (r = 1; r <= end; r++) {
				/* sum the values in preceeding bins */
				a1 = &accum[r - 1];
				a2 = &accum[r];
				if (a1->count) {
					a2->sumx += a1->sumx;
					a2->sumy += a1->sumy;
					a2->sumxx += a1->sumxx;
					a2->sumyy += a1->sumyy;
					a2->sumxy += a1->sumxy;
					a2->count += a1->count;
				}
				// Don't bother if at least 1/2 the box isn't ignore values
				// this is M_PI_4 because we're only counting half the pixels
				// to begin with.
				if ((r - start) % step == 0) {
					if (a2->count > r * r * M_PI_4) {
						double c = a2->count;
						ssxx     = a2->sumxx - a2->sumx * a2->sumx / c;
						ssyy     = a2->sumyy - a2->sumy * a2->sumy / c;
						ssxy     = a2->sumxy - a2->sumx * a2->sumy / c;
						if (ssxx != 0 && ssyy != 0) {
							out[cpos(i, j, ((r - start) / step), rval)] =
							    sqrt(ssxy * ssxy / (ssxx * ssyy));
						}
					}
				}
			}
		}
		printf("%d/%d\r", i, x);
		fflush(stdout);
	}

	free_window(w);
	free(distance);
	free(accum);

	return (rval);
}
示例#18
0
Var* ff_radial_symmetry(vfuncptr func, Var* arg)
{
	Var *obj = NULL, *rval = NULL;
	float ignore = FLT_MIN;
	float* out   = NULL;
	int x, y, z, i, j, k;
	int size   = 10;
	int xdelta = 0.0, ydelta = 0.0;
	float* line = NULL;
	int all     = 0;
	int first   = 1;
	Window* w;

	Alist alist[9];
	alist[0]      = make_alist("object", ID_VAL, NULL, &obj);
	alist[1]      = make_alist("size", DV_INT32, NULL, &size);
	alist[2]      = make_alist("xdelta", DV_INT32, NULL, &xdelta);
	alist[3]      = make_alist("ydelta", DV_INT32, NULL, &ydelta);
	alist[4]      = make_alist("ignore", DV_FLOAT, NULL, &ignore);
	alist[5]      = make_alist("all", DV_INT32, NULL, &all);
	alist[6]      = make_alist("first", DV_INT32, NULL, &first);
	alist[7].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No object specified\n", func->name);
		return (NULL);
	}

	if (size < 0) {
		parse_error("%s: Invalid size specified\n", func->name);
		return (NULL);
	}

	x = GetX(obj);
	y = GetY(obj);
	z = GetZ(obj);
	w = create_window(size, size, DV_FLOAT);

	if (all) {
		size_t n = (size_t)x * (size_t)y * (size_t)(size - first + 1);
		out      = (float*)calloc(n, sizeof(float));
		rval     = newVal(BSQ, x, y, (size - first + 1), DV_FLOAT, out);
		line     = (float*)calloc(size, sizeof(float));
	} else {
		size_t n = (size_t)x * (size_t)y;
		out      = (float*)calloc(n, sizeof(float));
		rval     = newVal(BSQ, x, y, 1, DV_FLOAT, out);
		line     = NULL;
	}

	for (i = 0; i < x; i += 1) {
		load_window(w, obj, i, 0, ignore);
		for (j = 0; j < y; j += 1) {
			if (j) roll_window(w, obj, i, j, ignore);

			out[cpos(i, j, 0, rval)] =
			    radial_symmetry(w->row, size, size, ignore, xdelta, ydelta, size, line);
			if (all) {
				for (k = first; k <= size; k++) {
					out[cpos(i, j, k - first, rval)] = line[k];
				}
			}
		}
	}

	free_window(w);
	return (rval);
}
示例#19
0
Var* ff_boxfilter(vfuncptr func, Var* arg)
{
	Var* v = NULL;
	Var *rcount, *rmean, *rs, *rn, *rsigma;
	Var* a;
	int x         = 0;
	int y         = 0;
	int z         = 0;
	int size      = 0;
	double ignore = FLT_MIN;
	int verbose   = 0;

	Alist alist[8];
	alist[0]      = make_alist("obj", ID_VAL, NULL, &v);
	alist[1]      = make_alist("x", DV_INT32, NULL, &x);
	alist[2]      = make_alist("y", DV_INT32, NULL, &y);
	alist[3]      = make_alist("z", DV_INT32, NULL, &z);
	alist[4]      = make_alist("size", DV_INT32, NULL, &size);
	alist[5]      = make_alist("ignore", DV_DOUBLE, NULL, &ignore);
	alist[6]      = make_alist("verbose", DV_INT32, NULL, &verbose);
	alist[7].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);
	if (v == NULL) {
		parse_error("%s(): No object specified\n", func->name);
		return (NULL);
	}
	if (x && y && size) {
		parse_error("%s(): Specify either size or (x, y, z), not both", func->name);
		return (NULL);
	}
	if (x == 0) x    = 1;
	if (y == 0) y    = 1;
	if (z == 0) z    = 1;
	if (size != 0) x = y = size;

	if (x == 0 || y == 0) {
		parse_error("%s(): No x or y specified", func->name);
		return (NULL);
	}

	init_sums(v, x, y, z, &rn, &rs, &rcount, &rmean, &rsigma, ignore);

	if (verbose) {
		a = new_struct(0);
		add_struct(a, "count", rcount);
		add_struct(a, "mean", rmean);
		add_struct(a, "sigma", rsigma);
		add_struct(a, "n", rn);
		add_struct(a, "s", rs);
		return (a);
	} else {
		mem_claim(rcount);
		free_var(rcount);
		mem_claim(rn);
		free_var(rn);
		mem_claim(rs);
		free_var(rs);
		return (rmean);
	}
}
示例#20
0
Var* ff_write(vfuncptr func, Var* arg)
{

	Var* ob         = NULL;
	char* filename  = NULL;
	char* title     = NULL;
	char* type      = NULL;
	char* separator = NULL; /* for csv */
	int header      = 0;    /* for csv */
	int force       = 0;    /* Force file overwrite */
	int hdf_old     = 0;    // write hdf file backward like davinci used to
	unsigned short iom_type_idx, iom_type_found;

	Alist alist[9];
	alist[0]      = make_alist("object", ID_UNK, NULL, &ob);
	alist[1]      = make_alist("filename", ID_STRING, NULL, &filename);
	alist[2]      = make_alist("type", ID_ENUM, NULL, &type);
	alist[3]      = make_alist("title", ID_STRING, NULL, &title);
	alist[4]      = make_alist("force", DV_INT32, NULL, &force);
	alist[5]      = make_alist("separator", ID_STRING, NULL, &separator);
	alist[6]      = make_alist("header", DV_INT32, NULL, &header);
	alist[7]      = make_alist("hdf_old", DV_INT32, NULL, &hdf_old);
	alist[8].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	/**
	** Make sure user specified an object
	**/
	if (ob == NULL) {
		parse_error("%s: No object specified.", func->name);
		return (NULL);
	}

	/**
	** get filename.  Verify type
	**/
	if (filename == NULL) {
		parse_error("No filename specified.");
		return (NULL);
	}
	filename = dv_locate_file(filename);

	if (type == NULL) {
		parse_error("No type specified.");
		return (NULL);
	}

	/*
	** Get title string
	*/
	if (title == NULL) {
		title = (char*)"DV data product";
	}

	/* Check type against list of types supported by iomedley. */

	iom_type_idx = iom_type_found = 0;

	while (iom_filetypes[iom_type_idx]) {
		if (!strcasecmp(type, iom_filetypes[iom_type_idx])) {
			iom_type_found = 1;
			break;
		}
		iom_type_idx++;
	}

	if (iom_type_found)
		dv_WriteIOM(ob, filename, type, force);
	else if (!strcasecmp(type, "raw"))
		dv_WriteRaw(ob, filename, force);
	else if (!strcasecmp(type, "vicar"))
		dv_WriteVicar(ob, filename, force);
	else if (!strcasecmp(type, "grd"))
		dv_WriteGRD(ob, filename, force, title, (char*)"davinci");
	/*    else if (!strcasecmp(type, "pnm"))    dv_WritePNM(ob, filename, force); */
	else if (!strcasecmp(type, "pgm"))
		dv_WritePGM(ob, filename, force);
	else if (!strcasecmp(type, "ppm"))
		dv_WritePPM(ob, filename, force);
	else if (!strcasecmp(type, "ascii"))
		WriteAscii(ob, filename, force);
	else if (!strcasecmp(type, "csv"))
		dv_WriteCSV(ob, filename, separator, header, force);
	else if (!strcasecmp(type, "ers"))
		dv_WriteERS(ob, filename, force);
	else if (!strcasecmp(type, "imath"))
		dv_WriteIMath(ob, filename, force);
	else if (!strcasecmp(type, "isis"))
		dv_WriteISIS(ob, filename, force, title);
	else if (!strcasecmp(type, "envi"))
		dv_WriteENVI(ob, filename, force);
	else if (!strcasecmp(type, "specpr")) {
		if (!force && file_exists(filename)) {
			parse_error("File %s already exists.\n", filename);
			return NULL;
		}
		WriteSpecpr(ob, filename, title);
	}

/*
** Below here are optional packages
*/
#ifdef HAVE_LIBHDF5
	else if (!strcasecmp(type, "hdf")) {
		struct stat statbuf;
		if (!force && !stat(filename, &statbuf)) {
			parse_error("File %s already exists.\n", filename);
			return NULL;
		}

		/* force ? */
		WriteHDF5(-1, filename, ob, hdf_old);
	}
#endif

#ifdef BUILD_MODULE_SUPPORT
	else if (iomod_handler_for_type(type))
		write_to_io_module(ob, filename, type, force);
#endif

#if 0
#ifdef HAVE_LIBMAGICK
	// else if (dvio_ValidGfx(type, GFX_type))    dv_WriteGFX_Image(ob, filename, force, GFX_type);
	else if (1) paramdvWriteImage(ob, filename, type, force);
#endif
#endif
	else {
		sprintf(error_buf, "Unrecognized type: %s", type);
		parse_error(NULL);
		return (NULL);
	}

	free(filename);

	return (NULL);
}
示例#21
0
Var* ff_list_dv_modules(vfuncptr func, Var* args)
{
	char* mod_name;
	Var* mod_var;
	Var* mlst;
	int i, n;
	char** mnlst;
	int len;
	dvModule* m;
	Var* v            = NULL;
	char* module_name = NULL;

	Alist alist[2]; /* arguments list */

	alist[0]      = make_alist("module", ID_STRING, NULL, &module_name);
	alist[1].name = NULL;

	if (parse_args(func, args, alist) == 0) {
		parse_error("%s(): argument parsing failed.", func->name);
		return NULL;
	}

	if (module_name != NULL) {
		// Find all the stuff in the specified module.

		// Locate the module
		if (Narray_find(loaded_modules, module_name, (void*)&v) != -1) {
			char* func_name;
			m     = &(V_MODULE(v));
			n     = Narray_count(m->functions);
			mnlst = (char**)calloc(n, sizeof(char*));
			for (i = 0; i < n; i++) {
				Narray_get(m->functions, i, &func_name, NULL);
				mnlst[i] = strdup(func_name);
			}
			return (newText(n, mnlst));
		} else {
			parse_error("Unable to find module %s\n", module_name);
			return (NULL);
		}
	} else {
		n     = Narray_count(loaded_modules);
		mnlst = (char**)calloc(n, sizeof(char*));
		if (mnlst == NULL) {
			return NULL;
		}

		for (i = 0; i < n; i++) {
			Narray_get(loaded_modules, i, &mod_name, (void**)&mod_var);
			m = &V_MODULE(mod_var);

			len = strlen(mod_name) + 1;
			if (m->ver) {
				len += strlen(m->ver) + 1;
			}

			mnlst[i] = (char*)calloc(len, sizeof(char));

			if (mnlst[i] == NULL) { /* memory failure */
				parse_error("Mem allocation error.\n");
				while (--i >= 0) {
					free(mnlst[i]);
				}
				free(mnlst);
				return NULL;
			}

			if (m->ver && strcmp(m->ver, "") == 0) {
				/* include version */
				sprintf(mnlst[i], "%s.%s", mod_name, m->ver);
			} else {
				/* no version associated with the module */
				sprintf(mnlst[i], "%s", mod_name);
			}
		}

		mlst = newText(n, mnlst);
		return mlst;
	}
}
示例#22
0
文件: ff_warp.c 项目: robwink/davinci
Var* ff_warp(vfuncptr func, Var* arg)
{
	Var *obj = NULL, *xm = NULL, *oval;
	float ignore = FLT_MIN;
	int i, j;
	float* out;
	int x, y, n;
	int grow = 0;
	float m[9];
	float* minverse;
	float xmax, xmin, ymax, ymin;
	float v[3];
	int dsize;
	const char* options[] = {"nearest", "bilinear", 0};
	char* interp          = NULL;

	float (*interp_f)(float, float, Var*, float);

	Alist alist[6];

	alist[0]      = make_alist("object", ID_VAL, NULL, &obj);
	alist[1]      = make_alist("matrix", ID_VAL, NULL, &xm);
	alist[2]      = make_alist("ignore", DV_FLOAT, NULL, &ignore);
	alist[3]      = make_alist("grow", DV_INT32, NULL, &grow);
	alist[4]      = make_alist("interp", ID_ENUM, options, &interp);
	alist[5].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	if (obj == NULL) {
		parse_error("%s: No object specified\n", func->name);
		return (NULL);
	}
	if (ignore == FLT_MIN) ignore = -32768;

	x = GetX(obj);
	y = GetY(obj);
	n = V_SIZE(xm)[2];

	for (j = 0; j < 3; j++) {
		for (i = 0; i < 3; i++) {
			m[i + j * 3] = extract_float(xm, cpos(i, j, 0, xm));
		}
	}

	xmin = ymin = 0;
	xmax        = x;
	ymax        = y;

	if (grow) {
		/* figure out the size of the output array */
		float* out;
		minverse = m_inverse(m);

		out  = vxm(new_v(0, 0), minverse);
		xmin = out[0];
		xmax = out[0];
		ymin = out[1];
		ymax = out[1];
		free(out);

		out  = vxm(new_v(x, 0), minverse);
		xmin = min(xmin, out[0]);
		xmax = max(xmax, out[0]);
		ymin = min(ymin, out[1]);
		ymax = max(ymax, out[1]);
		free(out);

		out  = vxm(new_v(0, y), minverse);
		xmin = min(xmin, out[0]);
		xmax = max(xmax, out[0]);
		ymin = min(ymin, out[1]);
		ymax = max(ymax, out[1]);
		free(out);

		out  = vxm(new_v(x, y), minverse);
		xmin = min(xmin, out[0]);
		xmax = max(xmax, out[0]);
		ymin = min(ymin, out[1]);
		ymax = max(ymax, out[1]);
		free(out);

		xmax = ceil(xmax);
		xmin = floor(xmin);
		ymax = ceil(ymax);
		ymin = floor(ymin);

		printf("new array corners:\n");
		printf("  %fx%f , %fx%f\n", xmin, ymin, xmax, ymax);
	}

	if (interp == NULL || !strcmp(interp, "nearest")) {
		interp_f = interp_nn;
	} else if (!strcmp(interp, "bilinear")) {
		interp_f = interp_bilinear;
	} else {
		parse_error("Invalid interpolation function\n");
		return (NULL);
	}

	dsize = (xmax - xmin) * (ymax - ymin);
	out   = calloc(dsize, sizeof(float));
	oval  = newVal(BSQ, xmax - xmin, ymax - ymin, 1, DV_FLOAT, out);

	for (j = ymin; j < ymax; j++) {
		for (i = xmin; i < xmax; i++) {
			v[0] = i + 0.5;
			v[1] = j + 0.5;
			v[2] = 1;
			vxm(v, m);
			out[cpos((int)(i - xmin), (int)(j - ymin), 0, oval)] = interp_f(v[0], v[1], obj, ignore);
		}
	}
	return (oval);
}