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

Go to the source code of this file.

Functions

PIXCMAPpixcmapCreate (l_int32 depth)
 
PIXCMAPpixcmapCreateRandom (l_int32 depth, l_int32 hasblack, l_int32 haswhite)
 
PIXCMAPpixcmapCreateLinear (l_int32 d, l_int32 nlevels)
 
PIXCMAPpixcmapCopy (const PIXCMAP *cmaps)
 
void pixcmapDestroy (PIXCMAP **pcmap)
 
l_ok pixcmapIsValid (const PIXCMAP *cmap, PIX *pix, l_int32 *pvalid)
 
l_ok pixcmapAddColor (PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval)
 
l_ok pixcmapAddRGBA (PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 aval)
 
l_ok pixcmapAddNewColor (PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 *pindex)
 
l_ok pixcmapAddNearestColor (PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 *pindex)
 
l_ok pixcmapUsableColor (PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 *pusable)
 
l_ok pixcmapAddBlackOrWhite (PIXCMAP *cmap, l_int32 color, l_int32 *pindex)
 
l_ok pixcmapSetBlackAndWhite (PIXCMAP *cmap, l_int32 setblack, l_int32 setwhite)
 
l_int32 pixcmapGetCount (const PIXCMAP *cmap)
 
l_int32 pixcmapGetFreeCount (PIXCMAP *cmap)
 
l_int32 pixcmapGetDepth (PIXCMAP *cmap)
 
l_ok pixcmapGetMinDepth (PIXCMAP *cmap, l_int32 *pmindepth)
 
l_ok pixcmapClear (PIXCMAP *cmap)
 
l_ok pixcmapGetColor (PIXCMAP *cmap, l_int32 index, l_int32 *prval, l_int32 *pgval, l_int32 *pbval)
 
l_ok pixcmapGetColor32 (PIXCMAP *cmap, l_int32 index, l_uint32 *pval32)
 
l_ok pixcmapGetRGBA (PIXCMAP *cmap, l_int32 index, l_int32 *prval, l_int32 *pgval, l_int32 *pbval, l_int32 *paval)
 
l_ok pixcmapGetRGBA32 (PIXCMAP *cmap, l_int32 index, l_uint32 *pval32)
 
l_ok pixcmapResetColor (PIXCMAP *cmap, l_int32 index, l_int32 rval, l_int32 gval, l_int32 bval)
 
l_ok pixcmapSetAlpha (PIXCMAP *cmap, l_int32 index, l_int32 aval)
 
l_int32 pixcmapGetIndex (PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 *pindex)
 
l_ok pixcmapHasColor (PIXCMAP *cmap, l_int32 *pcolor)
 
l_ok pixcmapIsOpaque (PIXCMAP *cmap, l_int32 *popaque)
 
l_ok pixcmapNonOpaqueColorsInfo (PIXCMAP *cmap, l_int32 *pntrans, l_int32 *pmax_trans, l_int32 *pmin_opaque)
 
l_ok pixcmapIsBlackAndWhite (PIXCMAP *cmap, l_int32 *pblackwhite)
 
l_ok pixcmapCountGrayColors (PIXCMAP *cmap, l_int32 *pngray)
 
l_ok pixcmapGetRankIntensity (PIXCMAP *cmap, l_float32 rankval, l_int32 *pindex)
 
l_ok pixcmapGetNearestIndex (PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 *pindex)
 
l_ok pixcmapGetNearestGrayIndex (PIXCMAP *cmap, l_int32 val, l_int32 *pindex)
 
l_ok pixcmapGetDistanceToColor (PIXCMAP *cmap, l_int32 index, l_int32 rval, l_int32 gval, l_int32 bval, l_int32 *pdist)
 
l_ok pixcmapGetRangeValues (PIXCMAP *cmap, l_int32 select, l_int32 *pminval, l_int32 *pmaxval, l_int32 *pminindex, l_int32 *pmaxindex)
 
PIXCMAPpixcmapGrayToFalseColor (l_float32 gamma)
 
PIXCMAPpixcmapGrayToColor (l_uint32 color)
 
PIXCMAPpixcmapColorToGray (PIXCMAP *cmaps, l_float32 rwt, l_float32 gwt, l_float32 bwt)
 
PIXCMAPpixcmapConvertTo4 (PIXCMAP *cmaps)
 
PIXCMAPpixcmapConvertTo8 (PIXCMAP *cmaps)
 
PIXCMAPpixcmapRead (const char *filename)
 
PIXCMAPpixcmapReadStream (FILE *fp)
 
PIXCMAPpixcmapReadMem (const l_uint8 *data, size_t size)
 
l_ok pixcmapWrite (const char *filename, const PIXCMAP *cmap)
 
l_ok pixcmapWriteStream (FILE *fp, const PIXCMAP *cmap)
 
l_ok pixcmapWriteMem (l_uint8 **pdata, size_t *psize, const PIXCMAP *cmap)
 
l_ok pixcmapToArrays (const PIXCMAP *cmap, l_int32 **prmap, l_int32 **pgmap, l_int32 **pbmap, l_int32 **pamap)
 
l_ok pixcmapToRGBTable (PIXCMAP *cmap, l_uint32 **ptab, l_int32 *pncolors)
 
l_ok pixcmapSerializeToMemory (PIXCMAP *cmap, l_int32 cpc, l_int32 *pncolors, l_uint8 **pdata)
 
PIXCMAPpixcmapDeserializeFromMemory (l_uint8 *data, l_int32 cpc, l_int32 ncolors)
 
