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

Go to the source code of this file.

Functions

static l_int32 checkRasteropCrop (l_int32 pixw, l_int32 pixh, l_int32 x, l_int32 y, l_int32 w, l_int32 h)
 
l_ok pixRasterop (PIX *pixd, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, PIX *pixs, l_int32 sx, l_int32 sy)
 
l_ok pixRasteropVip (PIX *pixd, l_int32 bx, l_int32 bw, l_int32 vshift, l_int32 incolor)
 
l_ok pixRasteropHip (PIX *pixd, l_int32 by, l_int32 bh, l_int32 hshift, l_int32 incolor)
 
PIXpixTranslate (PIX *pixd, PIX *pixs, l_int32 hshift, l_int32 vshift, l_int32 incolor)
 
l_ok pixRasteropIP (PIX *pixd, l_int32 hshift, l_int32 vshift, l_int32 incolor)
 
l_ok pixRasteropFullImage (PIX *pixd, PIX *pixs, l_int32 op)
 

Detailed Description

     General rasterop
          l_int32    pixRasterop()

     In-place full band translation
          l_int32    pixRasteropVip()
          l_int32    pixRasteropHip()

     Full image translation (general and in-place)
          l_int32    pixTranslate()
          l_int32    pixRasteropIP()

     Full image rasterop with no translation
          l_int32    pixRasteropFullImage()

     Checking for invalid crop box
          static l_int32   checkRasteropCrop()

Definition in file rop.c.

Function Documentation

◆ checkRasteropCrop()

static l_int32 checkRasteropCrop ( l_int32  pixw,
l_int32  pixh,
l_int32  x,
l_int32  y,
l_int32  w,
l_int32  h 
)
static

checkRasteropCrop()

Parameters
[in]pixw,pixhpix dimensions
[in]x,y,w,hcrop box parameters
Returns
0 if OK, 1 if the crop box does not intersect with the pix.
Notes:
     (1) The widths and heights must all be positive, but x and y
         can take on any value.
     (2) This works for checking both the source and dest regions.
     (3) This has been used to verify rasteropLow() cropping is correct.
         It is not needed for pre-filtering in pixRasterop().

Definition at line 553 of file rop.c.

Referenced by pixRasterop().

◆ pixRasterop()

l_ok pixRasterop ( PIX pixd,
l_int32  dx,
l_int32  dy,
l_int32  dw,
l_int32  dh,
l_int32  op,
PIX pixs,
l_int32  sx,
l_int32  sy 
)

pixRasterop()

