Beispiel #1
0
/**
 * This routine reads textual descriptions of sets of parameters
 * which describe the characteristics of feature dimensions.
 *
 * Exceptions:
 * - ILLEGALCIRCULARSPEC
 * - ILLEGALESSENTIALSPEC
 * - ILLEGALMINMAXSPEC
 * @param File open text file to read N parameter descriptions from
 * @param N number of parameter descriptions to read
 * @return Pointer to an array of parameter descriptors.
 * @note Globals: None
 * @note History: 6/6/89, DSJ, Created.
 */
PARAM_DESC *ReadParamDesc(FILE *File, uinT16 N) {
  int i;
  PARAM_DESC *ParamDesc;
  char Token[TOKENSIZE];

  ParamDesc = (PARAM_DESC *) Emalloc (N * sizeof (PARAM_DESC));
  for (i = 0; i < N; i++) {
    if (tfscanf(File, "%s", Token) != 1)
      DoError (ILLEGALCIRCULARSPEC,
        "Illegal circular/linear specification");
    if (Token[0] == 'c')
      ParamDesc[i].Circular = TRUE;
    else
      ParamDesc[i].Circular = FALSE;

    if (tfscanf(File, "%s", Token) != 1)
      DoError (ILLEGALESSENTIALSPEC,
        "Illegal essential/non-essential spec");
    if (Token[0] == 'e')
      ParamDesc[i].NonEssential = FALSE;
    else
      ParamDesc[i].NonEssential = TRUE;
    if (tfscanf(File, "%f%f", &(ParamDesc[i].Min), &(ParamDesc[i].Max)) != 2)
      DoError (ILLEGALMINMAXSPEC, "Illegal min or max specification");
    ParamDesc[i].Range = ParamDesc[i].Max - ParamDesc[i].Min;
    ParamDesc[i].HalfRange = ParamDesc[i].Range / 2;
    ParamDesc[i].MidRange = (ParamDesc[i].Max + ParamDesc[i].Min) / 2;
  }
  return (ParamDesc);
}
Beispiel #2
0
/**
 * This routine reads N floats from the specified text file
 * and places them into Buffer.  If Buffer is NULL, a buffer
 * is created and passed back to the caller.  If EOF is
 * encountered before any floats can be read, NULL is
 * returned.
 * @param File open text file to read floats from
 * @param N number of floats to read
 * @param Buffer pointer to buffer to place floats into
 * @return Pointer to buffer holding floats or NULL if EOF
 * @note Globals: None
 * @note Exceptions: ILLEGALFLOAT
 * @note History: 6/6/89, DSJ, Created.
 */
FLOAT32* ReadNFloats(FILE * File, uinT16 N, FLOAT32 Buffer[]) {
  bool needs_free = false;
  int i;
  int NumFloatsRead;

  if (Buffer == NULL) {
    Buffer = reinterpret_cast<FLOAT32*>(Emalloc(N * sizeof(FLOAT32)));
    needs_free = true;
  }

  for (i = 0; i < N; i++) {
    NumFloatsRead = tfscanf(File, "%f", &(Buffer[i]));
    if (NumFloatsRead != 1) {
      if ((NumFloatsRead == EOF) && (i == 0)) {
        if (needs_free) {
            Efree(Buffer);
        }
        return NULL;
      } else {
        DoError(ILLEGALFLOAT, "Illegal float specification");
      }
    }
  }
  return Buffer;
}
Beispiel #3
0
/**
 * This routine reads an single token from the specified
 * text file and interprets it as a prototype specification.
 * @param File open text file to read prototype style from
 * @return Prototype style read from text file
 * @note Globals: None
 * @note Exceptions: ILLEGALSTYLESPEC illegal prototype style specification
 * @note History: 6/8/89, DSJ, Created.
 */