char * pixcmapConvertToHex (l_uint8 *data, l_int32 ncolors)
 
l_ok pixcmapGammaTRC (PIXCMAP *cmap, l_float32 gamma, l_int32 minval, l_int32 maxval)
 
l_ok pixcmapContrastTRC (PIXCMAP *cmap, l_float32 factor)
 
l_ok pixcmapShiftIntensity (PIXCMAP *cmap, l_float32 fraction)
 
l_ok pixcmapShiftByComponent (PIXCMAP *cmap, l_uint32 srcval, l_uint32 dstval)
 

Detailed Description

     Colormap creation, copy, destruction, addition
          PIXCMAP    *pixcmapCreate()
          PIXCMAP    *pixcmapCreateRandom()
          PIXCMAP    *pixcmapCreateLinear()
          PIXCMAP    *pixcmapCopy()
          void        pixcmapDestroy()
          l_int32     pixcmapIsValid()
          l_int32     pixcmapAddColor()
          l_int32     pixcmapAddRGBA()
          l_int32     pixcmapAddNewColor()
          l_int32     pixcmapAddNearestColor()
          l_int32     pixcmapUsableColor()
          l_int32     pixcmapAddBlackOrWhite()
          l_int32     pixcmapSetBlackAndWhite()
          l_int32     pixcmapGetCount()
          l_int32     pixcmapGetDepth()
          l_int32     pixcmapGetMinDepth()
          l_int32     pixcmapGetFreeCount()
          l_int32     pixcmapClear()
     Colormap random access and test
          l_int32     pixcmapGetColor()
          l_int32     pixcmapGetColor32()
          l_int32     pixcmapGetRGBA()
          l_int32     pixcmapGetRGBA32()
          l_int32     pixcmapResetColor()
          l_int32     pixcmapSetAlpha()
          l_int32     pixcmapGetIndex()
          l_int32     pixcmapHasColor()
          l_int32     pixcmapIsOpaque()
          l_int32     pixcmapNonOpaqueColorsInfo()
          l_int32     pixcmapIsBlackAndWhite()
          l_int32     pixcmapCountGrayColors()
          l_int32     pixcmapGetRankIntensity()
          l_int32     pixcmapGetNearestIndex()
          l_int32     pixcmapGetNearestGrayIndex()
          l_int32     pixcmapGetDistanceToColor()
          l_int32     pixcmapGetRangeValues()
     Colormap conversion
          PIXCMAP    *pixcmapGrayToFalseColor()
          PIXCMAP    *pixcmapGrayToColor()
          PIXCMAP    *pixcmapColorToGray()
          PIXCMAP    *pixcmapConvertTo4()
          PIXCMAP    *pixcmapConvertTo8()
     Colormap I/O
          l_int32     pixcmapRead()
          l_int32     pixcmapReadStream()
          l_int32     pixcmapReadMem()
          l_int32     pixcmapWrite()
          l_int32     pixcmapWriteStream()
          l_int32     pixcmapWriteMem()
     Extract colormap arrays and serialization
          l_int32     pixcmapToArrays()
          l_int32     pixcmapToRGBTable()
          l_int32     pixcmapSerializeToMemory()
          PIXCMAP    *pixcmapDeserializeFromMemory()
          char       *pixcmapConvertToHex()
     Colormap transforms
          l_int32     pixcmapGammaTRC()
          l_int32     pixcmapContrastTRC()
          l_int32     pixcmapShiftIntensity()
          l_int32     pixcmapShiftByComponent()
 Note:
     (1) colormaps in leptonica have a maximum of 256 entries.
     (2) nalloc, the allocated size of the palette array, is related
         to the depth d of the pixels by:
                nalloc = 2^(d)
 

Definition in file colormap.c.

Function Documentation

◆ pixcmapAddBlackOrWhite()

l_ok pixcmapAddBlackOrWhite ( PIXCMAP cmap,
l_int32  color,
l_int32 *  pindex 
)

pixcmapAddBlackOrWhite()

Parameters
[in]cmap
[in]color0 for black, 1 for white
[out]pindex[optional] index of color; can be null
Returns
0 if OK, 1 on error
Notes:
     (1) This only adds color if not already there.
     (2) The alpha component is 255 (opaque)
     (3) This sets index to the requested color.
     (4) If there is no room in the colormap, returns the index
         of the closest color.

Definition at line 639 of file colormap.c.

References pixcmapAddNewColor(), pixcmapGetFreeCount(), and pixcmapGetRankIntensity().

◆ pixcmapAddColor()

l_ok pixcmapAddColor ( PIXCMAP cmap,
l_int32  rval,
l_int32  gval,
l_int32  bval 
)

pixcmapAddColor()

Parameters
[in]cmap
[in]rval,gval,bvalcolormap entry to be added; each number is in range [0, ... 255]
Returns
0 if OK, 1 on error
Notes:
     (1) This always adds the color if there is room.
     (2) The alpha component is 255 (opaque)

Definition at line 414 of file colormap.c.

References RGBA_Quad::alpha, PixColormap::array, RGBA_Quad::blue, RGBA_Quad::green, PixColormap::n, PixColormap::nalloc, and RGBA_Quad::red.

Referenced by fpixRenderContours(), makeColorfillTestData(), makeGrayQuantTableArb(), numaFillCmapFromHisto(), pixcmapAddNearestColor(), pixcmapAddNewColor(), pixcmapConvertTo4(), pixcmapConvertTo8(), pixcmapCreateLinear(), pixcmapCreateRandom(), pixcmapGenerateFromMedianCuts(), pixcmapGrayToColor(), pixcmapGrayToFalseColor(), pixCreateWithCmap(), and pixDisplayDiffBinary().