Parameters
[in]pixddest pix
[in]dxx val of UL corner of dest rectangle
[in]dyy val of UL corner of dest rectangle
[in]dwwidth of dest rectangle
[in]dhheight of dest rectangle
[in]opop code
[in]pixssrc pix
[in]sxx val of UL corner of src rectangle
[in]syy val of UL corner of src rectangle
Returns
0 if OK; 1 on error.
Notes:
     (1) This has the standard set of 9 args for rasterop.
         This function is your friend; it is worth memorizing!
     (2) If the operation involves only dest, this calls
         rasteropUniLow().  Otherwise, checks depth of the
         src and dest, and if they match, calls rasteropLow().
     (3) For the two-image operation, where both pixs and pixd
         are defined, they are typically different images.  However
         there are cases, such as pixSetMirroredBorder(), where
         in-place operations can be done, blitting pixels from
         one part of pixd to another.  Consequently, we permit
         such operations.  If you use them, be sure that there
         is no overlap between the source and destination rectangles
         in pixd (!)

 Background:
 -----------

 There are 18 operations, described by the op codes in pix.h.

 One, PIX_DST, is a no-op.

 Three, PIX_CLR, PIX_SET, and PIX_NOT(PIX_DST) operate only on the dest.
 These are handled by the low-level rasteropUniLow().

 The other 14 involve the both the src and the dest, and depend on
 the bit values of either just the src or the bit values of both
 src and dest.  They are handled by rasteropLow():

         PIX_SRC                             s
         PIX_NOT(PIX_SRC)                   ~s
         PIX_SRC | PIX_DST                   s | d
         PIX_SRC & PIX_DST                   s & d
         PIX_SRC ^ PIX_DST                   s ^ d
         PIX_NOT(PIX_SRC) | PIX_DST         ~s | d
         PIX_NOT(PIX_SRC) & PIX_DST         ~s & d
         PIX_NOT(PIX_SRC) ^ PIX_DST         ~s ^ d
         PIX_SRC | PIX_NOT(PIX_DST)          s | ~d
         PIX_SRC & PIX_NOT(PIX_DST)          s & ~d
         PIX_SRC ^ PIX_NOT(PIX_DST)          s ^ ~d
         PIX_NOT(PIX_SRC | PIX_DST)         ~(s | d)
         PIX_NOT(PIX_SRC & PIX_DST)         ~(s & d)
         PIX_NOT(PIX_SRC ^ PIX_DST)         ~(s ^ d)

 Each of these is implemented with one of three low-level
 functions, depending on the alignment of the left edge
 of the src and dest rectangles:
     * a fastest implementation if both left edges are
       (32-bit) word aligned
     * a very slightly slower implementation if both left
       edges have the same relative (32-bit) word alignment
     * the general routine that is invoked when
       both left edges have different word alignment

 Of the 14 binary rasterops above, only 12 are unique
 logical combinations (out of a possible 16) of src
 and dst bits:

       (sd)         (11)   (10)   (01)   (00)
  -----------------------------------------------
        s            1      1      0      0
       ~s            0      1      0      1
      s | d          1      1      1      0
      s & d          1      0      0      0
      s ^ d          0      1      1      0
     ~s | d          1      0      1      1
     ~s & d          0      0      1      0
     ~s ^ d          1      0      0      1
      s | ~d         1      1      0      1
      s & ~d         0      1      0      0
      s ^ ~d         1      0      0      1
     ~(s | d)        0      0      0      1
     ~(s & d)        0      1      1      1
     ~(s ^ d)        1      0      0      1

 Note that the following three operations are equivalent:
     ~(s ^ d)
     ~s ^ d
     s ^ ~d
 and in the implementation, we call them out with the first form;
 namely, ~(s ^ d).

 Of the 16 possible binary combinations of src and dest bits,
 the remaining 4 unique ones are independent of the src bit.
 They depend on either just the dest bit or on neither
 the src nor dest bits:

        d            1      0      1      0    (indep. of s)
       ~d            0      1      0      1    (indep. of s)
       CLR           0      0      0      0    (indep. of both s & d)
       SET           1      1      1      1    (indep. of both s & d)

 As mentioned above, three of these are implemented by
 rasteropUniLow(), and one is a no-op.

 How can these operation codes be represented by bits
 in such a way that when the basic operations are performed
 on the bits the results are unique for unique
 operations, and mimic the logic table given above?

 The answer is to choose a particular order of the pairings:
        (sd)         (11)   (10)   (01)   (00)
 (which happens to be the same as in the above table)
 and to translate the result into 4-bit representations
 of s and d.  For example, the Sun rasterop choice
 (omitting the extra bit for clipping) is

     PIX_SRC      0xc
     PIX_DST      0xa

 This corresponds to our pairing order given above:
        (sd)         (11)   (10)   (01)   (00)
 where for s = 1 we get the bit pattern
      PIX_SRC:        1      1      0      0     (0xc)
 and for d = 1 we get the pattern
      PIX_DST:         1      0      1      0    (0xa)

 OK, that's the pairing order that Sun chose.  How many different
 ways can we assign bit patterns to PIX_SRC and PIX_DST to get
 the boolean ops to work out?  Any of the 4 pairs can be put
 in the first position, any of the remaining 3 pairs can go
 in the second; and one of the remaining 2 pairs can go the the third.
 There is a total of 4*3*2 = 24 ways these pairs can be permuted.

Definition at line 204 of file rop.c.

References checkRasteropCrop(), PIX_CLR, PIX_DST, PIX_NOT, PIX_SET, pixGetData(), pixGetDimensions(), and rasteropUniLow().

Referenced by boxaGetCoverage(), ccbaDisplayImage1(), ccbaDisplayImage2(), kernelDisplayInPix(), pixaAccumulateSamples(), pixaCreateFromPix(), pixAddContinuedBorder(), pixAddMirroredBorder(), pixAddMixedBorder(), pixAddRepeatedBorder(), pixAddWithIndicator(), pixaDisplayUnsplit(), pixaSplitPix(), pixAutoPhotoinvert(), pixClearInRect(), pixCopyBorder(), pixCopyWithBoxa(), pixDilate(), pixErode(), pixHMT(), pixInvert(), pixMakeFrameMask(), pixMirroredTiling(), pixMorphSequenceByComponent(), pixRasteropFullImage(), pixRemoveWithIndicator(), pixSetMirroredBorder(), pixSetOrClearBorder(), pixTilingPaintTile(), pixWarpStereoscopic(), recogGetWindowedArea(), recogShowAverageTemplates(), and selDisplayInPix().

