static void writepbmrow(FILE * const fileP, const xel * const xelrow, unsigned int const cols, bool const plainFormat) { jmp_buf jmpbuf; jmp_buf * origJmpbufP; bit * bitrow; bitrow = pbm_allocrow(cols); if (setjmp(jmpbuf) != 0) { pbm_freerow(bitrow); pm_setjmpbuf(origJmpbufP); pm_longjmp(); } else { unsigned int col; pm_setjmpbufsave(&jmpbuf, &origJmpbufP); for (col = 0; col < cols; ++col) bitrow[col] = PNM_GET1(xelrow[col]) == 0 ? PBM_BLACK : PBM_WHITE; pbm_writepbmrow(fileP, bitrow, cols, plainFormat); pm_setjmpbuf(origJmpbufP); } pbm_freerow(bitrow); }
static void writepgmrow(FILE * const fileP, const xel * const xelrow, unsigned int const cols, xelval const maxval, int const format, bool const plainFormat) { jmp_buf jmpbuf; jmp_buf * origJmpbufP; gray * grayrow; grayrow = pgm_allocrow(cols); if (setjmp(jmpbuf) != 0) { pgm_freerow(grayrow); pm_setjmpbuf(origJmpbufP); pm_longjmp(); } else { unsigned int col; pm_setjmpbufsave(&jmpbuf, &origJmpbufP); for (col = 0; col < cols; ++col) grayrow[col] = PNM_GET1(xelrow[col]); pgm_writepgmrow(fileP, grayrow, cols, (gray) maxval, plainFormat); pm_setjmpbuf(origJmpbufP); } pgm_freerow(grayrow); }
tupletable pnm_alloctupletable(const struct pam * const pamP, unsigned int const size) { tupletable retval; const char * error; alloctupletable(pamP, size, &retval, &error); if (error) { pm_errormsg("%s", error); strfree(error); pm_longjmp(); } return retval; }
static tupletable tuplehashtotable(const struct pam * const pamP, tuplehash const tuplehash, unsigned int const allocsize) { /*---------------------------------------------------------------------------- Create a tuple table containing the info from a tuple hash. Allocate space in the table for 'allocsize' elements even if there aren't that many tuple values in the input hash. That's so the caller has room for expansion. Caller must ensure that 'allocsize' is at least as many tuple values as there are in the input hash. We allocate new space for all the table contents; there are no pointers in the table to tuples or anything else in existing space. -----------------------------------------------------------------------------*/ tupletable tupletable; const char * error; alloctupletable(pamP, allocsize, &tupletable, &error); if (error) { pm_errormsg("%s", error); strfree(error); pm_longjmp(); } else { unsigned int i, j; /* Loop through the hash table. */ j = 0; for (i = 0; i < HASH_SIZE; ++i) { /* Walk this hash chain */ struct tupleint_list_item * p; for (p = tuplehash[i]; p; p = p->next) { assert(j < allocsize); tupletable[j]->value = p->tupleint.value; pnm_assigntuple(pamP, tupletable[j]->tuple, p->tupleint.tuple); ++j; } } } return tupletable; }
static tuplehash computetuplefreqhash(struct pam * const pamP, tuple ** const tupleArray, unsigned int const maxsize, sample const newMaxval, unsigned int * const sizeP) { /*---------------------------------------------------------------------------- Compute a tuple frequency hash from a PAM. This is a hash that gives you the number of times a given tuple value occurs in the PAM. You can supply the input PAM in one of two ways: 1) a two-dimensional array of tuples tupleArray[][]; In this case, 'tupleArray' is non-NULL. 2) an open PAM file, positioned to the raster. In this case, 'tupleArray' is NULL. *pamP contains the file descriptor. We return with the file still open and its position undefined. In either case, *pamP contains parameters of the tuple array. Return the number of unique tuple values found as *sizeP. However, if the number of unique tuple values is greater than 'maxsize', return a null return value and *sizeP undefined. The tuple values that index the hash are scaled to a new maxval of 'newMaxval'. E.g. if the input has maxval 100 and 'newMaxval' is 50, and a particular tuple has sample value 50, it would be counted as sample value 25 in the hash. -----------------------------------------------------------------------------*/ tuplehash tuplefreqhash; tuple * rowbuffer; /* malloc'ed */ /* Buffer for a row read from the input file; undefined (but still allocated) if input is not from a file. */ tuple color; /* The color currently being added, scaled to the new maxval */ jmp_buf jmpbuf; jmp_buf * origJmpbufP; /* Initialize to "none" for purposes of error recovery */ tuplefreqhash = NULL; rowbuffer = NULL; color = NULL; if (setjmp(jmpbuf) == 0) { pm_setjmpbufsave(&jmpbuf, &origJmpbufP); computehashrecoverable(pamP, tupleArray, maxsize, newMaxval, sizeP, &tuplefreqhash, &rowbuffer, &color); pm_setjmpbuf(origJmpbufP); } else { if (color) pnm_freepamtuple(color); if (rowbuffer) pnm_freepamrow(rowbuffer); if (tuplefreqhash) pnm_destroytuplehash(tuplefreqhash); pm_longjmp(); } return tuplefreqhash; }