Leptonica  1.82.0
Image processing and image analysis suite
pixarith.c File Reference
#include <string.h>
#include <math.h>
#include "allheaders.h"

Go to the source code of this file.

Functions

l_ok pixAddConstantGray (PIX *pixs, l_int32 val)
 
l_ok pixMultConstantGray (PIX *pixs, l_float32 val)
 
PIXpixAddGray (PIX *pixd, PIX *pixs1, PIX *pixs2)
 
PIXpixSubtractGray (PIX *pixd, PIX *pixs1, PIX *pixs2)
 
PIXpixMultiplyGray (PIX *pixs, PIX *pixg, l_float32 norm)
 
PIXpixThresholdToValue (PIX *pixd, PIX *pixs, l_int32 threshval, l_int32 setval)
 
PIXpixInitAccumulate (l_int32 w, l_int32 h, l_uint32 offset)
 
PIXpixFinalAccumulate (PIX *pixs, l_uint32 offset, l_int32 depth)
 
PIXpixFinalAccumulateThreshold (PIX *pixs, l_uint32 offset, l_uint32 threshold)
 
l_ok pixAccumulate (PIX *pixd, PIX *pixs, l_int32 op)
 
l_ok pixMultConstAccumulate (PIX *pixs, l_float32 factor, l_uint32 offset)
 
PIXpixAbsDifference (PIX *pixs1, PIX *pixs2)
 
PIXpixAddRGB (PIX *pixs1, PIX *pixs2)
 
PIXpixMinOrMax (PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 type)
 
PIXpixMaxDynamicRange (PIX *pixs, l_int32 type)
 
PIXpixMaxDynamicRangeRGB (PIX *pixs, l_int32 type)
 
l_uint32 linearScaleRGBVal (l_uint32 sval, l_float32 factor)
 
l_uint32 logScaleRGBVal (l_uint32 sval, l_float32 *tab, l_float32 factor)
 
l_float32 * makeLogBase2Tab (void)
 
l_float32 getLogBase2 (l_int32 val, l_float32 *logtab)
 

Detailed Description


     One-image grayscale arithmetic operations (8, 16, 32 bpp)
          l_int32     pixAddConstantGray()
          l_int32     pixMultConstantGray()

     Two-image grayscale arithmetic operations (8, 16, 32 bpp)
          PIX        *pixAddGray()
          PIX        *pixSubtractGray()
          PIX        *pixMultiplyGray()

     Grayscale threshold operation (8, 16, 32 bpp)
          PIX        *pixThresholdToValue()

     Image accumulator arithmetic operations
          PIX        *pixInitAccumulate()
          PIX        *pixFinalAccumulate()
          PIX        *pixFinalAccumulateThreshold()
          l_int32     pixAccumulate()
          l_int32     pixMultConstAccumulate()

     Absolute value of difference
          PIX        *pixAbsDifference()

     Sum of color images
          PIX        *pixAddRGB()

     Two-image min and max operations (8 and 16 bpp)
          PIX        *pixMinOrMax()

     Scale pix for maximum dynamic range
          PIX        *pixMaxDynamicRange()
          PIX        *pixMaxDynamicRangeRGB()

     RGB pixel value scaling
          l_uint32    linearScaleRGBVal()
          l_uint32    logScaleRGBVal()

     Log base2 lookup
          l_float32  *makeLogBase2Tab()
          l_float32   getLogBase2()

     The image accumulator operations are used when you expect
     overflow from 8 bits on intermediate results.  For example,
     you might want a tophat contrast operator which is
        3*I - opening(I,S) - closing(I,S)
     To use these operations, first use the init to generate
     a 16 bpp image, use the accumulate to add or subtract 8 bpp
     images from that, or the multiply constant to multiply
     by a small constant (much less than 256 -- we don't want
     overflow from the 16 bit images!), and when you're finished
     use final to bring the result back to 8 bpp, clipped
     if necessary.  There is also a divide function, which
     can be used to divide one image by another, scaling the
     result for maximum dynamic range, and giving back the
     8 bpp result.

     A simpler interface to the arithmetic operations is
     provided in pixacc.c.

Definition in file pixarith.c.

Function Documentation

◆ linearScaleRGBVal()

l_uint32 linearScaleRGBVal ( l_uint32  sval,
l_float32  factor 
)

linearScaleRGBVal()

Parameters
[in]sval32-bit rgb pixel value
[in]factormultiplication factor on each component
Returns
dval linearly scaled version of sval
Notes:
     (1) factor must be chosen to be not greater than (255 / maxcomp),
         where maxcomp is the maximum value of the pixel components.
         Otherwise, the product will overflow a uint8.  In use, factor
         is the same for all pixels in a pix.
     (2) No scaling is performed on the transparency ("A") component.