◆ pixcmapAddNearestColor()

l_ok pixcmapAddNearestColor ( PIXCMAP cmap,
l_int32  rval,
l_int32  gval,
l_int32  bval,
l_int32 *  pindex 
)

pixcmapAddNearestColor()

Parameters
[in]cmap
[in]rval,gval,bvalcolormap entry to be added; each number is in range [0, ... 255]
[out]pindexindex of color
Returns
0 if OK, 1 on error
Notes:
     (1) This only adds color if not already there.
     (2) The alpha component is 255 (opaque)
     (3) If it's not in the colormap and there is no room to add
         another color, this returns the index of the nearest color.

Definition at line 545 of file colormap.c.

References PixColormap::n, PixColormap::nalloc, pixcmapAddColor(), pixcmapGetCount(), pixcmapGetIndex(), and pixcmapGetNearestIndex().

◆ pixcmapAddNewColor()

l_ok pixcmapAddNewColor ( PIXCMAP cmap,
l_int32  rval,
l_int32  gval,
l_int32  bval,
l_int32 *  pindex 
)

pixcmapAddNewColor()

Parameters
[in]cmap
[in]rval,gval,bvalcolormap entry to be added; each number is in range [0, ... 255]
[out]pindexindex of color
Returns
0 if OK, 1 on error; 2 if unable to add color
Notes:
     (1) This only adds color if not already there.
     (2) The alpha component is 255 (opaque)
     (3) This returns the index of the new (or existing) color.
     (4) Returns 2 with a warning if unable to add this color;
         the caller should check the return value.

Definition at line 496 of file colormap.c.

References PixColormap::n, PixColormap::nalloc, pixcmapAddColor(), pixcmapGetCount(), and pixcmapGetIndex().

Referenced by addColorizedGrayToCmap(), and pixcmapAddBlackOrWhite().

◆ pixcmapAddRGBA()

l_ok pixcmapAddRGBA ( PIXCMAP cmap,
l_int32  rval,
l_int32  gval,
l_int32  bval,
l_int32  aval 
)

pixcmapAddRGBA()

Parameters
[in]cmap
[in]rval,gval,bval,avalcolormap entry to be added; each number is in range [0, ... 255]
Returns
0 if OK, 1 on error
Notes:
     (1) This always adds the color if there is room.

Definition at line 452 of file colormap.c.

References RGBA_Quad::alpha, PixColormap::array, RGBA_Quad::blue, RGBA_Quad::green, PixColormap::n, PixColormap::nalloc, and RGBA_Quad::red.

Referenced by pixcmapDeserializeFromMemory(), and pixcmapReadStream().

◆ pixcmapClear()

l_ok pixcmapClear ( PIXCMAP cmap)

pixcmapClear()

Parameters
[in]cmap
Returns
0 if OK, 1 on error
Notes:
     (1) This removes the colors by setting the count to 0.

Definition at line 801 of file colormap.c.

References PixColormap::n.

◆ pixcmapColorToGray()

PIXCMAP* pixcmapColorToGray ( PIXCMAP cmaps,
l_float32  rwt,
l_float32  gwt,
l_float32  bwt 
)

pixcmapColorToGray()

Parameters
[in]cmaps
[in]rwt,gwt,bwtnon-negative; these should add to 1.0
Returns
cmap gray, or NULL on error
Notes:
     (1) This creates a gray colormap from an arbitrary colormap.
     (2) In use, attach the output gray colormap to the pix
         (or a copy of it) that provided the input colormap.

Definition at line 1710 of file colormap.c.

References pixcmapCopy(), pixcmapGetColor(), pixcmapGetCount(), and pixcmapResetColor().

◆ pixcmapContrastTRC()

l_ok pixcmapContrastTRC ( PIXCMAP cmap,
l_float32  factor 
)

pixcmapContrastTRC()

Parameters
[in]cmapcolormap
[in]factorgenerally between 0.0 [no enhancement] and 1.0, but can be larger than 1.0
Returns
0 if OK; 1 on error
Notes:
     (1) This is an in-place transform
     (2) See pixContrastTRC() and numaContrastTRC() in enhance.c
         for description and use of transform

Definition at line 2385 of file colormap.c.

References numaContrastTRC(), numaDestroy(), numaGetIValue(), pixcmapGetColor(), pixcmapGetCount(), and pixcmapResetColor().

◆ pixcmapConvertTo4()

PIXCMAP* pixcmapConvertTo4 ( PIXCMAP cmaps)

pixcmapConvertTo4()

Parameters
[in]cmapscolormap for 2 bpp pix
Returns
cmapd (4 bpp)
Notes:
     (1) This converts a 2 bpp colormap to 4 bpp.  The colors
         are the same; the output colormap entry array has size 16.

Definition at line 1767 of file colormap.c.

References pixcmapAddColor(), pixcmapCreate(), pixcmapGetColor(), pixcmapGetCount(), and pixcmapGetDepth().

◆ pixcmapConvertTo8()

PIXCMAP* pixcmapConvertTo8 ( PIXCMAP cmaps)

pixcmapConvertTo8()

Parameters
[in]cmapscolormap for 2 bpp or 4 bpp pix
Returns
cmapd (8 bpp)
Notes:
     (1) This converts a 2 bpp or 4 bpp colormap to 8 bpp.  The colors
         are the same; the output colormap entry array has size 256.

Definition at line 1802 of file colormap.c.

