/* ******** nrrdCommentCopy() ** ** copies comments from one nrrd to another ** Existing comments in nout are blown away ** ** This does NOT use biff. */ int nrrdCommentCopy(Nrrd *nout, const Nrrd *nin) { /* static const char me[]="nrrdCommentCopy"; */ size_t numc; int i, E; if (!(nout && nin)) { /* sprintf(err, "%s: got NULL pointer", me); biffMaybeAdd(NRRD, err, useBiff); */ return 1; } if (nout == nin) { /* can't satisfy semantics of copying with nout==nin */ return 2; } nrrdCommentClear(nout); numc = nin->cmtArr->len; E = 0; for (i=0; i<numc; i++) { if (!E) E |= nrrdCommentAdd(nout, nin->cmt[i]); } if (E) { /* sprintf(err, "%s: couldn't add all comments", me); biffMaybeAdd(NRRD, err, useBiff); */ return 3; } return 0; }
int _nrrdReadNrrdParse_comment (FILE *file, Nrrd *nrrd, NrrdIoState *nio, int useBiff) { char me[]="_nrrdReadNrrdParse_comment", err[BIFF_STRLEN]; char *info; AIR_UNUSED(file); info = nio->line + nio->pos; /* this skips the '#' at nio->line[nio->pos] and any other ' ' and '#' */ if (nrrdCommentAdd(nrrd, info)) { sprintf(err, "%s: trouble adding comment", me); biffMaybeAdd(NRRD, err, useBiff); return 1; } return 0; }
int nrrdHistoDraw(Nrrd *nout, const Nrrd *nin, size_t sy, int showLog, double max) { static const char me[]="nrrdHistoDraw", func[]="dhisto"; char cmt[AIR_STRLEN_MED]; unsigned int k, sx, x, y, maxhitidx, E, numticks, *Y, *logY, tick, *ticks; double hits, maxhits, usemaxhits; unsigned char *pgmData; airArray *mop; if (!(nin && nout && sy > 0)) { biffAddf(NRRD, "%s: invalid args", me); return 1; } if (nout == nin) { biffAddf(NRRD, "%s: nout==nin disallowed", me); return 1; } if (nrrdHistoCheck(nin)) { biffAddf(NRRD, "%s: input nrrd not a histogram", me); return 1; } sx = AIR_CAST(unsigned int, nin->axis[0].size); nrrdBasicInfoInit(nout, NRRD_BASIC_INFO_DATA_BIT); if (nrrdPGM(nout, sx, sy)) { biffAddf(NRRD, "%s: failed to allocate histogram image", me); return 1; } /* perhaps I should be using nrrdAxisInfoCopy */ nout->axis[0].spacing = nout->axis[1].spacing = AIR_NAN; nout->axis[0].thickness = nout->axis[1].thickness = AIR_NAN; nout->axis[0].min = nin->axis[0].min; nout->axis[0].max = nin->axis[0].max; nout->axis[0].center = nout->axis[1].center = nrrdCenterCell; nout->axis[0].label = (char *)airStrdup(nin->axis[0].label); nout->axis[1].label = (char *)airFree(nout->axis[1].label); pgmData = (unsigned char *)nout->data; maxhits = maxhitidx = 0; for (x=0; x<sx; x++) { hits = nrrdDLookup[nin->type](nin->data, x); if (maxhits < hits) { maxhits = hits; maxhitidx = x; } } if (AIR_EXISTS(max) && max > 0) { usemaxhits = max; } else { usemaxhits = maxhits; } nout->axis[1].min = usemaxhits; nout->axis[1].max = 0; numticks = (unsigned int)log10(usemaxhits + 1); mop = airMopNew(); ticks = (unsigned int*)calloc(numticks, sizeof(unsigned int)); airMopMem(mop, &ticks, airMopAlways); Y = (unsigned int*)calloc(sx, sizeof(unsigned int)); airMopMem(mop, &Y, airMopAlways); logY = (unsigned int*)calloc(sx, sizeof(unsigned int)); airMopMem(mop, &logY, airMopAlways); if (!(ticks && Y && logY)) { biffAddf(NRRD, "%s: failed to allocate temp arrays", me); airMopError(mop); return 1; } for (k=0; k<numticks; k++) { ticks[k] = airIndex(0, log10(pow(10,k+1) + 1), log10(usemaxhits+1), AIR_CAST(unsigned int, sy)); } for (x=0; x<sx; x++) { hits = nrrdDLookup[nin->type](nin->data, x); Y[x] = airIndex(0, hits, usemaxhits, AIR_CAST(unsigned int, sy)); logY[x] = airIndex(0, log10(hits+1), log10(usemaxhits+1), AIR_CAST(unsigned int, sy)); /* printf("%d -> %d,%d", x, Y[x], logY[x]); */ } for (y=0; y<sy; y++) { tick = 0; for (k=0; k<numticks; k++) tick |= ticks[k] == y; for (x=0; x<sx; x++) { pgmData[x + sx*(sy-1-y)] = (2 == showLog /* HACK: draw log curve, but not log tick marks */ ? (y >= logY[x] ? 0 /* above log curve */ : (y >= Y[x] ? 128 /* below log curve, above normal curve */ : 255 /* below log curve, below normal curve */ ) ) : (!showLog ? (y >= Y[x] ? 0 : 255) : (y >= logY[x] /* above log curve */ ? (!tick ? 0 /* not on tick mark */ : 255) /* on tick mark */ : (y >= Y[x] /* below log curve, above normal curve */ ? (!tick ? 128 /* not on tick mark */ : 0) /* on tick mark */ :255 /* below log curve, below normal curve */ ) ) ) ); } } E = AIR_FALSE; sprintf(cmt, "min value: %g\n", nout->axis[0].min); if (!E) E |= nrrdCommentAdd(nout, cmt); sprintf(cmt, "max value: %g\n", nout->axis[0].max); if (!E) E |= nrrdCommentAdd(nout, cmt); sprintf(cmt, "max hits: %g, in bin %d, around value %g\n", maxhits, maxhitidx, nrrdAxisInfoPos(nout, 0, maxhitidx)); if (!E) E |= nrrdCommentAdd(nout, cmt); if (!E) E |= nrrdContentSet_va(nout, func, nin, "%d", sy); if (E) { biffAddf(NRRD, "%s:", me); airMopError(mop); return 1; } /* bye */ airMopOkay(mop); return 0; }