Definition at line 1532 of file pixarith.c.

◆ logScaleRGBVal()

l_uint32 logScaleRGBVal ( l_uint32  sval,
l_float32 *  tab,
l_float32  factor 
)

logScaleRGBVal()

Parameters
[in]sval32-bit rgb pixel value
[in]tab256 entry log-base-2 table
[in]factormultiplication factor on each component
Returns
dval log scaled version of sval
Notes:
     (1) tab is made with makeLogBase2Tab().
     (2) factor must be chosen to be not greater than
         255.0 / log[base2](maxcomp), where maxcomp is the maximum
         value of the pixel components.  Otherwise, the product
         will overflow a uint8.  In use, factor is the same for
         all pixels in a pix.
     (3) No scaling is performed on the transparency ("A") component.

Definition at line 1565 of file pixarith.c.

◆ pixAbsDifference()

PIX* pixAbsDifference ( PIX pixs1,
PIX pixs2 
)

pixAbsDifference()

Parameters
[in]pixs1,pixs2both either 8 or 16 bpp gray, or 32 bpp RGB
Returns
pixd, or NULL on error
Notes:
     (1) The depth of pixs1 and pixs2 must be equal.
     (2) Clips computation to the min size, aligning the UL corners
     (3) For 8 and 16 bpp, assumes one gray component.
     (4) For 32 bpp, assumes 3 color components, and ignores the
         LSB of each word (the alpha channel)
     (5) Computes the absolute value of the difference between
         each component value.

Definition at line 970 of file pixarith.c.

◆ pixAccumulate()

l_ok pixAccumulate ( PIX pixd,
PIX pixs,
l_int32  op 
)

pixAccumulate()

Parameters
[in]pixd32 bpp
[in]pixs1, 8, 16 or 32 bpp
[in]opL_ARITH_ADD or L_ARITH_SUBTRACT
Returns
0 if OK; 1 on error
Notes:
     (1) This adds or subtracts each pixs value from pixd.
     (2) This clips to the minimum of pixs and pixd, so they
         do not need to be the same size.
     (3) The alignment is to the origin [UL corner] of pixs & pixd.

Definition at line 817 of file pixarith.c.

Referenced by pixaAccumulateSamples(), pixaccAdd(), and pixaccSubtract().

◆ pixAddConstantGray()

l_ok pixAddConstantGray ( PIX pixs,
l_int32  val 
)

pixAddConstantGray()

Parameters
[in]pixs8, 16 or 32 bpp
[in]valamount to add to each pixel
Returns
0 if OK, 1 on error
Notes:
     (1) In-place operation.
     (2) No clipping for 32 bpp.
     (3) For 8 and 16 bpp, if val > 0 the result is clipped
         to 0xff and 0xffff, rsp.
     (4) For 8 and 16 bpp, if val < 0 the result is clipped to 0.

Definition at line 119 of file pixarith.c.

References pixGetData(), and pixGetDimensions().

◆ pixAddGray()

PIX* pixAddGray ( PIX pixd,
PIX pixs1,
PIX pixs2 
)

pixAddGray()

Parameters
[in]pixd[optional]; this can be null, equal to pixs1, or different from pixs1
[in]pixs1can be equal to pixd
[in]pixs2
Returns
pixd always
Notes:
     (1) Arithmetic addition of two 8, 16 or 32 bpp images.
     (2) For 8 and 16 bpp, we do explicit clipping to 0xff and 0xffff,
         respectively.
     (3) Alignment is to UL corner.
     (4) There are 3 cases.  The result can go to a new dest,
         in-place to pixs1, or to an existing input dest:
         * pixd == null:   (src1 + src2) --> new pixd
         * pixd == pixs1:  (src1 + src2) --> src1  (in-place)
         * pixd != pixs1:  (src1 + src2) --> input pixd
     (5) pixs2 must be different from both pixd and pixs1.

Definition at line 265 of file pixarith.c.

◆ pixAddRGB()

PIX* pixAddRGB ( PIX pixs1,
PIX pixs2 
)

pixAddRGB()

Parameters
[in]pixs1,pixs232 bpp RGB, or colormapped
Returns
pixd, or NULL on error
Notes:
     (1) Clips computation to the minimum size, aligning the UL corners.
     (2) Removes any colormap to RGB, and ignores the LSB of each
         pixel word (the alpha channel).
     (3) Adds each component value, pixelwise, clipping to 255.
     (4) This is useful to combine two images where most of the
         pixels are essentially black, such as in pixPerceptualDiff().

