ppm_fs_info * ppm_fs_init(unsigned int const cols, pixval const maxval, unsigned int const flags) { ppm_fs_info * fiP; fiP = allocateFi(cols); fiP->lefttoright = 1; fiP->cols = cols; fiP->maxval = maxval; fiP->flags = flags; if (flags & FS_RANDOMINIT) { unsigned int i; srand(pm_randseed()); for (i = 0; i < cols +2; ++i) { /* random errors in [-1..+1] */ fiP->thisrederr[i] = rand() % 32 - 16; fiP->thisgreenerr[i] = rand() % 32 - 16; fiP->thisblueerr[i] = rand() % 32 - 16; } } else { unsigned int i; for (i = 0; i < cols + 2; ++i) fiP->thisrederr[i] = fiP->thisgreenerr[i] = fiP->thisblueerr[i] = 0; } return fiP; }
static void randomizeError(long ** const err, unsigned int const width, unsigned int const depth) { /*---------------------------------------------------------------------------- Set a random error in the range [-1 .. 1] (normalized via FS_SCALE) in the error array err[][]. -----------------------------------------------------------------------------*/ unsigned int col; srand(pm_randseed()); for (col = 0; col < width; ++col) { unsigned int plane; for (plane = 0; plane < depth; ++plane) err[plane][col] = rand() % (FS_SCALE * 2) - FS_SCALE; } }
int main(int argc, char *argv[]) { struct cmdlineInfo cmdline; FILE* ifP; pgm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); srand(cmdline.randomseedSpec ? cmdline.randomseed : pm_randseed()); ifP = pm_openr(cmdline.inputFilespec); if (cmdline.halftone == QT_HILBERT) doHilbert(ifP, cmdline.clumpSize); else { struct converter converter; struct pam graypam; struct pam bitpam; tuplen * grayrow; tuple * bitrow; int row; pnm_readpaminit(ifP, &graypam, PAM_STRUCT_SIZE(tuple_type)); bitpam = makeOutputPam(graypam.width, graypam.height); pnm_writepaminit(&bitpam); switch (cmdline.halftone) { case QT_FS: converter = createFsConverter(&graypam, cmdline.threshval); break; case QT_ATKINSON: converter = createAtkinsonConverter(&graypam, cmdline.threshval); break; case QT_THRESH: converter = createThreshConverter(&graypam, cmdline.threshval); break; case QT_DITHER8: converter = createClusterConverter(&graypam, DT_REGULAR, 8); break; case QT_CLUSTER: converter = createClusterConverter(&graypam, DT_CLUSTER, cmdline.clusterRadius); break; case QT_HILBERT: pm_error("INTERNAL ERROR: halftone is QT_HILBERT where it " "shouldn't be."); break; } grayrow = pnm_allocpamrown(&graypam); bitrow = pnm_allocpamrow(&bitpam); for (row = 0; row < graypam.height; ++row) { pnm_readpamrown(&graypam, grayrow); converter.convertRow(&converter, row, grayrow, bitrow); pnm_writepamrow(&bitpam, bitrow); } pnm_freepamrow(bitrow); pnm_freepamrow(grayrow); if (converter.destroy) converter.destroy(&converter); } pm_close(ifP); return 0; }