Leptonica 1.54
Файл src/blend.c
#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"

Функции

static l_int32 blendComponents (l_int32 a, l_int32 b, l_float32 fract)
static l_int32 blendHardLightComponents (l_int32 a, l_int32 b, l_float32 fract)
PIXpixBlend (PIX *pixs1, PIX *pixs2, l_int32 x, l_int32 y, l_float32 fract)
PIXpixBlendMask (PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 x, l_int32 y, l_float32 fract, l_int32 type)
PIXpixBlendGray (PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 x, l_int32 y, l_float32 fract, l_int32 type, l_int32 transparent, l_uint32 transpix)
PIXpixBlendColor (PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 x, l_int32 y, l_float32 fract, l_int32 transparent, l_uint32 transpix)
PIXpixBlendColorByChannel (PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 x, l_int32 y, l_float32 rfract, l_float32 gfract, l_float32 bfract, l_int32 transparent, l_uint32 transpix)
PIXpixBlendGrayAdapt (PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 x, l_int32 y, l_float32 fract, l_int32 shift)
PIXpixFadeWithGray (PIX *pixs, PIX *pixb, l_float32 factor, l_int32 type)
PIXpixBlendHardLight (PIX *pixd, PIX *pixs1, PIX *pixs2, l_int32 x, l_int32 y, l_float32 fract)
l_int32 pixBlendCmap (PIX *pixs, PIX *pixb, l_int32 x, l_int32 y, l_int32 sindex)
PIXpixBlendWithGrayMask (PIX *pixs1, PIX *pixs2, PIX *pixg, l_int32 x, l_int32 y)
l_int32 pixColorGray (PIX *pixs, BOX *box, l_int32 type, l_int32 thresh, l_int32 rval, l_int32 gval, l_int32 bval)
PIXpixSnapColor (PIX *pixd, PIX *pixs, l_uint32 srcval, l_uint32 dstval, l_int32 diff)
PIXpixSnapColorCmap (PIX *pixd, PIX *pixs, l_uint32 srcval, l_uint32 dstval, l_int32 diff)
PIXpixLinearMapToTargetColor (PIX *pixd, PIX *pixs, l_uint32 srcval, l_uint32 dstval)
l_int32 pixelLinearMapToTargetColor (l_uint32 scolor, l_uint32 srcmap, l_uint32 dstmap, l_uint32 *pdcolor)
l_int32 pixelFractionalShift (l_int32 rval, l_int32 gval, l_int32 bval, l_float32 fraction, l_uint32 *ppixel)

Функции

static l_int32 blendComponents ( l_int32  a,
l_int32  b,
l_float32  fract 
) [static]
static l_int32 blendHardLightComponents ( l_int32  a,
l_int32  b,
l_float32  fract 
) [static]
PIX* pixBlend ( PIX pixs1,
PIX pixs2,
l_int32  x,
l_int32  y,
l_float32  fract 
)

pixBlend()

Input: pixs1 (blendee) pixs2 (blender; typ. smaller) x,y (origin (UL corner) of pixs2 relative to the origin of pixs1; can be < 0) fract (blending fraction) Return: pixd (blended image), or null on error

Notes: (1) This is a simple top-level interface. For more flexibility, call directly into pixBlendMask(), etc.

l_int32 pixBlendCmap ( PIX pixs,
PIX pixb,
l_int32  x,
l_int32  y,
l_int32  sindex 
)

pixBlendCmap()

Input: pixs (2, 4 or 8 bpp, with colormap) pixb (colormapped blender) x, y (UL corner of blender relative to pixs) sindex (colormap index of pixels in pixs to be changed) Return: 0 if OK, 1 on error

Note: (1) This function combines two colormaps, and replaces the pixels in pixs that have a specified color value with those in pixb. (2) sindex must be in the existing colormap; otherwise an error is returned. In use, sindex will typically be the index for white (255, 255, 255). (3) Blender colors that already exist in the colormap are used; others are added. If any blender colors cannot be stored in the colormap, an error is returned. (4) In the implementation, a mapping is generated from each original blender colormap index to the corresponding index in the expanded colormap for pixs. Then for each pixel in pixs with value sindex, and which is covered by a blender pixel, the new index corresponding to the blender pixel is substituted for sindex.