Definition at line 1067 of file pixarith.c.

References pixGetDimensions().

◆ pixFinalAccumulate()

PIX* pixFinalAccumulate ( PIX pixs,
l_uint32  offset,
l_int32  depth 
)

pixFinalAccumulate()

Parameters
[in]pixs32 bpp
[in]offsetsame as used for initialization
[in]depth8, 16 or 32 bpp, of destination
Returns
pixd 8, 16 or 32 bpp, or NULL on error
Notes:
     (1) offset must be >= 0 and should not exceed 0x40000000.
     (2) offset is subtracted from the src 32 bpp image
     (3) For 8 bpp dest, the result is clipped to [0, 0xff]
     (4) For 16 bpp dest, the result is clipped to [0, 0xffff]

Definition at line 683 of file pixarith.c.

Referenced by pixaccFinal().

◆ pixFinalAccumulateThreshold()

PIX* pixFinalAccumulateThreshold ( PIX pixs,
l_uint32  offset,
l_uint32  threshold 
)

pixFinalAccumulateThreshold()

Parameters
[in]pixs32 bpp
[in]offsetsame as used for initialization
[in]thresholdvalues less than this are set in the destination
Returns
pixd 1 bpp, or NULL on error
Notes:
     (1) offset must be >= 0 and should not exceed 0x40000000.
     (2) offset is subtracted from the src 32 bpp image

Definition at line 760 of file pixarith.c.

◆ pixInitAccumulate()

PIX* pixInitAccumulate ( l_int32  w,
l_int32  h,
l_uint32  offset 
)

pixInitAccumulate()

Parameters
[in]w,hof accumulate array
[in]offsetinitialize the 32 bpp to have this value; not more than 0x40000000
Returns
pixd 32 bpp, or NULL on error
Notes:
     (1) offset must be >= 0.
     (2) offset is used so that we can do arithmetic
         with negative number results on l_uint32 data; it
         prevents the l_uint32 data from going negative.
     (3) Because we use l_int32 intermediate data results,
         these should never exceed the max of l_int32 (0x7fffffff).
         We do not permit the offset to be above 0x40000000,
         which is half way between 0 and the max of l_int32.
     (4) The same offset should be used for initialization,
         multiplication by a constant, and final extraction!
     (5) If you're only adding positive values, offset can be 0.

Definition at line 649 of file pixarith.c.

References pixCreate(), and pixSetAllArbitrary().

Referenced by pixaAccumulateSamples().

◆ pixMaxDynamicRange()

PIX* pixMaxDynamicRange ( PIX pixs,
l_int32  type 
)

pixMaxDynamicRange()

Parameters
[in]pixs4, 8, 16 or 32 bpp source
[in]typeL_LINEAR_SCALE or L_LOG_SCALE
Returns
pixd 8 bpp, or NULL on error
Notes:
     (1) Scales pixel values to fit maximally within the dest 8 bpp pixd
     (2) Assumes the source 'pixels' are a 1-component scalar.  For
         a 32 bpp source, each pixel is treated as a single number --
         not as a 3-component rgb pixel value.
     (3) Uses a LUT for log scaling.

Definition at line 1253 of file pixarith.c.

References pixGetDimensions().

◆ pixMaxDynamicRangeRGB()

PIX* pixMaxDynamicRangeRGB ( PIX pixs,
l_int32  type 
)

pixMaxDynamicRangeRGB()

Parameters
[in]pixs32 bpp rgb source
[in]typeL_LINEAR_SCALE or L_LOG_SCALE
Returns
pixd 32 bpp, or NULL on error
Notes:
     (1) Scales pixel values to fit maximally within a 32 bpp dest pixd
     (2) All color components are scaled with the same factor, based
         on the maximum r,g or b component in the image.  This should
         not be used if the 32-bit value is a single number (e.g., a
         count in a histogram generated by pixMakeHistoHS()).
     (3) Uses a LUT for log scaling.

Definition at line 1441 of file pixarith.c.

◆ pixMinOrMax()

PIX* pixMinOrMax ( PIX pixd,
PIX pixs1,
PIX pixs2,
l_int32  type 
)

pixMinOrMax()