PROTOSTYLE ReadProtoStyle(FILE *File) {
  char Token[TOKENSIZE];
  PROTOSTYLE Style;

  if (tfscanf(File, "%s", Token) != 1)
    DoError (ILLEGALSTYLESPEC, "Illegal prototype style specification");
  switch (Token[0]) {
    case 's':
      Style = spherical;
      break;
    case 'e':
      Style = elliptical;
      break;
    case 'm':
      Style = mixed;
      break;
    case 'a':
      Style = automatic;
      break;
    default:
      Style = elliptical;
      DoError (ILLEGALSTYLESPEC, "Illegal prototype style specification");
  }
  return (Style);
}
Beispiel #4
0
/**
 * Open Filename, read in all of the class-id/cutoff pairs
 * and insert them into the Cutoffs array.  Cutoffs are
 * indexed in the array by class id.  Unused entries in the
 * array are set to an arbitrarily high cutoff value.
 * @param CutoffFile name of file containing cutoff definitions
 * @param Cutoffs array to put cutoffs into
 * @param swap
 * @param end_offset
 * @return none
 * @note Globals: none
 * @note Exceptions: none
 * @note History: Wed Feb 20 09:38:26 1991, DSJ, Created.
 */
    void Classify::ReadNewCutoffs(FILE *CutoffFile, bool swap, inT64 end_offset,
                                  CLASS_CUTOFF_ARRAY Cutoffs) {
        char Class[UNICHAR_LEN + 1];
        CLASS_ID ClassId;
        int Cutoff;
        int i;

        if (shape_table_ != NULL) {
            if (!shapetable_cutoffs_.DeSerialize(swap, CutoffFile)) {
                tprintf("Error during read of shapetable pffmtable!\n");
            }
        }
        for (i = 0; i < MAX_NUM_CLASSES; i++)
            Cutoffs[i] = MAX_CUTOFF;

        while ((end_offset < 0 || ftell(CutoffFile) < end_offset) &&
               tfscanf(CutoffFile, "%"
            REALLY_QUOTE_IT(UNICHAR_LEN)
        "s %d",
                Class, &Cutoff) == 2) {
            if (strcmp(Class, "NULL") == 0) {
                ClassId = unicharset.unichar_to_id(" ");
            } else {
                ClassId = unicharset.unichar_to_id(Class);
            }
            Cutoffs[ClassId] = Cutoff;
            SkipNewline(CutoffFile);
        }
    }
Beispiel #5
0
bool read_unlv_file(                    //print list of sides
                     STRING name,        //basename of file
                     int32_t xsize,        //image size
                     int32_t ysize,        //image size
                     BLOCK_LIST *blocks  //output list
                    ) {
  FILE *pdfp;                    //file pointer
  BLOCK *block;                  //current block
  int x;                         //current top-down coords
  int y;
  int width;                     //of current block
  int height;
  BLOCK_IT block_it = blocks;    //block iterator

  name += UNLV_EXT;              //add extension
  if ((pdfp = fopen (name.string (), "rb")) == nullptr) {
    return false;                //didn't read one
  } else {
    while (tfscanf(pdfp, "%d %d %d %d %*s", &x, &y, &width, &height) >= 4) {
                                 //make rect block
      block = new BLOCK (name.string (), TRUE, 0, 0,
                         (int16_t) x, (int16_t) (ysize - y - height),
                         (int16_t) (x + width), (int16_t) (ysize - y));
                                 //on end of list
      block_it.add_to_end (block);
    }
    fclose(pdfp);
  }
  return true;
}
Beispiel #6
0
/**
 * This routine reads a single integer from the specified
 * file and checks to ensure that it is between 0 and
 * MAXSAMPLESIZE.
 * @param File open text file to read sample size from
 * @return Sample size
 * @note Globals: None
 * @note Exceptions: ILLEGALSAMPLESIZE	illegal format or range
 * @note History: 6/6/89, DSJ, Created.
 */
uinT16 ReadSampleSize(FILE *File) {
  int SampleSize;

  if ((tfscanf(File, "%d", &SampleSize) != 1) ||
    (SampleSize < 0) || (SampleSize > MAXSAMPLESIZE))
    DoError (ILLEGALSAMPLESIZE, "Illegal sample size");
  return (SampleSize);
}
/**
 * Create a new feature set of the specified type and read in
 * the features from File.  The correct text representation
 * for a feature set is an integer which specifies the number (N)
 * of features in a set followed by a list of N feature
 * descriptions.
 * @param File open text file to read new feature set from
 * @param FeatureDesc specifies type of feature to read from File
 * @return New feature set read from File.
 */
