Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by Mis ( 15 years ago )
#include "cmRGBtoHSL.h"
#include <mmStringUtilities.h>
#include <mmInterfaceInitializers.h>
#include <mmOperatingSystemCalls.h>
#include <math.h>
mmImages::cmRGBtoHSL::cmRGBtoHSL(mmMemory::mmMemoryManagerI* p_psMemoryManager,
mmLog::mmLogReceiverI* p_psLogReceiver):
mmCalcMethodI(p_psMemoryManager,
p_psLogReceiver,
mmString(L"cmRGBtoHSL"))
{
swprintf(m_sCMParams.sShortName, L"MM: RGB to HSL method");
swprintf(m_sCMParams.sIDName, L"{2E80D65A-8877-4920-AEF5-770EDB476C56}");
swprintf(m_sCMParams.sDescription,L"Transforms RGB color model to HSL");
m_sCMParams.bIsMultithreaded = false;
// put members initialization code here
// put parameters definition here
UpdateParameters();
}
mmImages::cmRGBtoHSL::~cmRGBtoHSL()
{
}
bool mmImages::cmRGBtoHSL::Calculate()
{
mmInt width;
mmInt height;
mmInt channelCount; // how many channels
mmInt pixelCount; //how many pixels to calculate PER CHANNEL (width * height)
mmInt picID = 0; //picture's ID
mmInt layID[3]; //layers ID
for(int i=0; i<3;i++)
{
layID[i] = 0;
}
mmString layName[3] = {L"H", L"S", L"L"}; //layers names
for(int i=0; i<3;i++)
{
m_psImagesStructure->LockAllDataForWrite();
layID[i] = m_psImagesStructure->GetDataLayerID(layName[i]);
if(!(layID[i]>0)) layID[i] = m_psImagesStructure->AddNewDataLayer(layName[i], 0.0);
m_psImagesStructure->UnlockAllDataFromWrite();
}
bool flagR[3]={1, 0, 0}; //RGB flags
bool flagG[3]={0, 1, 0};
bool flagB[3]={0, 0, 1};
for(int i=0;i<m_psImagesStructure->GetImagesCount();++i) //perform for all immages
{
m_psImagesStructure->LockAllDataForRead();
m_psImagesStructure->GetImageParameters(picID, &width;, &height;, &channelCount;); //get params
pixelCount=height*width; //amount of pixels in single channel
//get RGB info
mmReal* R = new mmReal[pixelCount];
m_psImagesStructure->GetPixels(picID, 0, pixelCount, flagR, R, NULL);
mmReal* G = new mmReal[pixelCount];
m_psImagesStructure->GetPixels(picID, 0, pixelCount, flagG, G, NULL);
mmReal* B = new mmReal[pixelCount];
m_psImagesStructure->GetPixels(picID, 0, pixelCount, flagB, B, NULL);
m_psImagesStructure->UnlockAllDataFromRead();
//creating new layers to store transformed RGB values
mmReal * H = new mmReal[pixelCount];
mmReal * S = new mmReal[pixelCount];
mmReal * L = new mmReal[pixelCount];
//RGB to HSL transformation algorithm
mmReal r,g,b;
mmReal m = 255.0;
mmReal M = 0.0;
for(int j=0; j<pixelCount; j++)
{
m = 255.0;
M = 0.0;
//finding min
if (R[j] <= G[j]) m = R[j];
else m = G[j];
if (B[j] <= m) m = B[j];
//finding max
if (R[j] >= G[j]) M = R[j];
else M = G[j];
if (B[j] >= M) M = B[j];
//calculating Hue
if (M == m) H[j]=0;
else
{
if (M == R[j] && G[j] >= B[j]) H[j] = 60*(G[j] - B[j])/(M-m);
if (M == R[j] && G[j] < B[j]) H[j] = (60*(G[j] - B[j])/(M-m))+360;
if (M == G[j]) H[j] = (60*(B[j] - R[j])/(M-m))+120;
if (M == B[j]) H[j] = (60*(R[j] - G[j])/(M-m))+240;
}
//calculating Lightness
L[j] = 0.5*(M + m);
//calculating Saturation
if (M == m) S[j] = 0;
else
{
if (L[j] <= 0.5) S[j] = (M-m)/(M+m);
else S[j] = (M-m)/(2- 2*L[j]);
}
//m = 255.0;
//M = 0.0;
//r=R[j];// /255.0;
//g=G[j];// /255.0;
//b=B[j];// /255.0;
//
//m = min(r,min(g,b));
//M = max(r,max(g,b));
////if(r < m) min = r;
////else if(r > max) max = r;
////if(g < min) min = g;
////else if(g > max) max = g;
////if(b < min) min = b;
////else if(b > max) max = b;
//L[j] = (m + M)/2; //getting L
//if(m == M) //Chroma = 0 case
//{
// H[j]=0;
// S[j]=0;
//}
//else
//{
// if(L[j] < 0.5)
// {
// S[j]=(M - m)/(2 * L[j]);
// }
// else
// {
// S[j]=(M - m)/(2.0 - 2*L[j]);
// }
// if(r == M) H[j] = fmod((g - b)/(M - m),6);
// if(g == M) H[j] = 2.0 + (b - r)/(M - m);
// if(b == M) H[j] = 4.0 + (r - g)/(M - m);
//
// H[j] = H[j]*60.0;
// if(H[j]<0) H[j] += 360.0;
//}
m_psImagesStructure->LockAllDataForWrite();
m_psImagesStructure->SetPointsAdditionalDataLayerCoord(i, 0, pixelCount, layID[0], H);
m_psImagesStructure->SetPointsAdditionalDataLayerCoord(i, 0, pixelCount, layID[1], S);
m_psImagesStructure->SetPointsAdditionalDataLayerCoord(i, 0, pixelCount, layID[2], L);
m_psImagesStructure->UnlockAllDataFromWrite();
}
delete [] R, G, B, H, S, L;
}
return true;
}
void mmImages::cmRGBtoHSL::ExecBeforeSingleImageCalc(mmInt p_iCurrentImageID)
{
// put code which runs before every image calculation here
}
void mmImages::cmRGBtoHSL::ExecAfterSingleImageCalc(mmInt p_iCurrentImageID)
{
// put code which runs after every image calculation here
}
void mmImages::cmRGBtoHSL::RetrieveParameters()
{
// update parameters value here after their change by the user
}
Revise this Paste