Parameters
[in]pixd[optional] destination: this can be null, equal to pixs1, or different from pixs1
[in]pixs1can be equal to pixd
[in]pixs2
[in]typeL_CHOOSE_MIN, L_CHOOSE_MAX
Returns
pixd always
Notes:
     (1) This gives the min or max of two images, component-wise.
     (2) The depth can be 8 or 16 bpp for 1 component, and 32 bpp
         for a 3 component image.  For 32 bpp, ignore the LSB
         of each word (the alpha channel)
     (3) There are 3 cases:
         ~  if pixd == null,   Min(src1, src2) --> new pixd
         ~  if pixd == pixs1,  Min(src1, src2) --> src1  (in-place)
         ~  if pixd != pixs1,  Min(src1, src2) --> input pixd

Definition at line 1152 of file pixarith.c.

◆ pixMultConstAccumulate()

l_ok pixMultConstAccumulate ( PIX pixs,
l_float32  factor,
l_uint32  offset 
)

pixMultConstAccumulate()

Parameters
[in]pixs32 bpp
[in]factor
[in]offsetsame as used for initialization
Returns
0 if OK; 1 on error
Notes:
     (1) offset must be >= 0 and should not exceed 0x40000000.
     (2) This multiplies each pixel, relative to offset, by factor.
     (3) The result is returned with offset back in place.

Definition at line 916 of file pixarith.c.

Referenced by pixaccMultConst().

◆ pixMultConstantGray()

l_ok pixMultConstantGray ( PIX pixs,
l_float32  val 
)

pixMultConstantGray()

Parameters
[in]pixs8, 16 or 32 bpp
[in]val>= 0.0; amount to multiply by each pixel
Returns
0 if OK, 1 on error
Notes:
     (1) In-place operation; val must be >= 0.
     (2) No clipping for 32 bpp.
     (3) For 8 and 16 bpp, the result is clipped to 0xff and 0xffff, rsp.

Definition at line 190 of file pixarith.c.

References pixGetData(), and pixGetDimensions().

◆ pixMultiplyGray()

PIX* pixMultiplyGray ( PIX pixs,
PIX pixg,
l_float32  norm 
)

pixMultiplyGray()

Parameters
[in]pixs32 bpp rgb or 8 bpp gray
[in]pixg8 bpp gray
[in]normmultiplicative factor to avoid overflow; 0 for default
Returns
pixd, or null on error
Notes:
     (1) This function can be used for correcting a scanned image
         under non-uniform illumination.  For that application,
         pixs is the scanned image, pixg is an image whose values
         are inversely related to light from a uniform (say, white)
         target, and norm is typically the inverse of the maximum
         pixel value in pixg.
     (2) Set norm = 0 to get the default value, which is the inverse
         of the max value in pixg.  This avoids overflow in the product.
     (3) For 32 bpp pixs, all 3 components are multiplied by the
         same number.
     (4) Alignment is to UL corner.

Definition at line 449 of file pixarith.c.

References L_SELECT_MAX, pixCreateTemplate(), pixGetData(), pixGetDimensions(), and pixGetExtremeValue().

◆ pixSubtractGray()

PIX* pixSubtractGray ( PIX pixd,
PIX pixs1,
PIX pixs2 
)

pixSubtractGray()

Parameters
[in]pixd[optional]; this can be null, equal to pixs1, or different from pixs1
[in]pixs1can be equal to pixd
[in]pixs2
Returns
pixd always
Notes:
     (1) Arithmetic subtraction of two 8, 16 or 32 bpp images.
     (2) Source pixs2 is always subtracted from source pixs1.
     (3) Do explicit clipping to 0.
     (4) Alignment is to UL corner.
     (5) There are 3 cases.  The result can go to a new dest,
         in-place to pixs1, or to an existing input dest:
         (a) pixd == null   (src1 - src2) --> new pixd
         (b) pixd == pixs1  (src1 - src2) --> src1  (in-place)
         (d) pixd != pixs1  (src1 - src2) --> input pixd
     (6) pixs2 must be different from both pixd and pixs1.

Definition at line 357 of file pixarith.c.

◆ pixThresholdToValue()

PIX* pixThresholdToValue ( PIX pixd,
PIX pixs,
l_int32  threshval,
l_int32  setval 
)

pixThresholdToValue()

Parameters
[in]pixd[optional]; if not null, must be equal to pixs
[in]pixs8, 16, 32 bpp
[in]threshval
[in]setval
Returns
pixd always
Notes:
   ~ operation can be in-place (pixs == pixd) or to a new pixd
   ~ if setval > threshval, sets pixels with a value >= threshval to setval
   ~ if setval < threshval, sets pixels with a value <= threshval to setval
   ~ if setval == threshval, no-op

Definition at line 540 of file pixarith.c.