FEATURE_SET ReadFeatureSet(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
  int NumFeatures;
  ASSERT_HOST(tfscanf(File, "%d", &NumFeatures) == 1);
  ASSERT_HOST(NumFeatures >= 0);

  FEATURE_SET FeatureSet = NewFeatureSet(NumFeatures);
  for (int i = 0; i < NumFeatures; i++)
    AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));

  return FeatureSet;
}
Beispiel #8
0
/**
 * Read a character description from File, and return
 * a data structure containing this information.  The data
 * is formatted as follows:
 * @verbatim
     NumberOfSets
             ShortNameForSet1 Set1
             ShortNameForSet2 Set2
             ...
   @endverbatim
 *
 * Globals: 
 * - none
 * 
 * @param FeatureDefs    definitions of feature types/extractors
 * @param File open text file to read character description from
 * @return Character description read from File.
 * @note Exceptions: 
 * - ILLEGAL_NUM_SETS
 * @note History: Wed May 23 17:32:48 1990, DSJ, Created.
 */
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
                              FILE *File) {
    int NumSetsToRead;
    char ShortName[FEAT_NAME_SIZE];
    CHAR_DESC CharDesc;
    int Type;

    if (tfscanf(File, "%d", &NumSetsToRead) != 1 ||
        NumSetsToRead < 0 || NumSetsToRead > FeatureDefs.NumFeatureTypes)
        DoError(ILLEGAL_NUM_SETS, "Illegal number of feature sets");

    CharDesc = NewCharDescription(FeatureDefs);
    for (; NumSetsToRead > 0; NumSetsToRead--) {
        tfscanf(File, "%s", ShortName);
        Type = ShortNameToFeatureType(FeatureDefs, ShortName);
        CharDesc->FeatureSets[Type] =
                ReadFeatureSet(File, FeatureDefs.FeatureDesc[Type]);
    }
    return (CharDesc);

}                                // ReadCharDescription
/**
 * Create a new feature of the specified type and read in
 * the value of its parameters from File.  The extra penalty
 * for the feature is also computed by calling the appropriate
 * function for the specified feature type.  The correct text
 * representation for a feature is a list of N floats where
 * N is the number of parameters in the feature.
 * @param File open text file to read feature from
 * @param FeatureDesc specifies type of feature to read from File
 * @return New #FEATURE read from File.
 */
FEATURE ReadFeature(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
  FEATURE Feature;
  int i;

  Feature = NewFeature (FeatureDesc);
  for (i = 0; i < Feature->Type->NumParams; i++) {
    ASSERT_HOST(tfscanf(File, "%f", &(Feature->Params[i])) == 1);
#ifndef _WIN32
    assert (!std::isnan(Feature->Params[i]));
#endif
  }
  return Feature;
}
Beispiel #10
0
/**
 * This routine reads a textual description of a prototype from
 * the specified file.
 *
 * Exceptions:
 * - ILLEGALSIGNIFICANCESPEC
 * - ILLEGALSAMPLECOUNT
 * - ILLEGALMEANSPEC
 * - ILLEGALVARIANCESPEC
 * - ILLEGALDISTRIBUTION
 * @param File open text file to read prototype from
 * @param N number of dimensions used in prototype
 * @return List of prototypes
 * @note Globals: None
 * @note History: 6/6/89, DSJ, Created.
 */
