2012-10-24 17:19:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opj_includes.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LUP decomposition
|
|
|
|
*/
|
2012-11-15 14:13:36 +01:00
|
|
|
static OPJ_BOOL opj_lupDecompose(OPJ_FLOAT32 * matrix,
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_UINT32 * permutations,
|
|
|
|
OPJ_FLOAT32 * p_swap_area,
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 nb_compo);
|
2012-10-24 17:19:51 +02:00
|
|
|
/**
|
|
|
|
* LUP solving
|
|
|
|
*/
|
|
|
|
static void opj_lupSolve(OPJ_FLOAT32 * pResult,
|
|
|
|
OPJ_FLOAT32* pMatrix,
|
|
|
|
OPJ_FLOAT32* pVector,
|
|
|
|
OPJ_UINT32* pPermutations,
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 nb_compo,
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_FLOAT32 * p_intermediate_data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
*LUP inversion (call with the result of lupDecompose)
|
|
|
|
*/
|
|
|
|
static void opj_lupInvert ( OPJ_FLOAT32 * pSrcMatrix,
|
|
|
|
OPJ_FLOAT32 * pDestMatrix,
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 nb_compo,
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_UINT32 * pPermutations,
|
|
|
|
OPJ_FLOAT32 * p_src_temp,
|
|
|
|
OPJ_FLOAT32 * p_dest_temp,
|
|
|
|
OPJ_FLOAT32 * p_swap_area);
|
|
|
|
|
2012-10-29 15:35:11 +01:00
|
|
|
/*
|
|
|
|
==========================================================
|
|
|
|
Matric inversion interface
|
|
|
|
==========================================================
|
2012-10-24 17:19:51 +02:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Matrix inversion.
|
|
|
|
*/
|
2012-11-15 14:13:36 +01:00
|
|
|
OPJ_BOOL opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix,
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_FLOAT32 * pDestMatrix,
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 nb_compo)
|
2012-10-24 17:19:51 +02:00
|
|
|
{
|
|
|
|
OPJ_BYTE * l_data = 00;
|
2014-03-03 13:13:11 +01:00
|
|
|
OPJ_UINT32 l_permutation_size = nb_compo * (OPJ_UINT32)sizeof(OPJ_UINT32);
|
|
|
|
OPJ_UINT32 l_swap_size = nb_compo * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_UINT32 l_total_size = l_permutation_size + 3 * l_swap_size;
|
|
|
|
OPJ_UINT32 * lPermutations = 00;
|
|
|
|
OPJ_FLOAT32 * l_double_data = 00;
|
|
|
|
|
|
|
|
l_data = (OPJ_BYTE *) opj_malloc(l_total_size);
|
|
|
|
if (l_data == 0) {
|
|
|
|
return OPJ_FALSE;
|
|
|
|
}
|
|
|
|
lPermutations = (OPJ_UINT32 *) l_data;
|
|
|
|
l_double_data = (OPJ_FLOAT32 *) (l_data + l_permutation_size);
|
|
|
|
memset(lPermutations,0,l_permutation_size);
|
|
|
|
|
2012-11-13 16:16:35 +01:00
|
|
|
if(! opj_lupDecompose(pSrcMatrix,lPermutations,l_double_data,nb_compo)) {
|
2012-10-24 17:19:51 +02:00
|
|
|
opj_free(l_data);
|
|
|
|
return OPJ_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-11-13 16:16:35 +01:00
|
|
|
opj_lupInvert(pSrcMatrix,pDestMatrix,nb_compo,lPermutations,l_double_data,l_double_data + nb_compo,l_double_data + 2*nb_compo);
|
2012-10-24 17:19:51 +02:00
|
|
|
opj_free(l_data);
|
|
|
|
|
|
|
|
return OPJ_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-29 15:35:11 +01:00
|
|
|
/*
|
|
|
|
==========================================================
|
|
|
|
Local functions
|
|
|
|
==========================================================
|
2012-10-24 17:19:51 +02:00
|
|
|
*/
|
2012-11-15 14:13:36 +01:00
|
|
|
OPJ_BOOL opj_lupDecompose(OPJ_FLOAT32 * matrix,OPJ_UINT32 * permutations,
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_FLOAT32 * p_swap_area,
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 nb_compo)
|
2012-10-24 17:19:51 +02:00
|
|
|
{
|
|
|
|
OPJ_UINT32 * tmpPermutations = permutations;
|
|
|
|
OPJ_UINT32 * dstPermutations;
|
|
|
|
OPJ_UINT32 k2=0,t;
|
|
|
|
OPJ_FLOAT32 temp;
|
|
|
|
OPJ_UINT32 i,j,k;
|
|
|
|
OPJ_FLOAT32 p;
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 lLastColum = nb_compo - 1;
|
2014-03-03 13:13:11 +01:00
|
|
|
OPJ_UINT32 lSwapSize = nb_compo * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_FLOAT32 * lTmpMatrix = matrix;
|
|
|
|
OPJ_FLOAT32 * lColumnMatrix,* lDestMatrix;
|
|
|
|
OPJ_UINT32 offset = 1;
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 lStride = nb_compo-1;
|
2012-10-24 17:19:51 +02:00
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/*initialize permutations */
|
2012-11-13 16:16:35 +01:00
|
|
|
for (i = 0; i < nb_compo; ++i)
|
2012-10-24 17:19:51 +02:00
|
|
|
{
|
|
|
|
*tmpPermutations++ = i;
|
|
|
|
}
|
2012-10-25 08:58:30 +02:00
|
|
|
/* now make a pivot with colum switch */
|
2012-10-24 17:19:51 +02:00
|
|
|
tmpPermutations = permutations;
|
|
|
|
for (k = 0; k < lLastColum; ++k) {
|
|
|
|
p = 0.0;
|
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* take the middle element */
|
2012-10-24 17:19:51 +02:00
|
|
|
lColumnMatrix = lTmpMatrix + k;
|
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* make permutation with the biggest value in the column */
|
2012-11-13 16:16:35 +01:00
|
|
|
for (i = k; i < nb_compo; ++i) {
|
2012-10-24 17:19:51 +02:00
|
|
|
temp = ((*lColumnMatrix > 0) ? *lColumnMatrix : -(*lColumnMatrix));
|
|
|
|
if (temp > p) {
|
|
|
|
p = temp;
|
|
|
|
k2 = i;
|
|
|
|
}
|
2012-10-25 08:58:30 +02:00
|
|
|
/* next line */
|
2012-11-13 16:16:35 +01:00
|
|
|
lColumnMatrix += nb_compo;
|
2012-10-24 17:19:51 +02:00
|
|
|
}
|
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* a whole rest of 0 -> non singular */
|
2012-10-24 17:19:51 +02:00
|
|
|
if (p == 0.0) {
|
|
|
|
return OPJ_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* should we permute ? */
|
2012-10-24 17:19:51 +02:00
|
|
|
if (k2 != k) {
|
2012-10-25 08:58:30 +02:00
|
|
|
/*exchange of line */
|
|
|
|
/* k2 > k */
|
2012-10-24 17:19:51 +02:00
|
|
|
dstPermutations = tmpPermutations + k2 - k;
|
2012-10-25 08:58:30 +02:00
|
|
|
/* swap indices */
|
2012-10-24 17:19:51 +02:00
|
|
|
t = *tmpPermutations;
|
|
|
|
*tmpPermutations = *dstPermutations;
|
|
|
|
*dstPermutations = t;
|
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* and swap entire line. */
|
2012-11-13 16:16:35 +01:00
|
|
|
lColumnMatrix = lTmpMatrix + (k2 - k) * nb_compo;
|
2012-10-24 17:19:51 +02:00
|
|
|
memcpy(p_swap_area,lColumnMatrix,lSwapSize);
|
|
|
|
memcpy(lColumnMatrix,lTmpMatrix,lSwapSize);
|
|
|
|
memcpy(lTmpMatrix,p_swap_area,lSwapSize);
|
|
|
|
}
|
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* now update data in the rest of the line and line after */
|
2012-10-24 17:19:51 +02:00
|
|
|
lDestMatrix = lTmpMatrix + k;
|
2012-11-13 16:16:35 +01:00
|
|
|
lColumnMatrix = lDestMatrix + nb_compo;
|
2012-10-25 08:58:30 +02:00
|
|
|
/* take the middle element */
|
2012-10-24 17:19:51 +02:00
|
|
|
temp = *(lDestMatrix++);
|
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* now compute up data (i.e. coeff up of the diagonal). */
|
2012-11-13 16:16:35 +01:00
|
|
|
for (i = offset; i < nb_compo; ++i) {
|
2012-10-25 08:58:30 +02:00
|
|
|
/*lColumnMatrix; */
|
|
|
|
/* divide the lower column elements by the diagonal value */
|
2012-10-24 17:19:51 +02:00
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* matrix[i][k] /= matrix[k][k]; */
|
|
|
|
/* p = matrix[i][k] */
|
2012-10-24 17:19:51 +02:00
|
|
|
p = *lColumnMatrix / temp;
|
|
|
|
*(lColumnMatrix++) = p;
|
|
|
|
|
2012-11-13 16:16:35 +01:00
|
|
|
for (j = /* k + 1 */ offset; j < nb_compo; ++j) {
|
2012-10-25 08:58:30 +02:00
|
|
|
/* matrix[i][j] -= matrix[i][k] * matrix[k][j]; */
|
2012-10-24 17:19:51 +02:00
|
|
|
*(lColumnMatrix++) -= p * (*(lDestMatrix++));
|
|
|
|
}
|
2012-10-25 08:58:30 +02:00
|
|
|
/* come back to the k+1th element */
|
2012-10-24 17:19:51 +02:00
|
|
|
lDestMatrix -= lStride;
|
2012-10-25 08:58:30 +02:00
|
|
|
/* go to kth element of the next line */
|
2012-10-24 17:19:51 +02:00
|
|
|
lColumnMatrix += k;
|
|
|
|
}
|
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* offset is now k+2 */
|
2012-10-24 17:19:51 +02:00
|
|
|
++offset;
|
2012-10-25 08:58:30 +02:00
|
|
|
/* 1 element less for stride */
|
2012-10-24 17:19:51 +02:00
|
|
|
--lStride;
|
2012-10-25 08:58:30 +02:00
|
|
|
/* next line */
|
2012-11-13 16:16:35 +01:00
|
|
|
lTmpMatrix+=nb_compo;
|
2012-10-25 08:58:30 +02:00
|
|
|
/* next permutation element */
|
2012-10-24 17:19:51 +02:00
|
|
|
++tmpPermutations;
|
|
|
|
}
|
|
|
|
return OPJ_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void opj_lupSolve (OPJ_FLOAT32 * pResult,
|
|
|
|
OPJ_FLOAT32 * pMatrix,
|
|
|
|
OPJ_FLOAT32 * pVector,
|
|
|
|
OPJ_UINT32* pPermutations,
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 nb_compo,OPJ_FLOAT32 * p_intermediate_data)
|
2012-10-24 17:19:51 +02:00
|
|
|
{
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_INT32 k;
|
|
|
|
OPJ_UINT32 i,j;
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_FLOAT32 sum;
|
|
|
|
OPJ_FLOAT32 u;
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 lStride = nb_compo+1;
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_FLOAT32 * lCurrentPtr;
|
|
|
|
OPJ_FLOAT32 * lIntermediatePtr;
|
|
|
|
OPJ_FLOAT32 * lDestPtr;
|
|
|
|
OPJ_FLOAT32 * lTmpMatrix;
|
|
|
|
OPJ_FLOAT32 * lLineMatrix = pMatrix;
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_FLOAT32 * lBeginPtr = pResult + nb_compo - 1;
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_FLOAT32 * lGeneratedData;
|
|
|
|
OPJ_UINT32 * lCurrentPermutationPtr = pPermutations;
|
|
|
|
|
|
|
|
|
|
|
|
lIntermediatePtr = p_intermediate_data;
|
2012-11-13 16:16:35 +01:00
|
|
|
lGeneratedData = p_intermediate_data + nb_compo - 1;
|
2012-10-24 17:19:51 +02:00
|
|
|
|
2012-11-13 16:16:35 +01:00
|
|
|
for (i = 0; i < nb_compo; ++i) {
|
2012-10-24 17:19:51 +02:00
|
|
|
sum = 0.0;
|
|
|
|
lCurrentPtr = p_intermediate_data;
|
|
|
|
lTmpMatrix = lLineMatrix;
|
|
|
|
for (j = 1; j <= i; ++j)
|
|
|
|
{
|
2012-10-25 08:58:30 +02:00
|
|
|
/* sum += matrix[i][j-1] * y[j-1]; */
|
2012-10-24 17:19:51 +02:00
|
|
|
sum += (*(lTmpMatrix++)) * (*(lCurrentPtr++));
|
|
|
|
}
|
2012-10-25 08:58:30 +02:00
|
|
|
/*y[i] = pVector[pPermutations[i]] - sum; */
|
2012-10-24 17:19:51 +02:00
|
|
|
*(lIntermediatePtr++) = pVector[*(lCurrentPermutationPtr++)] - sum;
|
2012-11-13 16:16:35 +01:00
|
|
|
lLineMatrix += nb_compo;
|
2012-10-24 17:19:51 +02:00
|
|
|
}
|
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* we take the last point of the matrix */
|
2012-11-13 16:16:35 +01:00
|
|
|
lLineMatrix = pMatrix + nb_compo*nb_compo - 1;
|
2012-10-24 17:19:51 +02:00
|
|
|
|
2012-10-25 08:58:30 +02:00
|
|
|
/* and we take after the last point of the destination vector */
|
2012-11-13 16:16:35 +01:00
|
|
|
lDestPtr = pResult + nb_compo;
|
2012-10-24 17:19:51 +02:00
|
|
|
|
2012-11-13 16:16:35 +01:00
|
|
|
|
|
|
|
assert(nb_compo != 0);
|
|
|
|
for (k = (OPJ_INT32)nb_compo - 1; k != -1 ; --k) {
|
2012-10-24 17:19:51 +02:00
|
|
|
sum = 0.0;
|
|
|
|
lTmpMatrix = lLineMatrix;
|
|
|
|
u = *(lTmpMatrix++);
|
|
|
|
lCurrentPtr = lDestPtr--;
|
2012-11-13 16:16:35 +01:00
|
|
|
for (j = k + 1; j < nb_compo; ++j) {
|
|
|
|
/* sum += matrix[k][j] * x[j] */
|
2012-10-24 17:19:51 +02:00
|
|
|
sum += (*(lTmpMatrix++)) * (*(lCurrentPtr++));
|
|
|
|
}
|
2012-11-13 16:16:35 +01:00
|
|
|
/*x[k] = (y[k] - sum) / u; */
|
2012-10-24 17:19:51 +02:00
|
|
|
*(lBeginPtr--) = (*(lGeneratedData--) - sum) / u;
|
|
|
|
lLineMatrix -= lStride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void opj_lupInvert (OPJ_FLOAT32 * pSrcMatrix,
|
|
|
|
OPJ_FLOAT32 * pDestMatrix,
|
2012-11-13 16:16:35 +01:00
|
|
|
OPJ_UINT32 nb_compo,
|
2012-10-24 17:19:51 +02:00
|
|
|
OPJ_UINT32 * pPermutations,
|
|
|
|
OPJ_FLOAT32 * p_src_temp,
|
|
|
|
OPJ_FLOAT32 * p_dest_temp,
|
|
|
|
OPJ_FLOAT32 * p_swap_area )
|
|
|
|
{
|
|
|
|
OPJ_UINT32 j,i;
|
|
|
|
OPJ_FLOAT32 * lCurrentPtr;
|
|
|
|
OPJ_FLOAT32 * lLineMatrix = pDestMatrix;
|
2014-03-03 13:13:11 +01:00
|
|
|
OPJ_UINT32 lSwapSize = nb_compo * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
|
2012-10-24 17:19:51 +02:00
|
|
|
|
2012-11-13 16:16:35 +01:00
|
|
|
for (j = 0; j < nb_compo; ++j) {
|
2012-10-24 17:19:51 +02:00
|
|
|
lCurrentPtr = lLineMatrix++;
|
|
|
|
memset(p_src_temp,0,lSwapSize);
|
|
|
|
p_src_temp[j] = 1.0;
|
2012-11-13 16:16:35 +01:00
|
|
|
opj_lupSolve(p_dest_temp,pSrcMatrix,p_src_temp, pPermutations, nb_compo , p_swap_area);
|
2012-10-24 17:19:51 +02:00
|
|
|
|
2012-11-13 16:16:35 +01:00
|
|
|
for (i = 0; i < nb_compo; ++i) {
|
2012-10-24 17:19:51 +02:00
|
|
|
*(lCurrentPtr) = p_dest_temp[i];
|
2012-11-13 16:16:35 +01:00
|
|
|
lCurrentPtr+=nb_compo;
|
2012-10-24 17:19:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|