Leptonica  1.54
Файл src/coloring.c
#include "allheaders.h"

Функции

PIXpixColorGrayRegions (PIX *pixs, BOXA *boxa, l_int32 type, l_int32 thresh, l_int32 rval, l_int32 gval, l_int32 bval)
l_int32 pixColorGray (PIX *pixs, BOX *box, l_int32 type, l_int32 thresh, l_int32 rval, l_int32 gval, l_int32 bval)
PIXpixColorGrayMasked (PIX *pixs, PIX *pixm, 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)
PIXpixShiftByComponent (PIX *pixd, PIX *pixs, l_uint32 srcval, l_uint32 dstval)
l_int32 pixelShiftByComponent (l_int32 rval, l_int32 gval, l_int32 bval, l_uint32 srcval, l_uint32 dstval, l_uint32 *ppixel)
l_int32 pixelFractionalShift (l_int32 rval, l_int32 gval, l_int32 bval, l_float32 fraction, l_uint32 *ppixel)

Функции

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.

PIX* pixColorGrayMasked ( PIX pixs,
PIX pixm,
l_int32  type,
l_int32  thresh,
l_int32  rval,
l_int32  gval,
l_int32  bval 
)

pixColorGrayMasked()

Input: pixs (8 bpp gray, rgb or colormapped image) pixm (1 bpp mask, through which to apply color) type (L_PAINT_LIGHT, L_PAINT_DARK) thresh (average value below/above which pixel is unchanged) rval, gval, bval (new color to paint) Return: pixd (colorized), or null on error

Notes: (1) This generates a new image, where some of the pixels under FG in the mask are colorized. (2) See pixColorGray() for usage with and . Note that is only used for rgb; it is ignored for colormapped images. In most cases, the mask will be over the darker parts and == L_PAINT_DARK. (3) If pixs is colormapped this calls pixColorMaskedCmap(), which adds colors to the colormap for pixd; it only adds colors corresponding to strictly gray colors in the colormap. Otherwise, if pixs is 8 bpp gray, pixd will be 32 bpp rgb. (4) If pixs is 32 bpp rgb, for each pixel a "gray" value is found by averaging. This average is then used with the input rgb target to generate the output pixel values. (5) This can be used in conjunction with pixFindColorRegions() to add highlight color to a grayscale image.

PIX* pixColorGrayRegions ( PIX pixs,
BOXA boxa,
l_int32  type,
l_int32  thresh,
l_int32  rval,
l_int32  gval,
l_int32  bval 
)

pixColorGrayRegions()

Input: pixs (2, 4 or 8 bpp gray, rgb, or colormapped) boxa (of regions in which to apply color) type (L_PAINT_LIGHT, L_PAINT_DARK) thresh (average value below/above which pixel is unchanged) rval, gval, bval (new color to paint) Return: pixd, or null on error

Notes: (1) This generates a new image, where some of the pixels in each box in the boxa are colorized. See pixColorGray() for usage with and . Note that is only used for rgb; it is ignored for colormapped images. (2) If the input image is colormapped, the new image will be 8 bpp colormapped if possible; otherwise, it will be converted to 32 bpp rgb. Only pixels that are strictly gray will be colorized. (3) If the input image is not colormapped, it is converted to rgb. A "gray" value for a pixel is determined by averaging the components, and the output rgb value is determined from this. (4) This can be used in conjunction with pixFindColorRegions() to add highlight color to a grayscale image.

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.

l_int32 pixelShiftByComponent ( l_int32  rval,
l_int32  gval,
l_int32  bval,
l_uint32  srcval,
l_uint32  dstval,
l_uint32 ppixel 
)

pixelShiftByComponent()

Input: rval, gval, bval srcval (source color: 0xrrggbb00) dstval (target color: 0xrrggbb00) &ppixel (<return> rgb value) Return: 0 if OK, 1 on error

Notes: (1) This is a linear transformation that gives the same result on a single pixel as pixShiftByComponent() gives on a pix. Each component is handled separately. If the dest component is larger than the src, then the component is pushed toward 255 by the same fraction as the src --> dest shift.

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* pixShiftByComponent ( PIX pixd,
PIX pixs,
l_uint32  srcval,
l_uint32  dstval 
)

pixShiftByComponent()

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 linear mapping of the colors in pixs to colors in pixd. Let rs and rd be the red src and dest components in and , and rval is the red component of the src pixel. Then for all pixels in pixs, the mapping for the red component from pixs to pixd is: if (rd <= rs) (shift toward black) rval --> (rd/rs) * rval if (rd > rs) (shift toward white) (255 - rval) --> ((255 - rs)/(255 - rd)) * (255 - rval) Thus if rd <= rs, the red component of all pixels is mapped by the same fraction toward white, and if rd > rs, they are mapped by the same fraction toward black. This is essentially a different linear TRC (gamma = 1) for each component. The source and target color inputs are just used to generate the three fractions. (2) Note that this mapping differs from that in pixLinearMapToTargetColor(), which maps rs --> rd and does a piecewise stretching in between. (3) For inplace operation, call it this way: pixFractionalShiftByComponent(pixs, pixs, ... ) (4) For generating a new pixd: pixd = pixLinearMapToTargetColor(NULL, pixs, ...) (5) A simple application is to color a grayscale image. A light background can be colored using srcval = 0xffffff00 and picking a target background color for dstval. A dark foreground can be colored by using srcval = 0x0 and choosing a target foreground color for dstval.

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'.