References pixcmapAddColor(), pixcmapCopy(), pixcmapCreate(), pixcmapGetColor(), pixcmapGetCount(), and pixcmapGetDepth().

◆ pixcmapConvertToHex()

char* pixcmapConvertToHex ( l_uint8 *  data,
l_int32  ncolors 
)

pixcmapConvertToHex()

Parameters
[in]databinary serialized data
[in]ncolorsin colormap
Returns
hexdata bracketed, space-separated ascii hex string, or NULL on error.
Notes:
     (1) The number of bytes in data is 3 * ncolors.
     (2) Output is in form:
            < r0g0b0 r1g1b1 ... rngnbn >
         where r0, g0, b0 ... are each 2 bytes of hex ascii
     (3) This is used in pdf files to express the colormap as an
         array in ascii (human-readable) format.

Definition at line 2272 of file colormap.c.

◆ pixcmapCopy()

PIXCMAP* pixcmapCopy ( const PIXCMAP cmaps)

pixcmapCopy()

Parameters
[in]cmaps
Returns
cmapd, or NULL on error

Definition at line 248 of file colormap.c.

References PixColormap::nalloc, and pixcmapIsValid().

Referenced by pixcmapColorToGray(), and pixcmapConvertTo8().

◆ pixcmapCountGrayColors()

l_ok pixcmapCountGrayColors ( PIXCMAP cmap,
l_int32 *  pngray 
)

pixcmapCountGrayColors()

Parameters
[in]cmap
[out]pngraynumber of gray colors
Returns
0 if OK, 1 on error
Notes:
     (1) This counts the unique gray colors, including black and white.

Definition at line 1261 of file colormap.c.

References pixcmapGetColor(), and pixcmapGetCount().

◆ pixcmapCreate()

◆ pixcmapCreateLinear()

PIXCMAP* pixcmapCreateLinear ( l_int32  d,
l_int32  nlevels 
)

pixcmapCreateLinear()

Parameters
[in]ddepth of pix for this colormap; 1, 2, 4 or 8
[in]nlevelsvalid in range [2, 2^d]
Returns
cmap, or NULL on error
Notes:
     (1) Colormap has equally spaced gray color values
         from black (0, 0, 0) to white (255, 255, 255).

Definition at line 218 of file colormap.c.

References pixcmapAddColor(), and pixcmapCreate().

◆ pixcmapCreateRandom()

PIXCMAP* pixcmapCreateRandom ( l_int32  depth,
l_int32  hasblack,
l_int32  haswhite 
)

pixcmapCreateRandom()

Parameters
[in]depthof pix, in bpp: 2, 4 or 8
[in]hasblack1 if the first color is black; 0 if no black
[in]haswhite1 if the last color is white; 0 if no white
Returns
cmap, or NULL on error
Notes:
     (1) This sets up a colormap with random colors,
         where the first color is optionally black, the last color
         is optionally white, and the remaining colors are
         chosen randomly.
     (2) The number of randomly chosen colors is:
              2^(depth) - haswhite - hasblack
     (3) Because rand() is seeded, it might disrupt otherwise
         deterministic results if also used elsewhere in a program.
     (4) rand() is not threadsafe, and will generate garbage if run
         on multiple threads at once -- though garbage is generally
         what you want from a random number generator!
     (5) Modern rand()s have equal randomness in low and high order
         bits, but older ones don't.  Here, we're just using rand()
         to choose colors for output.

Definition at line 172 of file colormap.c.

References pixcmapAddColor(), and pixcmapCreate().

Referenced by pixaDisplayRandomCmap(), pixBlendBoxaRandom(), and pixRenderRandomCmapPtaa().

◆ pixcmapDeserializeFromMemory()

PIXCMAP* pixcmapDeserializeFromMemory ( l_uint8 *  data,
l_int32  cpc,
l_int32  ncolors 
)

pixcmapDeserializeFromMemory()

Parameters
[in]databinary string, 3 or 4 bytes per color
[in]cpccomponents/color: 3 for rgb, 4 for rgba
[in]ncolors> 0
Returns
cmap, or NULL on error

Definition at line 2211 of file colormap.c.

References pixcmapAddRGBA(), and pixcmapCreate().

◆ pixcmapDestroy()

void pixcmapDestroy ( PIXCMAP **  pcmap)

pixcmapDestroy()

Parameters
[in,out]pcmapset to null on return
Returns
void

Definition at line 279 of file colormap.c.

References PixColormap::array.

Referenced by pixBlendBoxaRandom(), pixcmapReadStream(), pixDestroyColormap(), and pixThresholdGrayArb().

◆ pixcmapGammaTRC()

l_ok pixcmapGammaTRC ( PIXCMAP cmap,
l_float32  gamma,
l_int32  minval,
l_int32  maxval 
)

pixcmapGammaTRC()

Parameters
[in]cmapcolormap
[in]gammagamma correction; must be > 0.0
[in]minvalinput value that gives 0 for output; can be < 0
[in]maxvalinput value that gives 255 for output; can be > 255
Returns
0 if OK; 1 on error
Notes:
     (1) This is an in-place transform
     (2) See pixGammaTRC() and numaGammaTRC() in enhance.c
         for description and use of transform

Definition at line 2330 of file colormap.c.

References numaDestroy(), numaGammaTRC(), numaGetIValue(), pixcmapGetColor(), pixcmapGetCount(), and pixcmapResetColor().

◆ pixcmapGetColor()

◆ pixcmapGetColor32()

