//=================================================================================
// slicSupervoxelMex.c
//
//
// AUTORIGHTS
// Copyright (C) 2015 Ecole Polytechnique Federale de Lausanne (EPFL), Switzerland.
//
// Created by Radhakrishna Achanta on 12/01/15.
//=================================================================================
/*Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of EPFL nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS AND 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<mex.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
//=================================================================================
// rgbtolab
//=================================================================================
void rgbtolab(unsigned char* rin, unsigned char* gin, unsigned char* bin, int sz, double* lout, double* aout, double* bout)
{
int i; int sR, sG, sB;
double R,G,B;
double X,Y,Z;
double r, g, b;
const double epsilon = 0.008856; //actual CIE standard
const double kappa = 903.3; //actual CIE standard
const double Xr = 0.950456; //reference white
const double Yr = 1.0; //reference white
const double Zr = 1.088754; //reference white
double xr,yr,zr;
double fx, fy, fz;
double lval,aval,bval;
for(i = 0; i < sz; i++)
{
sR = rin[i]; sG = gin[i]; sB = bin[i];
R = sR/255.0;
G = sG/255.0;
B = sB/255.0;
if(R <= 0.04045) r = R/12.92;
else r = pow((R+0.055)/1.055,2.4);
if(G <= 0.04045) g = G/12.92;
else g = pow((G+0.055)/1.055,2.4);
if(B <= 0.04045) b = B/12.92;
else b = pow((B+0.055)/1.055,2.4);
X = r*0.4124564 + g*0.3575761 + b*0.1804375;
Y = r*0.2126729 + g*0.7151522 + b*0.0721750;
Z = r*0.0193339 + g*0.1191920 + b*0.9503041;
//------------------------
// XYZ to LAB conversion
//------------------------
xr = X/Xr;
yr = Y/Yr;
zr = Z/Zr;
if(xr > epsilon) fx = pow(xr, 1.0/3.0);
else fx = (kappa*xr + 16.0)/116.0;
if(yr > epsilon) fy = pow(yr, 1.0/3.0);
else fy = (kappa*yr + 16.0)/116.0;
if(zr > epsilon) fz = pow(zr, 1.0/3.0);
else fz = (kappa*zr + 16.0)/116.0;
lval = 116.0*fy-16.0;
aval = 500.0*(fx-fy);
bval = 200.0*(fy-fz);
lout[i] = lval; aout[i] = aval; bout[i] = bval;
}
}
//=================================================================================
/// getVoxelSeeds
///
/// Works the same way for color and grayscale cases.
//=================================================================================
void getVoxelSeeds(int width, int height, int depth, int numReqdSupervoxels, double step, int* seedIndices, int* numseeds)
{
#if 0//older code, less precise number of seeds
const bool hexgrid = false;
int n;
int xstrips, ystrips, zstrips;
int xerr, yerr,zerr;
double xerrperstrip,yerrperstrip,zerrperstrip;
int xoff,yoff,zoff;
int x,y,z;
int xe,ye,ze;
int seedx,seedy,seedz;
int i;
int sz2 = width*height;
double STEP = step;
xstrips = (0.5+(double)(width)/(double)(STEP)); if(xstrips <= 0) xstrips = 1;
ystrips = (0.5+(double)(height)/(double)(STEP)); if(ystrips <= 0) ystrips = 1;
zstrips = (0.5+(double)(depth)/(double)(STEP)); if(zstrips <= 0) zstrips = 1;
xerr = width - STEP*xstrips;if(xerr < 0){xerr = width - STEP*xstrips;}
yerr = height - STEP*ystrips;if(yerr < 0){yerr = height- STEP*ystrips;}
zerr = depth - STEP*zstrips;if(zerr < 0){zerr = depth - STEP*zstrips;}
xerrperstrip = (double)(xerr)/(double)(xstrips);
yerrperstrip = (double)(yerr)/(double)(ystrips);
zerrperstrip = (double)(zerr)/(double)(zstrips);
xoff = STEP/2; if(STEP >= width) xoff = width/2;
yoff = STEP/2; if(STEP >= height)yoff = height/2;
zoff = STEP/2; if(STEP >= depth) zoff = depth/2;
n = 0;
for( z = 0; z < zstrips; z++ )
{
ze = z*zerrperstrip;
for( y = 0; y < ystrips; y++ )
{
ye = y*yerrperstrip;
for( x = 0; x < xstrips; x++ )
{
xe = x*xerrperstrip;
seedx = (x*STEP+xoff+xe);
seedy = (y*STEP+yoff+ye);
seedz = (z*STEP+zoff+ze);
i = seedz*sz2 + seedy*width + seedx;
seedIndices[n] = i;
n++;
}
}
}
*numseeds = n;
#else
int nxsteps,nysteps,nzsteps;
double xstep,ystep,zstep;
double xoff,yoff,zoff;
int n, x, y, z, i;
int seedx,seedy,seedz;
int numcreatedseeds;
int numdiff;
int dimind;
int dims[3];
const int sz2 = width*height;
const int sz3 = width*height*depth;
double supervoxelvolume = (double)sz3/(double)numReqdSupervoxels;
xstep = ystep = zstep = step;
nxsteps = (double)width /(double)step; if(nxsteps <= 0) nxsteps = 1;
nysteps = (double)height/(double)step; if(nysteps <= 0) nysteps = 1;
nzsteps = (double)depth /(double)step; if(nzsteps <= 0) nzsteps = 1;
dims[0] = nxsteps;
dims[1] = nysteps;
dims[2] = nzsteps;
numcreatedseeds = dims[0]*dims[1]*dims[2];
dimind = 0;
while(numcreatedseeds < numReqdSupervoxels)
{
dims[dimind]++;
numcreatedseeds = dims[0]*dims[1]*dims[2];
dimind++; if(dimind >= 3) dimind = 0;
}
numdiff = abs(numcreatedseeds-numReqdSupervoxels);
dimind = 0;
while(numcreatedseeds > numReqdSupervoxels)
{
if(dims[dimind] > 1) dims[dimind]--;
numcreatedseeds = dims[0]*dims[1]*dims[2];
if(abs(numcreatedseeds-numReqdSupervoxels) > numdiff)
{
dims[dimind]++;
break;
}
dimind++; if(dimind >= 3) dimind = 0;
}
nxsteps = dims[0];
nysteps = dims[1];
nzsteps = dims[2];
xstep = (double)width/(double)nxsteps;
ystep = (double)height/(double)nysteps;
zstep = (double)depth/(double)nzsteps;
if(xstep >= width) xstep = 0;
if(ystep >= height)ystep = 0;
if(zstep >= depth) zstep = 0;
xoff = step/2.0; if(step >= width) xoff = width /2.0;
yoff = step/2.0; if(step >= height)yoff = height/2.0;
zoff = step/2.0; if(step >= depth) zoff = depth /2.0;
n = 0;
for(int z = 0; z < nzsteps; z++)
{
seedz = zoff + z*zstep; if(seedz >= depth) seedz = depth-zoff;
for(int y = 0; y < nysteps; y++)
{
seedy = yoff + y*ystep; if(seedy >= height) seedy = height-yoff;
for(int x = 0; x < nxsteps; x++