◆ pixRasteropFullImage()

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

pixRasteropFullImage()

Parameters
[in]pixd
[in]pixs
[in]opany of the op-codes
Returns
0 if OK; 1 on error
Notes:
     ~ this is a wrapper for a common 2-image raster operation
     ~ both pixs and pixd must be defined
     ~ the operation is performed with aligned UL corners of pixs and pixd
     ~ the operation clips to the smallest pix; if the width or height
       of pixd is larger than pixs, some pixels in pixd will be unchanged

Definition at line 516 of file rop.c.

References pixRasterop().

◆ pixRasteropHip()

l_ok pixRasteropHip ( PIX pixd,
l_int32  by,
l_int32  bh,
l_int32  hshift,
l_int32  incolor 
)

pixRasteropHip()

Parameters
[in]pixdin-place operation
[in]bytop of horizontal band
[in]bhheight of horizontal band
[in]hshifthorizontal shift of band; hshift > 0 is to right
[in]incolorL_BRING_IN_WHITE, L_BRING_IN_BLACK
Returns
0 if OK; 1 on error
Notes:
     (1) This rasterop translates a horizontal band of the
         image either left or right, bringing in either white
         or black pixels from outside the image.
     (2) The horizontal band extends the full width of pixd.
     (3) If a colormap exists, the nearest color to white or black
         is brought in.

Definition at line 361 of file rop.c.

References L_BRING_IN_BLACK, L_BRING_IN_WHITE, pixGetData(), pixGetDimensions(), and rasteropHipLow().

Referenced by pixRasteropIP().

◆ pixRasteropIP()

l_ok pixRasteropIP ( PIX pixd,
l_int32  hshift,
l_int32  vshift,
l_int32  incolor 
)

pixRasteropIP()

Parameters
[in]pixdin-place translation
[in]hshifthorizontal shift; hshift > 0 is to right
[in]vshiftvertical shift; vshift > 0 is down
[in]incolorL_BRING_IN_WHITE, L_BRING_IN_BLACK
Returns
0 if OK; 1 on error

Definition at line 475 of file rop.c.

References pixGetDimensions(), pixRasteropHip(), and pixRasteropVip().

Referenced by pixAffineSequential(), pixCompareWithTranslation(), and pixTranslate().

◆ pixRasteropVip()

l_ok pixRasteropVip ( PIX pixd,
l_int32  bx,
l_int32  bw,
l_int32  vshift,
l_int32  incolor 
)

pixRasteropVip()

Parameters
[in]pixdin-place
[in]bxleft edge of vertical band
[in]bwwidth of vertical band
[in]vshiftvertical shift of band; vshift > 0 is down
[in]incolorL_BRING_IN_WHITE, L_BRING_IN_BLACK
Returns
0 if OK; 1 on error
Notes:
     (1) This rasterop translates a vertical band of the
         image either up or down, bringing in either white
         or black pixels from outside the image.
     (2) The vertical band extends the full height of pixd.
     (3) If a colormap exists, the nearest color to white or black
         is brought in.

Definition at line 283 of file rop.c.

References L_BRING_IN_BLACK, L_BRING_IN_WHITE, pixGetData(), pixGetDimensions(), and rasteropVipLow().

Referenced by pixRasteropIP().

◆ pixTranslate()

PIX* pixTranslate ( PIX pixd,
PIX pixs,
l_int32  hshift,
l_int32  vshift,
l_int32  incolor 
)

pixTranslate()

Parameters
[in]pixd[optional] destination: this can be null, equal to pixs, or different from pixs
[in]pixs
[in]hshifthorizontal shift; hshift > 0 is to right
[in]vshiftvertical shift; vshift > 0 is down
[in]incolorL_BRING_IN_WHITE, L_BRING_IN_BLACK
Returns
pixd, or NULL on error.
Notes:
     (1) The general pattern is:
           pixd = pixTranslate(pixd, pixs, ...);
         For clarity, when you know the case, use one of these:
           pixd = pixTranslate(NULL, pixs, ...);  // new
           pixTranslate(pixs, pixs, ...);         // in-place
           pixTranslate(pixd, pixs, ...);         // to existing pixd
     (2) If an existing pixd is not the same size as pixs, the
         image data will be reallocated.

Definition at line 445 of file rop.c.

References pixCopy(), and pixRasteropIP().

Referenced by pixaTranslate(), and pixWarpStereoscopic().