l_ok pixcmapGetColor32 ( PIXCMAP cmap,
l_int32  index,
l_uint32 *  pval32 
)

pixcmapGetColor32()

Parameters
[in]cmap
[in]index
[out]pval3232-bit rgb color value
Returns
0 if OK, 1 if not accessible caller should check
Notes:
     (1) The returned alpha channel value is 255.

Definition at line 864 of file colormap.c.

References composeRGBAPixel(), and pixcmapGetColor().

◆ pixcmapGetCount()

◆ pixcmapGetDepth()

l_int32 pixcmapGetDepth ( PIXCMAP cmap)

pixcmapGetDepth()

Parameters
[in]cmap
Returns
depth, or 0 on error

Definition at line 742 of file colormap.c.

References PixColormap::depth.

Referenced by pixcmapConvertTo4(), and pixcmapConvertTo8().

◆ pixcmapGetDistanceToColor()

l_ok pixcmapGetDistanceToColor ( PIXCMAP cmap,
l_int32  index,
l_int32  rval,
l_int32  gval,
l_int32  bval,
l_int32 *  pdist 
)

pixcmapGetDistanceToColor()

Parameters
[in]cmap
[in]index
[in]rval,gval,bvaltarget color
[out]pdistthe distance from the cmap entry to target
Returns
0 if OK, 1 on error
Notes:
     (1) Returns the L2 distance (squared) between the color at index i
         and the target color.

Definition at line 1464 of file colormap.c.

References PixColormap::array, RGBA_Quad::blue, RGBA_Quad::green, pixcmapGetCount(), and RGBA_Quad::red.

◆ pixcmapGetFreeCount()

l_int32 pixcmapGetFreeCount ( PIXCMAP cmap)

pixcmapGetFreeCount()

Parameters
[in]cmap
Returns
free entries, or 0 on error

Definition at line 725 of file colormap.c.

References PixColormap::n, and PixColormap::nalloc.

Referenced by pixcmapAddBlackOrWhite().

◆ pixcmapGetIndex()

l_int32 pixcmapGetIndex ( PIXCMAP cmap,
l_int32  rval,
l_int32  gval,
l_int32  bval,
l_int32 *  pindex 
)

pixcmapGetIndex()

Parameters
[in]cmap
[in]rval,gval,bvalcolormap colors to search for; each number is in range [0, ... 255]
[out]pindexvalue of index found
Returns
0 if found, 1 if not found caller must check

Definition at line 1036 of file colormap.c.

References PixColormap::array, and pixcmapGetCount().

Referenced by pixcmapAddNearestColor(), pixcmapAddNewColor(), and pixcmapUsableColor().

◆ pixcmapGetMinDepth()

l_ok pixcmapGetMinDepth ( PIXCMAP cmap,
l_int32 *  pmindepth 
)

pixcmapGetMinDepth()

Parameters
[in]cmap
[out]pmindepthminimum depth to support the colormap
Returns
0 if OK, 1 on error
Notes:
     (1) On error, &mindepth is returned as 0.

Definition at line 765 of file colormap.c.

References pixcmapGetCount().

◆ pixcmapGetNearestGrayIndex()

l_ok pixcmapGetNearestGrayIndex ( PIXCMAP cmap,
l_int32  val,
l_int32 *  pindex 
)

pixcmapGetNearestGrayIndex()

Parameters
[in]cmap
[in]valgray value to search for; in range [0, ... 255]
[out]pindexthe index of the nearest color
Returns
0 if OK, 1 on error caller must check
Notes:
     (1) This should be used on gray colormaps.  It uses only the
         green value of the colormap.
     (2) Returns the index of the exact color if possible, otherwise the
         index of the color closest to the target color.

Definition at line 1411 of file colormap.c.

References PixColormap::array, RGBA_Quad::green, and pixcmapGetCount().

◆ pixcmapGetNearestIndex()

l_ok pixcmapGetNearestIndex ( PIXCMAP cmap,
l_int32  rval,
l_int32  gval,
l_int32  bval,
l_int32 *  pindex 
)

pixcmapGetNearestIndex()

Parameters
[in]cmap
[in]rval,gval,bvalcolormap colors to search for; each number is in range [0, ... 255]
[out]pindexthe index of the nearest color
Returns
0 if OK, 1 on error caller must check
Notes:
     (1) Returns the index of the exact color if possible, otherwise the
         index of the color closest to the target color.
     (2) Nearest color is that which is the least sum-of-squares distance
         from the target color.

Definition at line 1353 of file colormap.c.

References PixColormap::array, RGBA_Quad::blue, RGBA_Quad::green, pixcmapGetCount(), and RGBA_Quad::red.

Referenced by pixcmapAddNearestColor(), and pixcmapToOctcubeLUT().

◆ pixcmapGetRangeValues()

l_ok pixcmapGetRangeValues ( PIXCMAP cmap,
l_int32  select,
l_int32 *  pminval,
l_int32 *  pmaxval,
l_int32 *  pminindex,
l_int32 *  pmaxindex 
)

pixcmapGetRangeValues()

Parameters
[in]cmap
[in]selectL_SELECT_RED, L_SELECT_GREEN, L_SELECT_BLUE or L_SELECT_AVERAGE
[out]pminval[optional] minimum value of component
[out]pmaxval[optional] maximum value of component
[out]pminindex[optional] index of minimum value
[out]pmaxindex[optional] index of maximum value
Returns
0 if OK, 1 on error
Notes:
     (1) Returns, for selected components (or the average), the
         the extreme values (min and/or max) and their indices
         that are found in the cmap.

Definition at line 1520 of file colormap.c.

