Beispiel #1
0
int
main(int argc, char *argv[]) {
  char *resS, *scS, *outS;
  int i, res;
  FILE *out;
  float hue, R, G, B, sc;
  
  me = argv[0];
  if (4 != argc)
    usage();
  resS = argv[1];
  scS = argv[2];
  outS = argv[3];
  
  if (1 != sscanf(resS, "%d", &res)) {
    fprintf(stderr, "%s: couldn't parse \"%s\" as int\n", me, resS);
    exit(1);
  }
  if (1 != sscanf(scS, "%f", &sc)) {
    fprintf(stderr, "%s: couldn't parse \"%s\" as float\n", me, scS);
    exit(1);
  }
  if (!(out = fopen(outS, "wa"))) {
    fprintf(stderr, "%s: couldn't open \"%s\" for writing\n", me, outS);
    exit(1);
  }

  fprintf(out, "# space: RGB\n");
  for (i=0; i<=res-1; i++) {
    hue = AIR_AFFINE(0, i, res, 0.0, 1.0);
    dyeHSVtoRGB(&R, &G, &B, hue, 1, 1);
    fprintf(out, "%g %g %g %g\n", hue, sc*R, sc*G, sc*B);
  }

  fclose(out);
  exit(0);
}
Beispiel #2
0
int
main(int argc, const char *argv[]) {
  const char *me;
  hestOpt *hopt;
  hestParm *hparm;
  airArray *mop;

  char **ninStr, *err, *outS, doneStr[13];
  Nrrd *nin0, *nin, *nrgb, *nout, *nhist[2], *npreout, *nhproj[3];
  float *rgb;
  float *out, *preout, *hist[2], maxSum,
    upSample, overSampleScale;
  unsigned int size0, sX, sY, sH, ninLen, ti, overSampleNum;
  NrrdResampleContext *rsmc;
  NrrdKernelSpec *ksp;

  me = argv[0];
  mop = airMopNew();
  hopt = NULL;
  hparm = hestParmNew();
  airMopAdd(mop, hparm, (airMopper)hestParmFree, airMopAlways);
  hparm->respFileEnable = AIR_TRUE;
  hestOptAdd(&hopt, "i", "images", airTypeString, 1, -1, &ninStr, NULL,
             "input image sequence", &ninLen, NULL, NULL);
  hestOptAdd(&hopt, "sh", "histo size", airTypeUInt, 1, 1, &sH, "500",
             "histogram size");
  hestOptAdd(&hopt, "k", "kern", airTypeOther, 1, 1, &ksp,
             "tent", "kernel for upsampling images",
             NULL, NULL, nrrdHestKernelSpec);
  hestOptAdd(&hopt, "us", "upsampling", airTypeFloat, 1, 1, &upSample,
             "1", "amount of upsampling of image");
  hestOptAdd(&hopt, "osn", "# oversmp", airTypeUInt, 1, 1, &overSampleNum,
             "1", "number of sample per (upsampled) pixel");
  hestOptAdd(&hopt, "osc", "scaling", airTypeFloat, 1, 1, &overSampleScale,
             "1", "scaling with oversampling");
  hestOptAdd(&hopt, "ms", "max sum", airTypeFloat, 1, 1, &maxSum,
             "10", "per-hue histogram summation is non-linearly and "
             "asymptotically clamped to this maximum");
  hestOptAdd(&hopt, "o", "nout", airTypeString, 1, 1, &outS, "-",
             "output filename", NULL);

  hestParseOrDie(hopt, argc-1, argv+1, hparm,
                 me, mchistInfo, AIR_TRUE, AIR_TRUE, AIR_TRUE);
  airMopAdd(mop, hopt, (airMopper)hestOptFree, airMopAlways);
  airMopAdd(mop, hopt, (airMopper)hestParseFree, airMopAlways);

  if (0 == overSampleNum) {
    fprintf(stderr, "%s: overSampleNum must be > 0\n", me);
    airMopError(mop);
    return 1;
  }
  nin0 = nrrdNew();
  airMopAdd(mop, nin0, (airMopper)nrrdNuke, airMopAlways);
  if (nrrdLoad(nin0, ninStr[0], NULL)) {
    airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
    fprintf(stderr, "%s: couldn't load first image:\n%s", me, err);
    airMopError(mop);
    return 1;
  }
  if (!( (3 == nin0->axis[0].size || 4 == nin0->axis[0].size)
         && 3 == nin0->dim
         && nrrdTypeUChar == nin0->type )) {
    fprintf(stderr, "%s: 1st image not 3D (3-or-4)-by-X-by-Y %s array "
            "(got %u-D %s array)\n", me,
            airEnumStr(nrrdType, nrrdTypeUChar),
            nin0->dim,
            airEnumStr(nrrdType, nin0->type));
    airMopError(mop);
    return 1;
  }
  rsmc = nrrdResampleContextNew();
  airMopAdd(mop, rsmc, (airMopper)nrrdResampleContextNix, airMopAlways);
  size0 = AIR_CAST(unsigned int, nin0->axis[0].size);
  sX = AIR_CAST(unsigned int, upSample*nin0->axis[1].size);
  sY = AIR_CAST(unsigned int, upSample*nin0->axis[2].size);
  nrgb = nrrdNew();
  airMopAdd(mop, nrgb, (airMopper)nrrdNuke, airMopAlways);
  if (nrrdResampleDefaultCenterSet(rsmc, nrrdCenterCell)
      || nrrdResampleInputSet(rsmc, nin0)
      || nrrdResampleKernelSet(rsmc, 1, ksp->kernel, ksp->parm)
      || nrrdResampleKernelSet(rsmc, 2, ksp->kernel, ksp->parm)
      || nrrdResampleSamplesSet(rsmc, 1, sX)
      || nrrdResampleSamplesSet(rsmc, 2, sY)
      || nrrdResampleRangeFullSet(rsmc, 1)
      || nrrdResampleRangeFullSet(rsmc, 2)
      || nrrdResampleTypeOutSet(rsmc, nrrdTypeFloat)
      || nrrdResampleRenormalizeSet(rsmc, AIR_TRUE)
      || nrrdResampleExecute(rsmc, nrgb)) {
    airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
    fprintf(stderr, "%s: error resampling slice:\n%s", me, err);
    airMopError(mop);
    return 1;
  }

  nhist[0] = nrrdNew();
  airMopAdd(mop, nhist[0], (airMopper)nrrdNuke, airMopAlways);
  nhist[1] = nrrdNew();
  airMopAdd(mop, nhist[1], (airMopper)nrrdNuke, airMopAlways);
  if (nrrdMaybeAlloc_va(nhist[0], nrrdTypeFloat, 2,
                        AIR_CAST(size_t, sH),
                        AIR_CAST(size_t, sH))
      || nrrdMaybeAlloc_va(nhist[1], nrrdTypeFloat, 2,
                           AIR_CAST(size_t, sH),
                           AIR_CAST(size_t, sH))) {
    airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
    fprintf(stderr, "%s: error allocating histos:\n%s", me, err);
    airMopError(mop);
    return 1;
  }
  nhist[0]->axis[0].min = nhist[0]->axis[1].min = 0.0;
  nhist[0]->axis[0].max = nhist[0]->axis[1].max = 1.0;
  nhist[1]->axis[0].min = nhist[1]->axis[1].min = 0.0;
  nhist[1]->axis[0].max = nhist[1]->axis[1].max = 1.0;
  nhproj[0] = nrrdNew();
  airMopAdd(mop, nhproj[0], (airMopper)nrrdNix, airMopAlways);
  nhproj[1] = nrrdNew();
  airMopAdd(mop, nhproj[1], (airMopper)nrrdNix, airMopAlways);
  nhproj[2] = nrrdNew();
  airMopAdd(mop, nhproj[2], (airMopper)nrrdNix, airMopAlways);

  printf("working ...       ");
  hist[0] = AIR_CAST(float *, nhist[0]->data);
  hist[1] = AIR_CAST(float *, nhist[1]->data);
  nin = nrrdNew();
  airMopAdd(mop, nin, (airMopper)nrrdNuke, airMopAlways);
  npreout = nrrdNew();
  airMopAdd(mop, npreout, (airMopper)nrrdNuke, airMopAlways);
  if (nrrdMaybeAlloc_va(npreout, nrrdTypeFloat, 3,
                        AIR_CAST(size_t, 3),
                        AIR_CAST(size_t, sH),
                        AIR_CAST(size_t, ninLen))) {
    airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
    fprintf(stderr, "%s: error allocating pre-output:\n%s", me, err);
    airMopError(mop);
    return 1;
  }
  preout = AIR_CAST(float *, npreout->data);
  for (ti=0; ti<ninLen; ti++) {
    printf("%s", airDoneStr(0, ti, ninLen, doneStr)); fflush(stdout);
    if (nrrdLoad(nin, ninStr[ti], NULL)) {
      airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
      fprintf(stderr, "%s: couldn't load image[%u]:\n%s", me, ti, err);
      airMopError(mop);
      return 1;
    }
    if (!nrrdSameSize(nin0, nin, AIR_TRUE)) {
      airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
      fprintf(stderr, "%s: nin[%u] not like nin[0]:\n%s", me, ti, err);
      airMopError(mop);
      return 1;
    }
    if (nrrdResampleInputSet(rsmc, nin)
        || nrrdResampleExecute(rsmc, nrgb)) {
      airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
      fprintf(stderr, "%s: trouble resampling nin[%u]:\n%s", me, ti, err);
      airMopError(mop);
      return 1;
    }
    if (nrrdWrap_va(nhproj[0], preout + 0*sH, nrrdTypeFloat, 1, AIR_CAST(size_t, sH)) ||
        nrrdWrap_va(nhproj[1], preout + 1*sH, nrrdTypeFloat, 1, AIR_CAST(size_t, sH)) ||
        nrrdWrap_va(nhproj[2], preout + 2*sH, nrrdTypeFloat, 1, AIR_CAST(size_t, sH))) {
      airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
      fprintf(stderr, "%s: trouble wrapping output[%u]:\n%s", me, ti, err);
      airMopError(mop);
      return 1;
    }
    rgb = AIR_CAST(float *, nrgb->data);
    imageProc(nhproj, nhist, sH,
              rgb, size0, sX*sY,
              overSampleNum, overSampleScale);
    preout += 3*sH;
  }
  printf("%s\n", airDoneStr(0, ti, ninLen, doneStr)); fflush(stdout);

  nout = nrrdNew();
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
  if (nrrdMaybeAlloc_va(nout, nrrdTypeFloat, 3,
                        AIR_CAST(size_t, 3),
                        AIR_CAST(size_t, sH),
                        AIR_CAST(size_t, ninLen))) {
    airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
    fprintf(stderr, "%s: error allocating output:\n%s", me, err);
    airMopError(mop);
    return 1;
  }
  out = AIR_CAST(float *, nout->data);
  preout = AIR_CAST(float *, npreout->data);
  for (ti=0; ti<ninLen; ti++) {
    unsigned int hi;
    float hh, vv, ss, scl;
    for (hi=0; hi<sH; hi++) {
      hh = AIR_AFFINE(0, hi, sH, 0, 1);
      if (!preout[hi + 2*sH]) {
        ELL_3V_SET(out + 3*hi, 0, 0, 0);
      } else {
        ss = preout[hi + 2*sH];
        scl = ss/(maxSum + ss);
        vv = scl*preout[hi + 1*sH];
        dyeHSVtoRGB(out + 0 + 3*hi, out + 1 + 3*hi, out + 2 + 3*hi,
                    hh, preout[hi + 0*sH], vv);
      }
    }
    out += 3*sH;
    preout += 3*sH;
  }

  if (nrrdSave(outS, nout, NULL)) {
    airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
    fprintf(stderr, "%s: error saving output:\n%s", me, err);
    airMopError(mop);
    return 1;
  }

  airMopOkay(mop);
  return 0;
}