Skip to content

Instantly share code, notes, and snippets.

@VirtuosoChris
Created February 11, 2017 01:40
Show Gist options
  • Save VirtuosoChris/e9bc58bfa023cdf97e8b971402a04309 to your computer and use it in GitHub Desktop.
Save VirtuosoChris/e9bc58bfa023cdf97e8b971402a04309 to your computer and use it in GitHub Desktop.
sht_test
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
template<typename OUTTYPE, typename INTYPE>
void splice(OUTTYPE* finalImg, INTYPE*redImageIn, INTYPE* greenImageIn, INTYPE* blueImageIn, int pixels)
{
{
unsigned int r = 0, g = 0, b = 0;
for (unsigned int i = 0; i < pixels * 3;)
{
finalImg[i++] = redImageIn[r++];
finalImg[i++] = greenImageIn[g++];
finalImg[i++] = blueImageIn[b++];
}
}
}
template<typename OUTTYPE, typename INTYPE>
void separate(OUTTYPE* redImageIn, OUTTYPE* greenImageIn, OUTTYPE* blueImageIn, int pixels, INTYPE* imgIn)
{
int r = 0, g = 0, b = 0;
for (unsigned int i = 0; i < pixels * 3;)
{
//std::clog << i <<std::endl;
redImageIn[r++] = imgIn[i++];
greenImageIn[g++] = imgIn[i++];
blueImageIn[b++] = imgIn[i++];
}
}
int main(void)
{
try {
std::clog << "Begin." << std::endl;
rSHT<double> sh(9, 512);
int latLongWidth = 1024;
int latLongHeight = 512;
int channels = 3;
const char* filename = "lltest.hdr";
float *latLongData = stbi_loadf(filename, &latLongWidth, &latLongHeight, &channels, 0);
if (!latLongData)
{
throw std::runtime_error("Error reading input file ");
}
int latLongPixels = latLongWidth * latLongHeight;
rSHT<double>::ImageType redImageIn(latLongHeight, latLongWidth);
rSHT<double>::ImageType greenImageIn(latLongHeight, latLongWidth);
rSHT<double>::ImageType blueImageIn(latLongHeight, latLongWidth);
std::clog << "SEPARATING CHANNELS" << std::endl;
separate<double, float>(redImageIn.data(), greenImageIn.data(), blueImageIn.data(), latLongPixels, latLongData );
rSHT<double>::Coefficients shCoeffsR;
rSHT<double>::Coefficients shCoeffsG;
rSHT<double>::Coefficients shCoeffsB;
std::clog << "INITIALIZATION COMPLETE" << std::endl;
sh.fwd(shCoeffsR, redImageIn);
sh.fwd(shCoeffsG, greenImageIn);
sh.fwd(shCoeffsB, blueImageIn);
std::clog << "FORWARD TRANSFORMS COMPLETE" << std::endl;
rSHT<double>::ImageType redImageOut(latLongHeight, latLongWidth);
rSHT<double>::ImageType greenImageOut(latLongHeight, latLongWidth);
rSHT<double>::ImageType blueImageOut(latLongHeight, latLongWidth);
sh.inv(redImageOut, shCoeffsR);
sh.inv(greenImageOut, shCoeffsG);
sh.inv(blueImageOut, shCoeffsB);
std::clog << "INVERSE TRANSFORMS COMPLETE" << std::endl;
// 3 channels output image
float* finalImg = new float[latLongPixels * 3];
splice<float, double>(finalImg, redImageOut.data(), greenImageOut.data(), blueImageOut.data(), latLongPixels);
const char* filenameOut = "llOut.hdr";
stbi_write_hdr(filenameOut, latLongWidth, latLongHeight, channels, finalImg);
}
catch (const std::runtime_error& err)
{
std::cerr << "RUNTIME EXCEPTION : " << err.what() << std::endl;
}
std::clog << "End." << std::endl;
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment