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
e280ae
#ifdef _MSC_VER
Shinya Kitaoka c12eac
#pragma warning(disable : 4996)
Shinya Kitaoka c12eac
#endif
Shinya Kitaoka c12eac
Shinya Kitaoka c12eac
#include <algorithm></algorithm>
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>
Campbell Barton d0e335
#include "YOMBInputParam.h"
Shinya Kitaoka c12eac
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
// Construction/Destruction
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CYOMBInputParam::isRange(const char *s) const {
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)strlen(s); i++)
Shinya Kitaoka 120a6e
    if (s[i] == '-') return true;
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int CYOMBInputParam::getRangeBegin(const char *s) const {
Shinya Kitaoka 120a6e
  char ss[100];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  strcpy(ss, s);
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)strlen(ss); i++)
Shinya Kitaoka 120a6e
    if (ss[i] == '-') {
Shinya Kitaoka 120a6e
      ss[i] = '\0';
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  if (strlen(ss) == 0) return -1;
Shinya Kitaoka 120a6e
  return atoi(ss);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int CYOMBInputParam::getRangeEnd(const char *s) const {
Shinya Kitaoka 120a6e
  char ss[100];
Shinya Kitaoka 120a6e
  int i = 0;
Shinya Kitaoka 120a6e
  for (i = strlen(s) - 1; i >= 0; i--)
Shinya Kitaoka 120a6e
    if (s[i] == '-') break;
Shinya Kitaoka 120a6e
  strcpy(ss, &s[i + 1]);
Shinya Kitaoka 120a6e
  if (strlen(ss) == 0) return -1;
Shinya Kitaoka 120a6e
  return atoi(ss);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CYOMBInputParam::strToColorIndex(const char *s, COLOR_INDEX_LIST &cil,
Shinya Kitaoka 120a6e
                                      const int maxIndex) {
Shinya Kitaoka 120a6e
  /* If s=="-1", all of the indices are put into the ColorIndexList
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  if (strcmp(s, "-1") == 0) {
Shinya Kitaoka 120a6e
    for (int i = 0; i <= maxIndex; i++)
Shinya Kitaoka 120a6e
      if (cil.nb < MAXNBCIL) {
Shinya Kitaoka 120a6e
        cil.ci[cil.nb] = (unsigned short)i;
Shinya Kitaoka 120a6e
        cil.nb++;
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        return;
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (isRange(s)) {
Shinya Kitaoka 120a6e
    int begin = getRangeBegin(s);
Shinya Kitaoka 120a6e
    int end   = getRangeEnd(s);
Shinya Kitaoka 120a6e
    if (begin >= 0 && end >= 0) {
Shinya Kitaoka 120a6e
      begin = std::min(begin, maxIndex);
Shinya Kitaoka 120a6e
      end   = std::min(end, maxIndex);
Shinya Kitaoka 120a6e
      for (int i = std::min(begin, end);
Shinya Kitaoka 120a6e
           i <= std::max(begin, end) && cil.nb < MAXNBCIL; i++)
Shinya Kitaoka 120a6e
        cil.ci[cil.nb++] = (unsigned short)i;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    // If there is no 'begin' or 'end' of range, it is considered
Shinya Kitaoka 120a6e
    // 'bad parameter'. For example: '-3' or '5-'.
Shinya Kitaoka 120a6e
    /*		else if ( begin>=0 && end<0 ) {
Shinya Kitaoka 120a6e
            if ( cil.nb
Shinya Kitaoka 120a6e
                    cil.ci[cil.nb]=begin;
Shinya Kitaoka 120a6e
                    cil.nb++;
Shinya Kitaoka 120a6e
            }
Shinya Kitaoka 120a6e
    } else if ( begin<0 && end>=0 ) {
Shinya Kitaoka 120a6e
            if ( cil.nb
Shinya Kitaoka 120a6e
                    cil.ci[cil.nb]=end;
Shinya Kitaoka 120a6e
                    cil.nb++;
Shinya Kitaoka 120a6e
            }
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (cil.nb < MAXNBCIL) {
Shinya Kitaoka 120a6e
      int q                                         = atoi(s);
Shinya Kitaoka 120a6e
      if (q >= 0 && q <= maxIndex) cil.ci[cil.nb++] = (unsigned short)q;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Campbell Barton b3bd84
static int ushortCompare(const void *a, const void *b) {
Shinya Kitaoka 120a6e
  unsigned short *aa;
Shinya Kitaoka 120a6e
  unsigned short *bb;
Shinya Kitaoka 120a6e
  aa = (unsigned short *)a;
Shinya Kitaoka 120a6e
  bb = (unsigned short *)b;
Shinya Kitaoka 120a6e
  if (*aa == *bb) return 0;
Shinya Kitaoka 120a6e
  if (*aa < *bb) return -1;
Shinya Kitaoka 120a6e
  return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CYOMBInputParam::makeColorIndexList(const char *s, COLOR_INDEX_LIST &cil,
Shinya Kitaoka 120a6e
                                         const int maxIndex) {
Shinya Kitaoka 120a6e
  int ln = strlen(s);
Shinya Kitaoka 120a6e
  int i, j;
Shinya Kitaoka 120a6e
  char ss[100];
Shinya Kitaoka 120a6e
  COLOR_INDEX_LIST tmpCil;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  cil.nb = tmpCil.nb = 0;
Shinya Kitaoka 120a6e
  for (i = 0; i < ln; i++) {
Shinya Kitaoka 120a6e
    if (s[i] != ',') {
Shinya Kitaoka 120a6e
      strcpy(ss, &s[i]);
Shinya Kitaoka 120a6e
      for (j = 0; j < (int)strlen(ss); j++)
Shinya Kitaoka 120a6e
        if (ss[j] == ',') {
Shinya Kitaoka 120a6e
          ss[j] = '\0';
Shinya Kitaoka 120a6e
          break;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      i += (strlen(ss) - 1);
Shinya Kitaoka 120a6e
      strToColorIndex(ss, tmpCil, maxIndex);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  qsort(tmpCil.ci, tmpCil.nb, sizeof(unsigned short), ushortCompare);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (tmpCil.nb > 0) {
Shinya Kitaoka 120a6e
    cil.ci[0] = tmpCil.ci[0];
Shinya Kitaoka 120a6e
    cil.nb    = 1;
Shinya Kitaoka 120a6e
    for (i = j = 1; i < tmpCil.nb; i++)
Shinya Kitaoka 120a6e
      if (tmpCil.ci[i] != tmpCil.ci[i - 1]) cil.ci[cil.nb++] = tmpCil.ci[i];
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    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)
Campbell Barton f49389
//		25. sensitivity 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[],
Shinya Kitaoka 120a6e
                                 const int shrink) {
Shinya Kitaoka 120a6e
  m_nbColor = m_nbSample = 0;
Shinya Kitaoka 120a6e
  m_dSample              = 0.0;
Shinya Kitaoka 120a6e
  m_scale                = shrink > 0 ? 1.0 / (double)shrink : 1.0;
Shinya Kitaoka 120a6e
  m_isEconf              = false;
Shinya Kitaoka 120a6e
  m_isShowSelection      = false;
Shinya Kitaoka 120a6e
  if (argc == 30) {
Shinya Kitaoka 120a6e
    m_isCM             = false;
Shinya Kitaoka 120a6e
    m_isRandomSampling = argv[29][0] == '0' ? false : true;
Shinya Kitaoka 120a6e
    m_isStopAtContour  = argv[28][0] == '0' ? false : true;
Shinya Kitaoka 120a6e
    // Amount - dSample
Shinya Kitaoka 120a6e
    m_dSample = atof(argv[27]) * m_scale;
Shinya Kitaoka 120a6e
    // Quality - nbSamples
Shinya Kitaoka 120a6e
    m_nbSample = (int)atof(argv[26]);
Shinya Kitaoka 120a6e
    if (shrink > 1) {
Shinya Kitaoka 120a6e
      //			if ( m_nbSample>2 ) {
Shinya Kitaoka 120a6e
      //				m_nbSample=
Shinya Kitaoka 120a6e
      //(int)((double)m_nbSample*sqrt(m_scale)+0.5);
Shinya Kitaoka 120a6e
      //				m_nbSample= m_nbSample<3 ? 3 :
Shinya Kitaoka 38fd86
      // m_nbSample;
Shinya Kitaoka 120a6e
      //			}
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      m_nbSample = m_nbSample < 1 ? 1 : m_nbSample;
Shinya Kitaoka 120a6e
    if ((int)(m_dSample * m_dSample * 2.5) < m_nbSample)
Shinya Kitaoka 120a6e
      m_nbSample = (int)(m_dSample * m_dSample * 2.5);
Toshihiro Shimizu 890ddd
Campbell Barton f49389
    // Sensitivity - dist A
Shinya Kitaoka 120a6e
    m_dA = 3.0 * atof(argv[25]) / 50.0;
Shinya Kitaoka 120a6e
    m_dA = m_dA <= 0.001 ? 0.001 : m_dA;
Campbell Barton f49389
    // Sensitivity - dist AB
Shinya Kitaoka 120a6e
    m_dAB = atof(argv[25]) / 50.0;
Shinya Kitaoka 120a6e
    m_dAB = m_dAB <= 0.001 ? 0.001 : m_dAB;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    int j     = 24;
Shinya Kitaoka 120a6e
    m_nbColor = 0;
Shinya Kitaoka 120a6e
    for (int i = 0; i < 5; i++) {
Shinya Kitaoka 120a6e
      if (atoi(argv[j--]) > 0) {
Shinya Kitaoka 120a6e
        m_color[m_nbColor][0] = atoi(argv[j--]);
Shinya Kitaoka 120a6e
        m_color[m_nbColor][1] = atoi(argv[j--]);
Shinya Kitaoka 120a6e
        m_color[m_nbColor][2] = atoi(argv[j--]);
Shinya Kitaoka 120a6e
        m_color[m_nbColor][3] = atoi(argv[j--]);
Shinya Kitaoka 120a6e
        m_nbColor++;
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        j -= 4;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
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[],
Shinya Kitaoka 120a6e
                                 const int shrink, const bool isCM16) {
Shinya Kitaoka 120a6e
  int nbInk   = isCM16 ? 31 : 4095;
Shinya Kitaoka 120a6e
  int nbPaint = isCM16 ? 127 : 4095;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_nbColor = m_nbSample = 0;
Shinya Kitaoka 120a6e
  m_dSample              = 0.0;
Shinya Kitaoka 120a6e
  m_scale                = shrink > 0 ? 1.0 / (double)shrink : 1.0;
Shinya Kitaoka 120a6e
  m_isEconf              = false;
Shinya Kitaoka 120a6e
  m_isShowSelection      = false;
Shinya Kitaoka 120a6e
  m_dA = m_dAB = 0.01;
Shinya Kitaoka 120a6e
  if (argc == 6) {
Shinya Kitaoka 120a6e
    m_isCM             = true;
Shinya Kitaoka 120a6e
    m_isRandomSampling = argv[5][0] == '0' ? false : true;
Shinya Kitaoka 120a6e
    m_isStopAtContour  = argv[4][0] == '0' ? false : true;
Shinya Kitaoka 120a6e
    // Amount - dSample
Shinya Kitaoka 120a6e
    m_dSample = atof(argv[3]) * m_scale;
Shinya Kitaoka 120a6e
    // Quality - nbSamples
Shinya Kitaoka 120a6e
    m_nbSample = (int)atof(argv[2]);
Shinya Kitaoka 120a6e
    if (shrink > 1) {
Shinya Kitaoka 120a6e
      if (m_nbSample > 2) {
Shinya Kitaoka 120a6e
        m_nbSample = (int)((double)m_nbSample * sqrt(m_scale) + 0.5);
Shinya Kitaoka 120a6e
        m_nbSample = m_nbSample < 2 ? 2 : m_nbSample;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      m_nbSample = m_nbSample < 1 ? 1 : m_nbSample;
Shinya Kitaoka 120a6e
    if ((int)(m_dSample * m_dSample * 2.5) < m_nbSample)
Shinya Kitaoka 120a6e
      m_nbSample = (int)(m_dSample * m_dSample * 2.5);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Preparing of INK and PAINT index lists
Shinya Kitaoka 120a6e
    makeColorIndexList(&argv[1][0], m_ink, nbInk);
Shinya Kitaoka 120a6e
    makeColorIndexList(&argv[0][0], m_paint, nbPaint);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CYOMBInputParam::isOK() {
Shinya Kitaoka 120a6e
  if (!m_isCM && m_nbColor <= 1) return false;
Shinya Kitaoka 120a6e
  if (m_isCM && m_paint.nb <= 1) return false;
Shinya Kitaoka 120a6e
  if (m_dSample < 1.1) return false;
Shinya Kitaoka 120a6e
  if (m_nbSample <= 1) return false;
Shinya Kitaoka 120a6e
  //	if ( (int)(m_dSample*m_dSample*3.1)
Shinya Kitaoka 120a6e
  //		return false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CYOMBInputParam::print(COLOR_INDEX_LIST &cil) {
Shinya Kitaoka 120a6e
  /*	tmsg_info("nb=%d ci=",cil.nb);
Shinya Kitaoka 120a6e
  for( int i=0; i
Shinya Kitaoka 120a6e
          tmsg_info("i=%d %d",i,(int)cil.ci[i]);*/
Toshihiro Shimizu 890ddd
}