PROTOTYPE *ReadPrototype(FILE *File, uinT16 N) {
  char Token[TOKENSIZE];
  int Status;
  PROTOTYPE *Proto;
  int SampleCount;
  int i;

  if ((Status = tfscanf(File, "%s", Token)) == 1) {
    Proto = (PROTOTYPE *) Emalloc (sizeof (PROTOTYPE));
    Proto->Cluster = NULL;
    if (Token[0] == 's')
      Proto->Significant = TRUE;
    else
      Proto->Significant = FALSE;

    Proto->Style = ReadProtoStyle (File);

    if ((tfscanf(File, "%d", &SampleCount) != 1) || (SampleCount < 0))
      DoError (ILLEGALSAMPLECOUNT, "Illegal sample count");
    Proto->NumSamples = SampleCount;

    Proto->Mean = ReadNFloats (File, N, NULL);
    if (Proto->Mean == NULL)
      DoError (ILLEGALMEANSPEC, "Illegal prototype mean");

    switch (Proto->Style) {
      case spherical:
        if (ReadNFloats (File, 1, &(Proto->Variance.Spherical)) == NULL)
          DoError (ILLEGALVARIANCESPEC, "Illegal prototype variance");
        Proto->Magnitude.Spherical =
          1.0 / sqrt ((double) (2.0 * PI * Proto->Variance.Spherical));
        Proto->TotalMagnitude =
          pow (Proto->Magnitude.Spherical, (float) N);
        Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
        Proto->Weight.Spherical = 1.0 / Proto->Variance.Spherical;
        Proto->Distrib = NULL;
        break;
      case elliptical:
        Proto->Variance.Elliptical = ReadNFloats (File, N, NULL);
        if (Proto->Variance.Elliptical == NULL)
          DoError (ILLEGALVARIANCESPEC, "Illegal prototype variance");
        Proto->Magnitude.Elliptical =
          (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
        Proto->Weight.Elliptical =
          (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
        Proto->TotalMagnitude = 1.0;
        for (i = 0; i < N; i++) {
          Proto->Magnitude.Elliptical[i] =
            1.0 /
            sqrt ((double) (2.0 * PI * Proto->Variance.Elliptical[i]));
          Proto->Weight.Elliptical[i] =
            1.0 / Proto->Variance.Elliptical[i];
          Proto->TotalMagnitude *= Proto->Magnitude.Elliptical[i];
        }
        Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
        Proto->Distrib = NULL;
        break;
      case mixed:
        Proto->Distrib =
          (DISTRIBUTION *) Emalloc (N * sizeof (DISTRIBUTION));
        for (i = 0; i < N; i++) {
          if (tfscanf(File, "%s", Token) != 1)
            DoError (ILLEGALDISTRIBUTION,
              "Illegal prototype distribution");
          switch (Token[0]) {
            case 'n':
              Proto->Distrib[i] = normal;
              break;
            case 'u':
              Proto->Distrib[i] = uniform;
              break;
            case 'r':
              Proto->Distrib[i] = D_random;
              break;
            default:
              DoError (ILLEGALDISTRIBUTION,
                "Illegal prototype distribution");
          }
        }
        Proto->Variance.Elliptical = ReadNFloats (File, N, NULL);
        if (Proto->Variance.Elliptical == NULL)
          DoError (ILLEGALVARIANCESPEC, "Illegal prototype variance");
        Proto->Magnitude.Elliptical =
          (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
        Proto->Weight.Elliptical =
          (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
        Proto->TotalMagnitude = 1.0;
        for (i = 0; i < N; i++) {
          switch (Proto->Distrib[i]) {
            case normal:
              Proto->Magnitude.Elliptical[i] = 1.0 /
                sqrt ((double)
                (2.0 * PI * Proto->Variance.Elliptical[i]));
              Proto->Weight.Elliptical[i] =
                1.0 / Proto->Variance.Elliptical[i];
              break;
            case uniform:
            case D_random:
              Proto->Magnitude.Elliptical[i] = 1.0 /
                (2.0 * Proto->Variance.Elliptical[i]);
              break;
            case DISTRIBUTION_COUNT:
              ASSERT_HOST(!"Distribution count not allowed!");
          }
          Proto->TotalMagnitude *= Proto->Magnitude.Elliptical[i];
        }
        Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
        break;
    }
    return (Proto);
  }
  else if (Status == EOF)
    return (NULL);
  else {
    DoError (ILLEGALSIGNIFICANCESPEC, "Illegal significance specification");
    return (NULL);
  }
}