References L_SELECT_AVERAGE, L_SELECT_BLUE, L_SELECT_GREEN, L_SELECT_RED, pixcmapGetColor(), and pixcmapGetCount().

◆ pixcmapGetRankIntensity()

l_ok pixcmapGetRankIntensity ( PIXCMAP cmap,
l_float32  rankval,
l_int32 *  pindex 
)

pixcmapGetRankIntensity()

Parameters
[in]cmap
[in]rankval0.0 for darkest, 1.0 for lightest color
[out]pindexthe index into the colormap that corresponds to the rank intensity color
Returns
0 if OK, 1 on error

Definition at line 1302 of file colormap.c.

References L_SORT_INCREASING, numaAddNumber(), numaCreate(), numaDestroy(), numaGetIValue(), numaGetSortIndex(), pixcmapGetColor(), and pixcmapGetCount().

Referenced by pixcmapAddBlackOrWhite(), and pixcmapSetBlackAndWhite().

◆ pixcmapGetRGBA()

l_ok pixcmapGetRGBA ( PIXCMAP cmap,
l_int32  index,
l_int32 *  prval,
l_int32 *  pgval,
l_int32 *  pbval,
l_int32 *  paval 
)

pixcmapGetRGBA()

Parameters
[in]cmap
[in]index
[out]prval,pgval,pbval,pavaleach color value
Returns
0 if OK, 1 if not accessible caller should check

Definition at line 892 of file colormap.c.

References RGBA_Quad::alpha, PixColormap::array, RGBA_Quad::blue, RGBA_Quad::green, PixColormap::n, and RGBA_Quad::red.

Referenced by cmapEqual(), pixcmapGetRGBA32(), pixcmapSerializeToMemory(), and pixcmapToRGBTable().

◆ pixcmapGetRGBA32()

l_ok pixcmapGetRGBA32 ( PIXCMAP cmap,
l_int32  index,
l_uint32 *  pval32 
)

pixcmapGetRGBA32()

Parameters
[in]cmap
[in]index
[out]pval3232-bit rgba color value
Returns
0 if OK, 1 if not accessible caller should check

Definition at line 930 of file colormap.c.

References composeRGBAPixel(), and pixcmapGetRGBA().

◆ pixcmapGrayToColor()

PIXCMAP* pixcmapGrayToColor ( l_uint32  color)

pixcmapGrayToColor()

Parameters
[in]color
Returns
cmap, or NULL on error
Notes:
     (1) This creates a colormap that maps from gray to
         a specific color.  In the mapping, each component
         is faded to white, depending on the gray value.
     (2) In use, this is simply attached to a grayscale pix
         to give it the input color.

Definition at line 1678 of file colormap.c.

References extractRGBValues(), pixcmapAddColor(), and pixcmapCreate().

◆ pixcmapGrayToFalseColor()

PIXCMAP* pixcmapGrayToFalseColor ( l_float32  gamma)

pixcmapGrayToFalseColor()

Parameters
[in]gamma(factor) 0.0 or 1.0 for default; > 1.0 for brighter; 2.0 is quite nice
Returns
cmap, or NULL on error
Notes:
     (1) This creates a colormap that maps from gray to false colors.
         The colormap is modeled after the Matlap "jet" configuration.

Definition at line 1614 of file colormap.c.

References pixcmapAddColor(), and pixcmapCreate().

◆ pixcmapHasColor()

l_ok pixcmapHasColor ( PIXCMAP cmap,
l_int32 *  pcolor 
)

pixcmapHasColor()

Parameters
[in]cmap
[out]pcolorTRUE if cmap has color; FALSE otherwise
Returns
0 if OK, 1 on error

Definition at line 1075 of file colormap.c.

References pixcmapGetCount(), and pixcmapToArrays().

Referenced by pixcmapIsBlackAndWhite().

◆ pixcmapIsBlackAndWhite()

l_ok pixcmapIsBlackAndWhite ( PIXCMAP cmap,
l_int32 *  pblackwhite 
)

pixcmapIsBlackAndWhite()

Parameters
[in]cmap
[out]pblackwhiteTRUE if the cmap has only two colors: black (0,0,0) and white (255,255,255)
Returns
0 if OK, 1 on error

Definition at line 1220 of file colormap.c.

References PixColormap::array, pixcmapGetCount(), pixcmapHasColor(), and RGBA_Quad::red.

◆ pixcmapIsOpaque()

l_ok pixcmapIsOpaque ( PIXCMAP cmap,
l_int32 *  popaque 
)

pixcmapIsOpaque()

Parameters
[in]cmap
[out]popaqueTRUE if fully opaque: all entries are 255
Returns
0 if OK, 1 on error

Definition at line 1114 of file colormap.c.

References PixColormap::array, and pixcmapGetCount().

◆ pixcmapIsValid()

l_ok pixcmapIsValid ( const PIXCMAP cmap,
PIX pix,
l_int32 *  pvalid 
)

pixcmapIsValid()

Parameters
[in]cmap
[in]pixoptional; can be NULL
[out]pvalidreturn 1 if valid; 0 if not
Returns
0 if OK, 1 on error or if cmap is not valid
Notes:
     (1) If pix is input, this will verify that pixel values cannot
         overflow the colormap.  This is a relatively expensive operation
         that may need to check all the pixel values.
     (2) If pix is input, there must be at least one color in the
         colormap if it is to be valid with any pix, even if the
         pixels are all 0.

Definition at line 317 of file colormap.c.

References PixColormap::array, PixColormap::depth, PixColormap::n, and PixColormap::nalloc.

