static vx_status VX_CALLBACK vxFindWarpOutputValidator(vx_node node, vx_uint32 index, vx_meta_format_t *ptr) { vx_status status = VX_ERROR_INVALID_PARAMETERS; if (index == 2) { vx_parameter param = vxGetParameterByIndex(node, index); if (param) { vx_matrix matrix; vxQueryParameter(param, VX_PARAMETER_ATTRIBUTE_REF, &matrix, sizeof(matrix)); if (matrix) { vx_enum data_type = 0; vx_size rows = 0ul, columns = 0ul; vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_TYPE, &data_type, sizeof(data_type)); vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_ROWS, &rows, sizeof(rows)); vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_COLUMNS, &columns, sizeof(columns)); if ((data_type == VX_TYPE_FLOAT32) && (columns == 3) && (rows == 3)) { status = VX_SUCCESS; } vxReleaseMatrix(&matrix); } vxReleaseParameter(¶m); } } return status; }
vx_status vxMatrixInverse(vx_matrix input, vx_matrix output) { vx_size ci, co; vx_size ri, ro; vx_enum ti, to; vx_status status = VX_SUCCESS; status |= vxQueryMatrix(input, VX_MATRIX_ATTRIBUTE_COLUMNS, &ci, sizeof(ci)); status |= vxQueryMatrix(input, VX_MATRIX_ATTRIBUTE_ROWS, &ri, sizeof(ri)); status |= vxQueryMatrix(input, VX_MATRIX_ATTRIBUTE_TYPE, &ti, sizeof(ti)); status |= vxQueryMatrix(output, VX_MATRIX_ATTRIBUTE_COLUMNS, &co, sizeof(co)); status |= vxQueryMatrix(output, VX_MATRIX_ATTRIBUTE_ROWS, &ro, sizeof(ro)); status |= vxQueryMatrix(output, VX_MATRIX_ATTRIBUTE_TYPE, &to, sizeof(to)); if (status != VX_SUCCESS) return status; if (ci != co || ri != ro || ci != ri) return VX_ERROR_INVALID_DIMENSION; if (ti != to) return VX_ERROR_INVALID_TYPE; if (ti == VX_TYPE_FLOAT32) { vx_float32 in[ri][ci]; vx_float32 ou[ro][co]; vxAccessMatrix(input, in); vxAccessMatrix(output, NULL); vxh_matrix_inverse_f32(ci, ri, ou, in); vxCommitMatrix(output, ou); vxCommitMatrix(input, NULL); } /*! \bug implement integer */ return status; }
vx_status vxMatrixTrace(vx_matrix matrix, vx_scalar trace) { vx_size columns = 0u; vx_size rows = 0u; vx_status status = VX_SUCCESS; vx_enum mtype = VX_TYPE_INVALID, stype = VX_TYPE_INVALID; status |= vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_COLUMNS, &columns, sizeof(columns)); status |= vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_ROWS, &rows, sizeof(rows)); status |= vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_TYPE, &mtype, sizeof(mtype)); status |= vxQueryScalar(trace, VX_SCALAR_ATTRIBUTE_TYPE, &stype, sizeof(stype)); if (status != VX_SUCCESS) return VX_ERROR_INVALID_REFERENCE; if (mtype == VX_TYPE_INVALID || mtype != stype) return VX_ERROR_INVALID_TYPE; if (columns == 0 || columns != rows) return VX_ERROR_INVALID_DIMENSION; if (stype == VX_TYPE_INT32) { vx_int32 mat[rows][columns]; vx_int32 t = 0; status |= vxAccessScalarValue(trace, NULL); status |= vxAccessMatrix(matrix, mat); t = vxh_matrix_trace_i32(columns, rows, mat); status |= vxCommitMatrix(matrix, NULL); status |= vxCommitScalarValue(trace, &t); } else if (stype == VX_TYPE_FLOAT32) { vx_float32 mat[rows][columns]; vx_float32 t = 0.0f; status |= vxAccessScalarValue(trace, NULL); status |= vxAccessMatrix(matrix, mat); t = vxh_matrix_trace_f32(columns, rows, mat); status |= vxCommitMatrix(matrix, NULL); status |= vxCommitScalarValue(trace, &t); } return status; }
vx_status vxSetAffineRotationMatrix(vx_matrix matrix, vx_float32 angle, vx_float32 scale, vx_float32 center_x, vx_float32 center_y) { vx_status status = VX_FAILURE; vx_float32 mat[3][2]; vx_size columns = 0ul, rows = 0ul; vx_enum type = 0; vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_COLUMNS, &columns, sizeof(columns)); vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_ROWS, &rows, sizeof(rows)); vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_TYPE, &type, sizeof(type)); if ((columns == 2) && (rows == 3) && (type == VX_TYPE_FLOAT32)) { status = vxAccessMatrix(matrix, mat); if (status == VX_SUCCESS) { vx_float32 radians = (angle / 360.0f) * (VX_TAU); vx_float32 a = scale * cos(radians); vx_float32 b = scale * sin(radians); mat[0][0] = a; mat[1][0] = b; mat[2][0] = ((1.0f - a) * center_x) - (b * center_y); mat[0][1] = -b; mat[1][1] = a; mat[2][1] = (b * center_y) + ((1.0f - a) * center_y); status = vxCommitMatrix(matrix, mat); } } else { vxAddLogEntry(matrix, status, "Failed to set affine matrix due to type or dimension mismatch!\n"); } return status; }
static vx_status VX_CALLBACK vxMatrixModifyInputValidator(vx_node node, vx_uint32 index) { vx_status status = VX_ERROR_INVALID_PARAMETERS; if (index == 0) { vx_parameter param = vxGetParameterByIndex(node, index); if (param) { vx_matrix matrix; vxQueryParameter(param, VX_PARAMETER_ATTRIBUTE_REF, &matrix, sizeof(matrix)); if (matrix) { vx_enum data_type = 0; vx_size rows = 0ul, columns = 0ul; vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_TYPE, &data_type, sizeof(data_type)); vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_ROWS, &rows, sizeof(rows)); vxQueryMatrix(matrix, VX_MATRIX_ATTRIBUTE_COLUMNS, &columns, sizeof(columns)); if ((data_type == VX_TYPE_FLOAT32) && (columns == 3) && (rows == 3)) { status = VX_SUCCESS; } vxReleaseMatrix(&matrix); } vxReleaseParameter(¶m); } } if (index == 1 || index == 2) { vx_parameter param = vxGetParameterByIndex(node, index); if (param) { vx_scalar scalar = 0; status = vxQueryParameter(param, VX_PARAMETER_ATTRIBUTE_REF, &scalar, sizeof(scalar)); if ((status == VX_SUCCESS) && (scalar)) { vx_enum type = VX_TYPE_INVALID; vxQueryScalar(scalar, VX_SCALAR_ATTRIBUTE_TYPE, &type, sizeof(type)); if (type == VX_TYPE_UINT32) { status = VX_SUCCESS; } else { status = VX_ERROR_INVALID_TYPE; } vxReleaseScalar(&scalar); } vxReleaseParameter(¶m); } } if (index == 3) { vx_parameter param = vxGetParameterByIndex(node, index); if (param) { vx_scalar scalar = 0; status = vxQueryParameter(param, VX_PARAMETER_ATTRIBUTE_REF, &scalar, sizeof(scalar)); if ((status == VX_SUCCESS) && (scalar)) { vx_enum type = VX_TYPE_INVALID; vxQueryScalar(scalar, VX_SCALAR_ATTRIBUTE_TYPE, &type, sizeof(type)); if (type == VX_TYPE_FLOAT32) { status = VX_SUCCESS; } else { status = VX_ERROR_INVALID_TYPE; } vxReleaseScalar(&scalar); } vxReleaseParameter(¶m); } } return status; }
// nodeless version of NonLinearFilter kernel vx_status vxNonLinearFilter(vx_scalar function, vx_image src, vx_matrix mask, vx_image dst, vx_border_t *border) { vx_uint32 y, x; void *src_base = NULL; void *dst_base = NULL; vx_imagepatch_addressing_t src_addr, dst_addr; vx_rectangle_t rect; vx_uint32 low_x = 0, low_y = 0, high_x, high_y; vx_uint8 m[C_MAX_NONLINEAR_DIM * C_MAX_NONLINEAR_DIM]; vx_uint8 v[C_MAX_NONLINEAR_DIM * C_MAX_NONLINEAR_DIM]; vx_status status = vxGetValidRegionImage(src, &rect); status |= vxAccessImagePatch(src, &rect, 0, &src_addr, &src_base, VX_READ_ONLY); status |= vxAccessImagePatch(dst, &rect, 0, &dst_addr, &dst_base, VX_WRITE_ONLY); vx_enum func = 0; status |= vxCopyScalar(function, &func, VX_READ_ONLY, VX_MEMORY_TYPE_HOST); vx_size mrows, mcols; vx_enum mtype = 0; status |= vxQueryMatrix(mask, VX_MATRIX_ROWS, &mrows, sizeof(mrows)); status |= vxQueryMatrix(mask, VX_MATRIX_COLUMNS, &mcols, sizeof(mcols)); status |= vxQueryMatrix(mask, VX_MATRIX_TYPE, &mtype, sizeof(mtype)); vx_coordinates2d_t origin; status |= vxQueryMatrix(mask, VX_MATRIX_ORIGIN, &origin, sizeof(origin)); if ((mtype != VX_TYPE_UINT8) || (sizeof(m) < mrows * mcols)) status = VX_ERROR_INVALID_PARAMETERS; status |= vxCopyMatrix(mask, m, VX_READ_ONLY, VX_MEMORY_TYPE_HOST); if (status == VX_SUCCESS) { vx_size rx0 = origin.x; vx_size ry0 = origin.y; vx_size rx1 = mcols - origin.x - 1; vx_size ry1 = mrows - origin.y - 1; high_x = src_addr.dim_x; high_y = src_addr.dim_y; if (border->mode == VX_BORDER_UNDEFINED) { low_x += rx0; low_y += ry0; high_x -= rx1; high_y -= ry1; vxAlterRectangle(&rect, (vx_int32)rx0, (vx_int32)ry0, -(vx_int32)rx1, -(vx_int32)ry1); } for (y = low_y; y < high_y; y++) { for (x = low_x; x < high_x; x++) { vx_uint8 *dst = vxFormatImagePatchAddress2d(dst_base, x, y, &dst_addr); vx_int32 count = readMaskedRectangle_U8(src_base, &src_addr, border, VX_DF_IMAGE_U8, x, y, rx0, ry0, rx1, ry1, m, v); qsort(v, count, sizeof(vx_uint8), vx_uint8_compare); switch (func) { case VX_NONLINEAR_FILTER_MIN: *dst = v[0]; break; /* minimal value */ case VX_NONLINEAR_FILTER_MAX: *dst = v[count - 1]; break; /* maximum value */ case VX_NONLINEAR_FILTER_MEDIAN: *dst = v[count / 2]; break; /* pick the middle value */ } } } } status |= vxCommitImagePatch(src, NULL, 0, &src_addr, src_base); status |= vxCommitImagePatch(dst, &rect, 0, &dst_addr, dst_base); return status; }
int CVxParamMatrix::InitializeIO(vx_context context, vx_graph graph, vx_reference ref, const char * io_params) { // save reference object and get object attributes m_vxObjRef = ref; m_matrix = (vx_matrix)m_vxObjRef; ERROR_CHECK(vxQueryMatrix(m_matrix, VX_MATRIX_ATTRIBUTE_TYPE, &m_data_type, sizeof(m_data_type))); ERROR_CHECK(vxQueryMatrix(m_matrix, VX_MATRIX_ATTRIBUTE_COLUMNS, &m_columns, sizeof(m_columns))); ERROR_CHECK(vxQueryMatrix(m_matrix, VX_MATRIX_ATTRIBUTE_ROWS, &m_rows, sizeof(m_rows))); ERROR_CHECK(vxQueryMatrix(m_matrix, VX_MATRIX_ATTRIBUTE_SIZE, &m_size, sizeof(m_size))); // process I/O parameters if (*io_params == ':') io_params++; while (*io_params) { char ioType[64], fileName[256]; io_params = ScanParameters(io_params, "<io-operation>,<parameter>", "s,S", ioType, fileName); if (!_stricmp(ioType, "read")) { // read request syntax: read,<fileName>[,ascii|binary] m_fileNameRead.assign(RootDirUpdated(fileName)); m_fileNameForReadHasIndex = (m_fileNameRead.find("%") != m_fileNameRead.npos) ? true : false; m_readFileIsBinary = (m_fileNameRead.find(".txt") != m_fileNameRead.npos) ? false : true; while (*io_params == ',') { char option[64]; io_params = ScanParameters(io_params, ",ascii|binary", ",s", option); if (!_stricmp(option, "ascii")) { m_readFileIsBinary = false; } else if (!_stricmp(option, "binary")) { m_readFileIsBinary = true; } else ReportError("ERROR: invalid matrix read option: %s\n", option); } } else if (!_stricmp(ioType, "write")) { // write request syntax: write,<fileName>[,ascii|binary] m_fileNameWrite.assign(RootDirUpdated(fileName)); m_writeFileIsBinary = (m_fileNameWrite.find(".txt") != m_fileNameWrite.npos) ? false : true; while (*io_params == ',') { char option[64]; io_params = ScanParameters(io_params, ",ascii|binary", ",s", option); if (!_stricmp(option, "ascii")) { m_writeFileIsBinary = false; } else if (!_stricmp(option, "binary")) { m_writeFileIsBinary = true; } else ReportError("ERROR: invalid matrix write option: %s\n", option); } } else if (!_stricmp(ioType, "compare")) { // compare request syntax: compare,<fileName>[,ascii|binary][,err{<tolerance>}] m_fileNameCompare.assign(RootDirUpdated(fileName)); m_compareFileIsBinary = (m_fileNameCompare.find(".txt") != m_fileNameCompare.npos) ? false : true; while (*io_params == ',') { char option[64]; io_params = ScanParameters(io_params, ",ascii|binary|err{<tolerance>}", ",s", option); if (!_stricmp(option, "ascii")) { m_compareFileIsBinary = false; } else if (!_stricmp(option, "binary")) { m_compareFileIsBinary = true; } else if (!_strnicmp(option, "err{", 4)) { ScanParameters(&option[3], "{<tolerance>}", "{f}", &m_errTolerance); } else ReportError("ERROR: invalid matrix compare option: %s\n", option); } } else if (!_stricmp(ioType, "init")) { // write request syntax: init,{<value1>;<value2>;...<valueN>} NULLPTR_CHECK(m_bufForAccess = new vx_uint8[m_size]); vx_size index = 0; char fmt[3] = { '{', (m_data_type == VX_TYPE_FLOAT32) ? 'f' : 'd', 0 }; for (const char * s = fileName; *s && index < (m_columns * m_rows); fmt[0] = ';', index++) { if (m_data_type == VX_TYPE_INT32 || m_data_type == VX_TYPE_UINT8) { vx_uint32 value; s = ScanParameters(s, "<value>", fmt, &value); if (m_data_type == VX_TYPE_UINT8) ((vx_uint8 *)m_bufForAccess)[index] = (vx_uint8)value; else ((vx_int32 *)m_bufForAccess)[index] = value; } else if (m_data_type == VX_TYPE_FLOAT32) { s = ScanParameters(s, "<value>", fmt, &((vx_float32 *)m_bufForAccess)[index]); } else ReportError("ERROR: matrix init option not support for data_type of %s\n", GetVxObjectName()); } if (index < (m_columns * m_rows)) ReportError("ERROR: matrix init have too few values: %s\n", fileName); ERROR_CHECK(vxWriteMatrix(m_matrix, m_bufForAccess)); } else if (!_stricmp(ioType, "directive") && !_stricmp(fileName, "readonly")) { ERROR_CHECK(vxDirective((vx_reference)m_matrix, VX_DIRECTIVE_AMD_READ_ONLY)); } else if (!_stricmp(ioType, "ui") && !_strnicmp(fileName, "f", 1) && m_data_type == VX_TYPE_FLOAT32 && m_columns == 3 && m_rows == 3) { int id = 0; float valueR = 200.0f, valueInc = 0.5f; if (sscanf(&fileName[1], "%d,%g,%g", &id, &valueR, &valueInc) != 3) { printf("ERROR: invalid matrix UI configuration '%s'\n", fileName); return -1; } id--; GuiTrackBarInitializeMatrix((vx_reference)m_matrix, id, valueR, valueInc); GuiTrackBarProcessKey(0); // just initialize the matrix } else ReportError("ERROR: invalid matrix operation: %s\n", ioType); if (*io_params == ':') io_params++; else if (*io_params) ReportError("ERROR: unexpected character sequence in parameter specification: %s\n", io_params); } return 0; }