PIX* pixBlendColor ( PIX pixd,
PIX pixs1,
PIX pixs2,
l_int32  x,
l_int32  y,
l_float32  fract,
l_int32  transparent,
l_uint32  transpix 
)

pixBlendColor()

Input: pixd (<optional>; either NULL or equal to pixs1 for in-place) pixs1 (blendee; depth > 1) pixs2 (blender, 32 bpp; typ. smaller in size than pixs1) x,y (origin (UL corner) of pixs2 relative to the origin of pixs1) fract (blending fraction) transparent (1 to use transparency; 0 otherwise) transpix (pixel color in pixs2 that is to be transparent) Return: pixd if OK; pixs1 on error

Notes: (1) pixs2 must be 32 bpp, and have no colormap. (2) Clipping of pixs2 to pixs1 is done in the inner pixel loop. (3) If pixs1 has a colormap, it is removed to generate a 32 bpp pix. (4) If pixs1 has depth < 32, it is unpacked to generate a 32 bpp pix. (5) For inplace operation, call it this way: pixBlendColor(pixs1, pixs1, pixs2, ...) (6) For generating a new pixd: pixd = pixBlendColor(NULL, pixs1, pixs2, ...) (7) Only call in-place if pixs1 is 32 bpp; otherwise it is an error. (8) If transparent = 0, the blending fraction (fract) is applied equally to all pixels. (9) If transparent = 1, all pixels of value transpix (typically either 0 or 0xffffff00) in pixs2 are transparent in the blend.

PIX* pixBlendColorByChannel ( PIX pixd,
PIX pixs1,
PIX pixs2,
l_int32  x,
l_int32  y,
l_float32  rfract,
l_float32  gfract,
l_float32  bfract,
l_int32  transparent,
l_uint32  transpix 
)
PIX* pixBlendGray ( PIX pixd,
PIX pixs1,
PIX pixs2,
l_int32  x,
l_int32  y,
l_float32  fract,
l_int32  type,
l_int32  transparent,
l_uint32  transpix 
)

pixBlendGray()

Input: pixd (<optional>; either NULL or equal to pixs1 for in-place) pixs1 (blendee; depth > 1) pixs2 (blender, 8 bpp; typ. smaller in size than pixs1) x,y (origin (UL corner) of pixs2 relative to the origin of pixs1; can be < 0) fract (blending fraction) type (L_BLEND_GRAY, L_BLEND_GRAY_WITH_INVERSE) transparent (1 to use transparency; 0 otherwise) transpix (pixel grayval in pixs2 that is to be transparent) Return: pixd if OK; pixs1 on error

Notes: (1) pixs2 must be 8 bpp, and have no colormap. (2) Clipping of pixs2 to pixs1 is done in the inner pixel loop. (3) If pixs1 has a colormap, it is removed. (4) If pixs1 has depth < 8, it is unpacked to generate a 8 bpp pix. (5) For inplace operation, call it this way: pixBlendGray(pixs1, pixs1, pixs2, ...) (6) For generating a new pixd: pixd = pixBlendGray(NULL, pixs1, pixs2, ...) (7) Only call in-place if pixs1 does not have a colormap; otherwise it is an error. (8) If transparent = 0, the blending fraction (fract) is applied equally to all pixels. (9) If transparent = 1, all pixels of value transpix (typically either 0 or 0xff) in pixs2 are transparent in the blend. (10) After processing pixs1, it is either 8 bpp or 32 bpp:

  • if 8 bpp, the fraction of pixs2 is mixed with pixs1.
  • if 32 bpp, each component of pixs1 is mixed with the same fraction of pixs2. (11) For L_BLEND_GRAY_WITH_INVERSE, the white values of the blendee (cval == 255 in the code below) result in a delta of 0. Thus, these pixels are intrinsically transparent! The "pivot" value of the src, at which no blending occurs, is 128. Compare with the adaptive pivot in pixBlendGrayAdapt().