Referenced by pixcmapCopy(), and pixSetColormap().

◆ pixcmapNonOpaqueColorsInfo()

l_ok pixcmapNonOpaqueColorsInfo ( PIXCMAP cmap,
l_int32 *  pntrans,
l_int32 *  pmax_trans,
l_int32 *  pmin_opaque 
)

pixcmapNonOpaqueColorsInfo()

Parameters
[in]cmap
[out]pntrans[optional] number of transparent alpha entries; <= 256
[out]pmax_trans[optional] max index of transparent alpha
[out]pmin_opaque[optional] min index of opaque < 256
Returns
0 if OK, 1 on error
Notes:
     (1) This is used, for clarity, when writing the png tRNS palette.
         According to the spec, http://www.w3.org/TR/PNG/#11tRNS,
         if the tRNS palette is of size ntrans, the palette uses the first
         ntrans alpha entries in the cmap, and the remaining alpha values
         are assumed to be 255 (opaque), regardless of cmap alpha value.
     (2) Ordinarily, the non-opaque colors come first in the cmap, so
              min_opaque > max_trans
         and
              ntrans = max_trans + 1 = min_opaque.
         But this does not happen in general.  In trans-2bpp-palette.png,
         for example, only the third of four entries is not opaque, so
              ntrans = 1
              max_trans = 2 (index is 0-based)
              min_opaque = 0
         The tRNS palette must extend to the third entry to cover the
         color with transparency: use 3 as the fourth arg to png_set_tRNS().
     (3) If all entries are opaque, max_trans = -1.
         If all entries are transparent, min_opaque = size of cmap.

Definition at line 1173 of file colormap.c.

References PixColormap::array, and pixcmapGetCount().

◆ pixcmapRead()

PIXCMAP* pixcmapRead ( const char *  filename)

pixcmapRead()

Parameters
[in]filename
Returns
cmap, or NULL on error

Definition at line 1836 of file colormap.c.

References fopenReadStream(), and pixcmapReadStream().

◆ pixcmapReadMem()

PIXCMAP* pixcmapReadMem ( const l_uint8 *  data,
size_t  size 
)

pixcmapReadMem()

Parameters
[in]dataserialization of pixcmap; in ascii
[in]sizeof data in bytes; can use strlen to get it
Returns
cmap, or NULL on error

Definition at line 1905 of file colormap.c.

References fopenReadFromMemory(), and pixcmapReadStream().

◆ pixcmapReadStream()

PIXCMAP* pixcmapReadStream ( FILE *  fp)

pixcmapReadStream()

Parameters
[in]fpfile stream
Returns
cmap, or NULL on error

Definition at line 1863 of file colormap.c.

References pixcmapAddRGBA(), pixcmapCreate(), and pixcmapDestroy().

Referenced by pixcmapRead(), and pixcmapReadMem().

◆ pixcmapResetColor()

l_ok pixcmapResetColor ( PIXCMAP cmap,
l_int32  index,
l_int32  rval,
l_int32  gval,
l_int32  bval 
)

pixcmapResetColor()

Parameters
[in]cmap
[in]index
[in]rval,gval,bvalcolormap entry to be reset; each number is in range [0, ... 255]
Returns
0 if OK, 1 if not accessible caller should check
Notes:
     (1) This resets sets the color of an entry that has already
         been set and included in the count of colors.
     (2) The alpha component is 255 (opaque)

Definition at line 966 of file colormap.c.

References RGBA_Quad::alpha, PixColormap::array, RGBA_Quad::blue, RGBA_Quad::green, PixColormap::n, and RGBA_Quad::red.

Referenced by pixcmapColorToGray(), pixcmapContrastTRC(), pixcmapConvertHSVToRGB(), pixcmapConvertRGBToHSV(), pixcmapConvertRGBToYUV(), pixcmapConvertYUVToRGB(), pixcmapGammaTRC(), pixcmapSetBlackAndWhite(), pixcmapShiftByComponent(), and pixcmapShiftIntensity().

◆ pixcmapSerializeToMemory()

l_ok pixcmapSerializeToMemory ( PIXCMAP cmap,
l_int32  cpc,
l_int32 *  pncolors,
l_uint8 **  pdata 
)

pixcmapSerializeToMemory()

Parameters
[in]cmapcolormap
[in]cpccomponents/color: 3 for rgb, 4 for rgba
[out]pncolorsnumber of colors in table
[out]pdatabinary string, cpc bytes per color
Returns
0 if OK; 1 on error
Notes:
     (1) When serializing to store in a pdf, use cpc = 3.

Definition at line 2164 of file colormap.c.

References pixcmapGetCount(), and pixcmapGetRGBA().

◆ pixcmapSetAlpha()

l_ok pixcmapSetAlpha ( PIXCMAP cmap,
l_int32  index,
l_int32  aval 
)

pixcmapSetAlpha()

Parameters
[in]cmap
[in]index
[in]avalin range [0, ... 255]
Returns
0 if OK, 1 on error
Notes:
     (1) This modifies the transparency of one entry in a colormap.
         The alpha component by default is 255 (opaque).
         This is used when extracting the colormap from a PNG file
         without decoding the image.

Definition at line 1007 of file colormap.c.

References RGBA_Quad::alpha, PixColormap::array, and PixColormap::n.

◆ pixcmapSetBlackAndWhite()

l_ok pixcmapSetBlackAndWhite ( PIXCMAP cmap,
l_int32  setblack,
l_int32  setwhite 
)

pixcmapSetBlackAndWhite()

