diff options
Diffstat (limited to 'editor/pammixmulti.c')
-rw-r--r-- | editor/pammixmulti.c | 151 |
1 files changed, 74 insertions, 77 deletions
diff --git a/editor/pammixmulti.c b/editor/pammixmulti.c index f5012d7a..27390ec6 100644 --- a/editor/pammixmulti.c +++ b/editor/pammixmulti.c @@ -13,6 +13,7 @@ #include "shhopt.h" #include "mallocvar.h" #include "nstring.h" +#include "rand.h" typedef enum { BLEND_AVERAGE, /* Take the average color of all pixels */ @@ -36,6 +37,8 @@ struct ProgramState { /* Standard deviation when selecting images via a mask */ unsigned long ** imageWeights; /* Per-image weights as a function of grayscale level */ + struct pm_randSt randSt; + /* Random number generator parameters and internal state */ }; @@ -134,9 +137,9 @@ parseCommandLine(int argc, const char ** argv, } static void -openInputFiles(unsigned int const inFileCt, - const char ** const inFileName, - struct ProgramState * const stateP) { +initInput(unsigned int const inFileCt, + const char ** const inFileName, + struct ProgramState * const stateP) { /*---------------------------------------------------------------------------- Open all of the input files. @@ -180,6 +183,25 @@ openInputFiles(unsigned int const inFileCt, } + +static void +termInput(struct ProgramState * const stateP) { +/*---------------------------------------------------------------------------- + Deallocate all of the resources we allocated. +-----------------------------------------------------------------------------*/ + unsigned int i; + + for (i = 0; i < stateP->inFileCt; ++i) { + pnm_freepamrow(stateP->inTupleRows[i]); + pm_close(stateP->inPam[i].file); + } + + free(stateP->inTupleRows); + free(stateP->inPam); +} + + + static void initMask(const char * const maskFileName, struct ProgramState * const stateP) { @@ -233,6 +255,15 @@ initOutput(FILE * const ofP, } +static void +termOutput(struct ProgramState * const stateP) { + + free(stateP->outTupleRow); + + pm_close(stateP->outPam.file); +} + + static void blendTuplesRandom(struct ProgramState * const stateP, @@ -243,8 +274,8 @@ blendTuplesRandom(struct ProgramState * const stateP, from a random input image. -----------------------------------------------------------------------------*/ unsigned int const depth = stateP->inPam[0].depth; - unsigned int const img = (unsigned int) (rand() % stateP->inFileCt); - + unsigned int const img = (unsigned int) (pm_rand(&stateP->randSt) % + stateP->inFileCt); unsigned int samp; for (samp = 0; samp < depth; ++samp) @@ -277,26 +308,6 @@ blendTuplesAverage(struct ProgramState * const stateP, static void -randomNormal2(double * const r1P, - double * const r2P) { -/*---------------------------------------------------------------------------- - Return two normally distributed random numbers. ------------------------------------------------------------------------------*/ - double u1, u2; - - do { - u1 = drand48(); - u2 = drand48(); - } - while (u1 <= DBL_EPSILON); - - *r1P = sqrt(-2.0*log(u1)) * cos(2.0*M_PI*u2); - *r2P = sqrt(-2.0*log(u1)) * sin(2.0*M_PI*u2); -} - - - -static void precomputeImageWeights(struct ProgramState * const stateP, double const sigma) { /*---------------------------------------------------------------------------- @@ -332,7 +343,8 @@ precomputeImageWeights(struct ProgramState * const stateP, double r[2]; unsigned int k; - randomNormal2(&r[0], &r[1]); + pm_gaussrand2(&stateP->randSt, &r[0], &r[1]); + for (k = 0; k < 2; ++k) { int const img = r[k] * sigma + pctGray * stateP->inFileCt * 0.999999; @@ -453,26 +465,6 @@ blendImages(BlendType const blend, -static void -termState(struct ProgramState * const stateP) { -/*---------------------------------------------------------------------------- - Deallocate all of the resources we allocated. ------------------------------------------------------------------------------*/ - unsigned int i; - - for (i = 0; i < stateP->inFileCt; ++i) { - pnm_freepamrow(stateP->inTupleRows[i]); - pm_close(stateP->inPam[i].file); - } - - free(stateP->outTupleRow); - free(stateP->inTupleRows); - free(stateP->inPam); - pm_close(stateP->outPam.file); -} - - - int main(int argc, const char * argv[]) { @@ -483,13 +475,14 @@ main(int argc, const char * argv[]) { parseCommandLine(argc, argv, &cmdline); - srand(cmdline.randomseedSpec ? cmdline.randomseed : pm_randseed()); - - openInputFiles(cmdline.inFileNameCt, cmdline.inFileName, &state); + initInput(cmdline.inFileNameCt, cmdline.inFileName, &state); if (cmdline.blend == BLEND_MASK) initMask(cmdline.maskfile, &state); + pm_randinit(&state.randSt); + pm_srand2(&state.randSt, cmdline.randomseedSpec, cmdline.randomseed); + initOutput(stdout, &state); if (cmdline.blend == BLEND_MASK) @@ -497,10 +490,14 @@ main(int argc, const char * argv[]) { blendImages(cmdline.blend, &state); + termOutput(&state); + + pm_randterm(&state.randSt); + if (cmdline.blend == BLEND_MASK) termMask(&state); - termState(&state); + termInput(&state); freeCmdline(&cmdline); @@ -510,34 +507,34 @@ main(int argc, const char * argv[]) { /* COPYRIGHT LICENSE and WARRANTY DISCLAIMER -Copyright (c) 2018 Scott Pakin +Copyright (c) 2018-2023 Scott Pakin All rights reserved Redistribution and use in source and binary forms, with or without -modification, are permitted (subject to the limitations in the disclaimer -below) provided that the following conditions are met: - - Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - Neither the names of the oopyright owners nor the names of its contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - - NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY - THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT - NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the + distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |