Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// YOMBInputParam.cpp: implementation of the CYOMBInputParam class.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <search.h></search.h>
Toshihiro Shimizu 890ddd
#include "YombInputParam.h"
Toshihiro Shimizu 890ddd
//#include "tmsg.h"
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
// Construction/Destruction
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool CYOMBInputParam::isRange(const char *s) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)strlen(s); i++)
Toshihiro Shimizu 890ddd
		if (s[i] == '-')
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int CYOMBInputParam::getRangeBegin(const char *s) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	char ss[100];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	strcpy(ss, s);
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)strlen(ss); i++)
Toshihiro Shimizu 890ddd
		if (ss[i] == '-') {
Toshihiro Shimizu 890ddd
			ss[i] = '\0';
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	if (strlen(ss) == 0)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return atoi(ss);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int CYOMBInputParam::getRangeEnd(const char *s) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	char ss[100];
Toshihiro Shimizu 890ddd
	int i = 0;
Toshihiro Shimizu 890ddd
	for (i = strlen(s) - 1; i >= 0; i--)
Toshihiro Shimizu 890ddd
		if (s[i] == '-')
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
	strcpy(ss, &s[i + 1]);
Toshihiro Shimizu 890ddd
	if (strlen(ss) == 0)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return atoi(ss);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CYOMBInputParam::strToColorIndex(const char *s, COLOR_INDEX_LIST &cil,
Toshihiro Shimizu 890ddd
									  const int maxIndex)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* If s=="-1", all of the indices are put into the ColorIndexList
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	if (strcmp(s, "-1") == 0) {
Toshihiro Shimizu 890ddd
		for (int i = 0; i <= maxIndex; i++)
Toshihiro Shimizu 890ddd
			if (cil.nb < MAXNBCIL) {
Toshihiro Shimizu 890ddd
				cil.ci[cil.nb] = (unsigned short)i;
Toshihiro Shimizu 890ddd
				cil.nb++;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (isRange(s)) {
Toshihiro Shimizu 890ddd
		int begin = getRangeBegin(s);
Toshihiro Shimizu 890ddd
		int end = getRangeEnd(s);
Toshihiro Shimizu 890ddd
		if (begin >= 0 && end >= 0) {
Toshihiro Shimizu 890ddd
			begin = min(begin, maxIndex);
Toshihiro Shimizu 890ddd
			end = min(end, maxIndex);
Toshihiro Shimizu 890ddd
			for (int i = min(begin, end); i <= max(begin, end) && cil.nb < MAXNBCIL; i++)
Toshihiro Shimizu 890ddd
				cil.ci[cil.nb++] = (unsigned short)i;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		// If there is no 'begin' or 'end' of range, it is considered
Toshihiro Shimizu 890ddd
		// 'bad parameter'. For example: '-3' or '5-'.
Toshihiro Shimizu 890ddd
		/*		else if ( begin>=0 && end<0 ) {
Toshihiro Shimizu 890ddd
			if ( cil.nb
Toshihiro Shimizu 890ddd
				cil.ci[cil.nb]=begin;
Toshihiro Shimizu 890ddd
				cil.nb++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else if ( begin<0 && end>=0 ) {
Toshihiro Shimizu 890ddd
			if ( cil.nb
Toshihiro Shimizu 890ddd
				cil.ci[cil.nb]=end;
Toshihiro Shimizu 890ddd
				cil.nb++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (cil.nb < MAXNBCIL) {
Toshihiro Shimizu 890ddd
			int q = atoi(s);
Toshihiro Shimizu 890ddd
			if (q >= 0 && q <= maxIndex)
Toshihiro Shimizu 890ddd
				cil.ci[cil.nb++] = (unsigned short)q;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int ushortCompare(const void *a, const void *b)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	unsigned short *aa;
Toshihiro Shimizu 890ddd
	unsigned short *bb;
Toshihiro Shimizu 890ddd
	aa = (unsigned short *)a;
Toshihiro Shimizu 890ddd
	bb = (unsigned short *)b;
Toshihiro Shimizu 890ddd
	if (*aa == *bb)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	if (*aa < *bb)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CYOMBInputParam::makeColorIndexList(const char *s, COLOR_INDEX_LIST &cil,
Toshihiro Shimizu 890ddd
										 const int maxIndex)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int ln = strlen(s);
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	char ss[100];
Toshihiro Shimizu 890ddd
	COLOR_INDEX_LIST tmpCil;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	cil.nb = tmpCil.nb = 0;
Toshihiro Shimizu 890ddd
	for (i = 0; i < ln; i++) {
Toshihiro Shimizu 890ddd
		if (s[i] != ',') {
Toshihiro Shimizu 890ddd
			strcpy(ss, &s[i]);
Toshihiro Shimizu 890ddd
			for (j = 0; j < (int)strlen(ss); j++)
Toshihiro Shimizu 890ddd
				if (ss[j] == ',') {
Toshihiro Shimizu 890ddd
					ss[j] = '\0';
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			i += (strlen(ss) - 1);
Toshihiro Shimizu 890ddd
			strToColorIndex(ss, tmpCil, maxIndex);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	qsort(tmpCil.ci, tmpCil.nb, sizeof(unsigned short), ushortCompare);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (tmpCil.nb > 0) {
Toshihiro Shimizu 890ddd
		cil.ci[0] = tmpCil.ci[0];
Toshihiro Shimizu 890ddd
		cil.nb = 1;
Toshihiro Shimizu 890ddd
		for (i = j = 1; i < tmpCil.nb; i++)
Toshihiro Shimizu 890ddd
			if (tmpCil.ci[i] != tmpCil.ci[i - 1])
Toshihiro Shimizu 890ddd
				cil.ci[cil.nb++] = tmpCil.ci[i];
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		cil.nb = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Constructor of RGBM input parameters.
Toshihiro Shimizu 890ddd
// It supposes 30 parameters:
Toshihiro Shimizu 890ddd
//		0,1,2,3,4		isColor4, color4 (RGBM)
Toshihiro Shimizu 890ddd
//		5,6,7,8,9		isColor3, color3 (RGBM)
Toshihiro Shimizu 890ddd
//		10,11,12,13,14		isColor2, color2 (RGBM)
Toshihiro Shimizu 890ddd
//		15,16,17,18,19		isColor1, color1 (RGBM)
Toshihiro Shimizu 890ddd
//		20,21,22,23,24		isColor0, color0 (RGBM)
Toshihiro Shimizu 890ddd
//		25. sensibility of selection (dAB)
Toshihiro Shimizu 890ddd
//		26. nbSamples (Quality). The number of sampling pixels.
Toshihiro Shimizu 890ddd
//		27. dSamples (Amount). The distance of sampling.
Toshihiro Shimizu 890ddd
//		28. isStopAtContour (Y/N) Stops the sampling at contour line.
Toshihiro Shimizu 890ddd
//		29. isRandomSampling (Y/N) Random or equal sampling.
Toshihiro Shimizu 890ddd
CYOMBInputParam::CYOMBInputParam(const int argc, const char *argv[],
Toshihiro Shimizu 890ddd
								 const int shrink)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_nbColor = m_nbSample = 0;
Toshihiro Shimizu 890ddd
	m_dSample = 0.0;
Toshihiro Shimizu 890ddd
	m_scale = shrink > 0 ? 1.0 / (double)shrink : 1.0;
Toshihiro Shimizu 890ddd
	m_isEconf = false;
Toshihiro Shimizu 890ddd
	m_isShowSelection = false;
Toshihiro Shimizu 890ddd
	if (argc == 30) {
Toshihiro Shimizu 890ddd
		m_isCM = false;
Toshihiro Shimizu 890ddd
		m_isRandomSampling = argv[29][0] == '0' ? false : true;
Toshihiro Shimizu 890ddd
		m_isStopAtContour = argv[28][0] == '0' ? false : true;
Toshihiro Shimizu 890ddd
		// Amount - dSample
Toshihiro Shimizu 890ddd
		m_dSample = atof(argv[27]) * m_scale;
Toshihiro Shimizu 890ddd
		// Quality - nbSamples
Toshihiro Shimizu 890ddd
		m_nbSample = (int)atof(argv[26]);
Toshihiro Shimizu 890ddd
		if (shrink > 1) {
Toshihiro Shimizu 890ddd
			//			if ( m_nbSample>2 ) {
Toshihiro Shimizu 890ddd
			//				m_nbSample= (int)((double)m_nbSample*sqrt(m_scale)+0.5);
Toshihiro Shimizu 890ddd
			//				m_nbSample= m_nbSample<3 ? 3 : m_nbSample;
Toshihiro Shimizu 890ddd
			//			}
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			m_nbSample = m_nbSample < 1 ? 1 : m_nbSample;
Toshihiro Shimizu 890ddd
		if ((int)(m_dSample * m_dSample * 2.5) < m_nbSample)
Toshihiro Shimizu 890ddd
			m_nbSample = (int)(m_dSample * m_dSample * 2.5);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Sensibility - dist A
Toshihiro Shimizu 890ddd
		m_dA = 3.0 * atof(argv[25]) / 50.0;
Toshihiro Shimizu 890ddd
		m_dA = m_dA <= 0.001 ? 0.001 : m_dA;
Toshihiro Shimizu 890ddd
		// Sensibility - dist AB
Toshihiro Shimizu 890ddd
		m_dAB = atof(argv[25]) / 50.0;
Toshihiro Shimizu 890ddd
		m_dAB = m_dAB <= 0.001 ? 0.001 : m_dAB;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int j = 24;
Toshihiro Shimizu 890ddd
		m_nbColor = 0;
Toshihiro Shimizu 890ddd
		for (int i = 0; i < 5; i++) {
Toshihiro Shimizu 890ddd
			if (atoi(argv[j--]) > 0) {
Toshihiro Shimizu 890ddd
				m_color[m_nbColor][0] = atoi(argv[j--]);
Toshihiro Shimizu 890ddd
				m_color[m_nbColor][1] = atoi(argv[j--]);
Toshihiro Shimizu 890ddd
				m_color[m_nbColor][2] = atoi(argv[j--]);
Toshihiro Shimizu 890ddd
				m_color[m_nbColor][3] = atoi(argv[j--]);
Toshihiro Shimizu 890ddd
				m_nbColor++;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				j -= 4;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Constructor of RGBM input parameters.
Toshihiro Shimizu 890ddd
// It supposes 6 parameters:
Toshihiro Shimizu 890ddd
//		0. Paint index list
Toshihiro Shimizu 890ddd
//		1. Ink index list
Toshihiro Shimizu 890ddd
//		2. nbSamples (Quality). The number of sampling pixels.
Toshihiro Shimizu 890ddd
//		3. dSamples (Amount). The distance of sampling.
Toshihiro Shimizu 890ddd
//		4. isStopAtContour (Y/N) Stops the sampling at contour line.
Toshihiro Shimizu 890ddd
//		5. isRandomSampling (Y/N) Random or equal sampling.
Toshihiro Shimizu 890ddd
// isCM16 - informs about the type of colormap CM16/CM24.
Toshihiro Shimizu 890ddd
CYOMBInputParam::CYOMBInputParam(const int argc, const char *argv[],
Toshihiro Shimizu 890ddd
								 const int shrink, const bool isCM16)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int nbInk = isCM16 ? 31 : 4095;
Toshihiro Shimizu 890ddd
	int nbPaint = isCM16 ? 127 : 4095;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_nbColor = m_nbSample = 0;
Toshihiro Shimizu 890ddd
	m_dSample = 0.0;
Toshihiro Shimizu 890ddd
	m_scale = shrink > 0 ? 1.0 / (double)shrink : 1.0;
Toshihiro Shimizu 890ddd
	m_isEconf = false;
Toshihiro Shimizu 890ddd
	m_isShowSelection = false;
Toshihiro Shimizu 890ddd
	m_dA = m_dAB = 0.01;
Toshihiro Shimizu 890ddd
	if (argc == 6) {
Toshihiro Shimizu 890ddd
		m_isCM = true;
Toshihiro Shimizu 890ddd
		m_isRandomSampling = argv[5][0] == '0' ? false : true;
Toshihiro Shimizu 890ddd
		m_isStopAtContour = argv[4][0] == '0' ? false : true;
Toshihiro Shimizu 890ddd
		// Amount - dSample
Toshihiro Shimizu 890ddd
		m_dSample = atof(argv[3]) * m_scale;
Toshihiro Shimizu 890ddd
		// Quality - nbSamples
Toshihiro Shimizu 890ddd
		m_nbSample = (int)atof(argv[2]);
Toshihiro Shimizu 890ddd
		if (shrink > 1) {
Toshihiro Shimizu 890ddd
			if (m_nbSample > 2) {
Toshihiro Shimizu 890ddd
				m_nbSample = (int)((double)m_nbSample * sqrt(m_scale) + 0.5);
Toshihiro Shimizu 890ddd
				m_nbSample = m_nbSample < 2 ? 2 : m_nbSample;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			m_nbSample = m_nbSample < 1 ? 1 : m_nbSample;
Toshihiro Shimizu 890ddd
		if ((int)(m_dSample * m_dSample * 2.5) < m_nbSample)
Toshihiro Shimizu 890ddd
			m_nbSample = (int)(m_dSample * m_dSample * 2.5);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Preparing of INK and PAINT index lists
Toshihiro Shimizu 890ddd
		makeColorIndexList(&argv[1][0], m_ink, nbInk);
Toshihiro Shimizu 890ddd
		makeColorIndexList(&argv[0][0], m_paint, nbPaint);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool CYOMBInputParam::isOK()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_isCM && m_nbColor <= 1)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	if (m_isCM && m_paint.nb <= 1)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	if (m_dSample < 1.1)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	if (m_nbSample <= 1)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	//	if ( (int)(m_dSample*m_dSample*3.1)
Toshihiro Shimizu 890ddd
	//		return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CYOMBInputParam::print(COLOR_INDEX_LIST &cil)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/*	tmsg_info("nb=%d ci=",cil.nb);
Toshihiro Shimizu 890ddd
	for( int i=0; i
Toshihiro Shimizu 890ddd
		tmsg_info("i=%d %d",i,(int)cil.ci[i]);*/
Toshihiro Shimizu 890ddd
}