Parameters
[in]cmap
[in]setblack0 for no operation; 1 to set darkest color to black
[in]setwhite0 for no operation; 1 to set lightest color to white
Returns
0 if OK, 1 on error

Definition at line 678 of file colormap.c.

References pixcmapGetRankIntensity(), and pixcmapResetColor().

Referenced by pixThresholdGrayArb().

◆ pixcmapShiftByComponent()

l_ok pixcmapShiftByComponent ( PIXCMAP cmap,
l_uint32  srcval,
l_uint32  dstval 
)

pixcmapShiftByComponent()

Parameters
[in]cmapcolormap
[in]srcvalsource color: 0xrrggbb00
[in]dstvaltarget color: 0xrrggbb00
Returns
0 if OK; 1 on error
Notes:
     (1) This is an in-place transform
     (2) It implements pixelShiftByComponent() for each color.
         The mapping is specified by srcval and dstval.
     (3) If a component decreases, the component in the colormap
         decreases by the same ratio.  Likewise for increasing, except
         all ratios are taken with respect to the distance from 255.

Definition at line 2487 of file colormap.c.

References extractRGBValues(), pixcmapGetColor(), pixcmapGetCount(), pixcmapResetColor(), and pixelShiftByComponent().

◆ pixcmapShiftIntensity()

l_ok pixcmapShiftIntensity ( PIXCMAP cmap,
l_float32  fraction 
)

pixcmapShiftIntensity()

Parameters
[in]cmapcolormap
[in]fractionbetween -1.0 and +1.0
Returns
0 if OK; 1 on error
Notes:
     (1) This is an in-place transform
     (2) It does a proportional shift of the intensity for each color.
     (3) If fraction < 0.0, it moves all colors towards (0,0,0).
         This darkens the image.
         If fraction > 0.0, it moves all colors towards (255,255,255)
         This fades the image.
     (4) The equivalent transform can be accomplished with pixcmapGammaTRC(),
         but it is considerably more difficult (see numaGammaTRC()).

Definition at line 2437 of file colormap.c.

References pixcmapGetColor(), pixcmapGetCount(), and pixcmapResetColor().

◆ pixcmapToArrays()

l_ok pixcmapToArrays ( const PIXCMAP cmap,
l_int32 **  prmap,
l_int32 **  pgmap,
l_int32 **  pbmap,
l_int32 **  pamap 
)

pixcmapToArrays()

Parameters
[in]cmapcolormap
[out]prmaparray of red values
[out]pgmaparray of green values
[out]pbmaparray of blue values
[out]pamap[optional] array of alpha (transparency) values
Returns
0 if OK; 1 on error

Definition at line 2068 of file colormap.c.

References RGBA_Quad::alpha, PixColormap::array, RGBA_Quad::blue, RGBA_Quad::green, pixcmapGetCount(), and RGBA_Quad::red.

Referenced by pixcmapHasColor(), pixcmapToOctcubeLUT(), and pixcmapWriteStream().

◆ pixcmapToRGBTable()

l_ok pixcmapToRGBTable ( PIXCMAP cmap,
l_uint32 **  ptab,
l_int32 *  pncolors 
)

pixcmapToRGBTable()

Parameters
[in]cmapcolormap
[out]ptabtable of rgba values for the colormap
[out]pncolors[optional] size of table
Returns
0 if OK; 1 on error

Definition at line 2121 of file colormap.c.

References composeRGBAPixel(), pixcmapGetCount(), and pixcmapGetRGBA().

◆ pixcmapUsableColor()

l_ok pixcmapUsableColor ( PIXCMAP cmap,
l_int32  rval,
l_int32  gval,
l_int32  bval,
l_int32 *  pusable 
)

pixcmapUsableColor()

Parameters
[in]cmap
[in]rval,gval,bvalcolormap entry to be added; each number is in range [0, ... 255]
[out]pusable1 if usable; 0 if not
Returns
0 if OK, 1 on error
Notes:
     (1) This checks if the color already exists or if there is
         room to add it.  It makes no change in the colormap.

Definition at line 592 of file colormap.c.

References PixColormap::n, PixColormap::nalloc, and pixcmapGetIndex().

◆ pixcmapWrite()

l_ok pixcmapWrite ( const char *  filename,
const PIXCMAP cmap 
)

pixcmapWrite()

Parameters
[in]filename
[in]cmap
Returns
0 if OK, 1 on error

Definition at line 1933 of file colormap.c.

References fopenWriteStream(), and pixcmapWriteStream().

◆ pixcmapWriteMem()

l_ok pixcmapWriteMem ( l_uint8 **  pdata,
size_t *  psize,
const PIXCMAP cmap 
)

pixcmapWriteMem()

Parameters
[out]pdatadata of serialized pixcmap; ascii
[out]psizesize of returned data
[in]cmap
Returns
0 if OK, 1 on error
Notes:
     (1) Serializes a pixcmap in memory and puts the result in a buffer.

Definition at line 2011 of file colormap.c.

References fopenWriteWinTempfile(), l_binaryReadStream(), and pixcmapWriteStream().

◆ pixcmapWriteStream()

l_ok pixcmapWriteStream ( FILE *  fp,
const PIXCMAP cmap 
)

pixcmapWriteStream()

Parameters
[in]fpfile stream
[in]cmap
Returns
0 if OK, 1 on error

Definition at line 1965 of file colormap.c.

References PixColormap::depth, PixColormap::n, and pixcmapToArrays().

Referenced by pixcmapWrite(), and pixcmapWriteMem().