PIX* pixBlendGrayAdapt ( PIX pixd,
PIX pixs1,
PIX pixs2,
l_int32  x,
l_int32  y,
l_float32  fract,
l_int32  shift 
)

pixBlendGrayAdapt()

Input: pixd (<optional>; either NULL or equal to pixs1 for in-place) pixs1 (blendee; depth > 1) pixs2 (blender, 8 bpp; typ. smaller in size than pixs1) x,y (origin (UL corner) of pixs2 relative to the origin of pixs1; can be < 0) fract (blending fraction) shift (>= 0 but <= 128: shift of zero blend value from median source; use -1 for default value; ) Return: pixd if OK; pixs1 on error

Notes: (1) pixs2 must be 8 bpp, and have no colormap. (2) Clipping of pixs2 to pixs1 is done in the inner pixel loop. (3) If pixs1 has a colormap, it is removed. (4) If pixs1 has depth < 8, it is unpacked to generate a 8 bpp pix. (5) For inplace operation, call it this way: pixBlendGray(pixs1, pixs1, pixs2, ...) For generating a new pixd: pixd = pixBlendGray(NULL, pixs1, pixs2, ...) Only call in-place if pixs1 does not have a colormap; otherwise it is an error. (6) This does a blend with inverse. Whereas in pixGlendGray(), the zero blend point is where the blendee pixel is 128, here the zero blend point is found adaptively, with respect to the median of the blendee region. If the median is < 128, the zero blend point is found from median + shift. Otherwise, if the median >= 128, the zero blend point is median - shift. The purpose of shifting the zero blend point away from the median is to prevent a situation in pixBlendGray() where the median is 128 and the blender is not visible. The default value of shift is 64. (7) After processing pixs1, it is either 8 bpp or 32 bpp:

  • if 8 bpp, the fraction of pixs2 is mixed with pixs1.
  • if 32 bpp, each component of pixs1 is mixed with the same fraction of pixs2. (8) The darker the blender, the more it mixes with the blendee. A blender value of 0 has maximum mixing; a value of 255 has no mixing and hence is transparent.
PIX* pixBlendHardLight ( PIX pixd,
PIX pixs1,
PIX pixs2,
l_int32  x,
l_int32  y,
l_float32  fract 
)
PIX* pixBlendMask ( PIX pixd,
PIX pixs1,
PIX pixs2,
l_int32  x,
l_int32  y,
l_float32  fract,
l_int32  type 
)

pixBlendMask()

Input: pixd (<optional>; either NULL or equal to pixs1 for in-place) pixs1 (blendee; depth > 1) pixs2 (blender; typ. smaller in size than pixs1) x,y (origin (UL corner) of pixs2 relative to the origin of pixs1; can be < 0) fract (blending fraction) type (L_BLEND_WITH_INVERSE, L_BLEND_TO_WHITE, L_BLEND_TO_BLACK) Return: pixd if OK; pixs1 on error

Notes: (1) pixs2 must be 1 bpp (2) Clipping of pixs2 to pixs1 is done in the inner pixel loop. (3) If pixs1 has a colormap, it is removed. (4) For inplace operation, call it this way: pixBlendMask(pixs1, pixs1, pixs2, ...) (5) For generating a new pixd: pixd = pixBlendMask(NULL, pixs1, pixs2, ...) (6) Only call in-place if pixs1 does not have a colormap.

PIX* pixBlendWithGrayMask ( PIX pixs1,
PIX pixs2,
PIX pixg,
l_int32  x,
l_int32  y 
)

pixBlendWithGrayMask()

Input: pixs1 (8 bpp gray, rgb or colormapped) pixs2 (8 bpp gray, rgb or colormapped) pixg (8 bpp gray, for transparency of pixs2; can be null) x, y (UL corner of pixg with respect to pixs1) Return: pixd (blended image), or null on error

Notes: (1) The result is 8 bpp grayscale if both pixs1 and pixs2 are 8 bpp gray. Otherwise, the result is 32 bpp rgb. (2) pixg is an 8 bpp transparency image, where 0 is transparent and 255 is opaque. It determines the transparency of pixs2 when applied over pixs1. It can be null if pixs2 is rgb, in which case we use the alpha component of pixs2. (3) If pixg exists, both it and pixs2 must be the same size, and they are applied with both their UL corners at the location (x, y) in pixs1. (4) The pixels in pixd are a combination of those in pixs1 and pixs2, where the amount from pixs2 is proportional to the value of the pixel (p) in pixg, and the amount from pixs1 is proportional to (255 - p). Thus pixg is a transparency image (usually called an alpha blender) where each pixel can be associated with a pixel in pixs2, and determines the amount of the pixs2 pixel in the final result. For example, if pixg is all 0, pixs2 is transparent and the result in pixd is simply pixs1. (5) A typical use is for the pixs2/pixg combination to be a small watermark that is applied to pixs1.

l_int32 pixColorGray ( PIX pixs,
BOX box,
l_int32  type,
l_int32  thresh,
l_int32  rval,
l_int32  gval,
l_int32  bval 
)

pixColorGray()

Input: pixs (8 bpp gray, rgb or colormapped image) box (<optional> region in which to apply color; can be NULL) type (L_PAINT_LIGHT, L_PAINT_DARK) thresh (average value below/above which pixel is unchanged) rval, gval, bval (new color to paint) Return: 0 if OK; 1 on error

Notes: (1) This is an in-place operation; pixs is modified. If pixs is colormapped, the operation will add colors to the colormap. Otherwise, pixs will be converted to 32 bpp rgb if it is initially 8 bpp gray. (2) If type == L_PAINT_LIGHT, it colorizes non-black pixels, preserving antialiasing. If type == L_PAINT_DARK, it colorizes non-white pixels, preserving antialiasing. (3) If box is NULL, applies function to the entire image; otherwise, clips the operation to the intersection of the box and pix. (4) If colormapped, calls pixColorGrayCmap(), which applies the coloring algorithm only to pixels that are strictly gray. (5) For RGB, determines a "gray" value by averaging; then uses this value, plus the input rgb target, to generate the output pixel values. (6) thresh is only used for rgb; it is ignored for colormapped pix. If type == L_PAINT_LIGHT, use thresh = 0 if all pixels are to be colored (black pixels will be unaltered). In situations where there are a lot of black pixels, setting thresh > 0 will make the function considerably more efficient without affecting the final result. If type == L_PAINT_DARK, use thresh = 255 if all pixels are to be colored (white pixels will be unaltered). In situations where there are a lot of white pixels, setting thresh < 255 will make the function considerably more efficient without affecting the final result.

l_int32 pixelFractionalShift ( l_int32  rval,
l_int32  gval,
l_int32  bval,
l_float32  fraction,
l_uint32 ppixel 
)

pixelFractionalShift()

Input: rval, gval, bval fraction (negative toward black; positive toward white) &ppixel (<return> rgb value) Return: 0 if OK, 1 on error

Notes: (1) This transformation leaves the hue invariant, while changing the saturation and intensity. It can be used for that purpose in pixLinearMapToTargetColor(). (2) is in the range [-1 .... +1]. If < 0, saturation is increased and brightness is reduced. The opposite results if > 0. If == -1, the resulting pixel is black; == 1 results in white.

l_int32 pixelLinearMapToTargetColor ( l_uint32  scolor,
l_uint32  srcmap,
l_uint32  dstmap,
l_uint32 pdcolor 
)

pixelLinearMapToTargetColor()

Input: scolor (rgb source color: 0xrrggbb00) srcmap (source mapping color: 0xrrggbb00) dstmap (target mapping color: 0xrrggbb00) &pdcolor (<return> rgb dest color: 0xrrggbb00) Return: 0 if OK, 1 on error

Notes: (1) This does this does a piecewise linear mapping of each component of to , based on the relation between the components of and . It is the same transformation, performed on a single color, as mapped on every pixel in a pix by pixLinearMapToTargetColor(). (2) For each component, if the sval is larger than the smap, the dval will be pushed up from dmap towards white. Otherwise, dval will be pushed down from dmap towards black. This is because you can visualize the transformation as a linear stretching where smap moves to dmap, and everything else follows linearly with 0 and 255 fixed. (3) The mapping will in general change the hue of . However, if the and targets are related by a transformation given by pixelFractionalShift(), the hue will be invariant.

PIX* pixFadeWithGray ( PIX pixs,
PIX pixb,
l_float32  factor,
l_int32  type 
)

pixFadeWithGray()

Input: pixs (colormapped or 8 bpp or 32 bpp) pixb (8 bpp blender) factor (multiplicative factor to apply to blender value) type (L_BLEND_TO_WHITE, L_BLEND_TO_BLACK) Return: pixd, or null on error

Notes: (1) This function combines two pix aligned to the UL corner; they need not be the same size. (2) Each pixel in pixb is multiplied by 'factor' divided by 255, and clipped to the range [0 ... 1]. This gives the fade fraction to be appied to pixs. Fade either to white (L_BLEND_TO_WHITE) or to black (L_BLEND_TO_BLACK).

PIX* pixLinearMapToTargetColor ( PIX pixd,
PIX pixs,
l_uint32  srcval,
l_uint32  dstval 
)

pixLinearMapToTargetColor()

Input: pixd (<optional>; either NULL or equal to pixs for in-place) pixs (32 bpp rgb) srcval (source color: 0xrrggbb00) dstval (target color: 0xrrggbb00) Return: pixd (with all pixels mapped based on the srcval/destval mapping), or pixd on error

Notes: (1) For each component (r, b, g) separately, this does a piecewise linear mapping of the colors in pixs to colors in pixd. If rs and rd are the red src and dest components in and , then the range [0 ... rs] in pixs is mapped to [0 ... rd] in pixd. Likewise, the range [rs ... 255] in pixs is mapped to [rd ... 255] in pixd. And similarly for green and blue. (2) The mapping will in general change the hue of the pixels. However, if the src and dst targets are related by a transformation given by pixelFractionalShift(), the hue is invariant. (3) For inplace operation, call it this way: pixLinearMapToTargetColor(pixs, pixs, ... ) (4) For generating a new pixd: pixd = pixLinearMapToTargetColor(NULL, pixs, ...)

PIX* pixSnapColor ( PIX pixd,
PIX pixs,
l_uint32  srcval,
l_uint32  dstval,
l_int32  diff 
)

pixSnapColor()

Input: pixd (<optional>; either NULL or equal to pixs for in-place) pixs (colormapped or 8 bpp gray or 32 bpp rgb) srcval (color center to be selected for change: 0xrrggbb00) dstval (target color for pixels: 0xrrggbb00) diff (max absolute difference, applied to all components) Return: pixd (with all pixels within diff of pixval set to pixval), or pixd on error

Notes: (1) For inplace operation, call it this way: pixSnapColor(pixs, pixs, ... ) (2) For generating a new pixd: pixd = pixSnapColor(NULL, pixs, ...) (3) If pixs has a colormap, it is handled by pixSnapColorCmap(). (4) All pixels within 'diff' of 'srcval', componentwise, will be changed to 'dstval'.

PIX* pixSnapColorCmap ( PIX pixd,
PIX pixs,
l_uint32  srcval,
l_uint32  dstval,
l_int32  diff 
)

pixSnapColorCmap()

Input: pixd (<optional>; either NULL or equal to pixs for in-place) pixs (colormapped) srcval (color center to be selected for change: 0xrrggbb00) dstval (target color for pixels: 0xrrggbb00) diff (max absolute difference, applied to all components) Return: pixd (with all pixels within diff of srcval set to dstval), or pixd on error

Notes: (1) For inplace operation, call it this way: pixSnapCcmap(pixs, pixs, ... ) (2) For generating a new pixd: pixd = pixSnapCmap(NULL, pixs, ...) (3) pixs must have a colormap. (4) All colors within 'diff' of 'srcval', componentwise, will be changed to 'dstval'.