Example #1
0
        // grayquant.c (1952, 1)
        // makeGrayQuantTableArb(na, outdepth, ptab, pcmap) as int
        // makeGrayQuantTableArb(NUMA *, l_int32, l_int32 **, PIXCMAP **) as l_ok
        ///  <summary>
        /// (1) The number of bins is the count of %na + 1.<para/>
        ///
        /// (2) The bin boundaries in na must be sorted in increasing order.<para/>
        ///
        /// (3) The table is an inverse colormap: it maps input gray level
        /// to colormap index (the bin number).<para/>
        ///
        /// (4) The colormap generated here has quantized values at the
        /// center of each bin.  If you want to use the average gray
        /// value of pixels within the bin, discard the colormap and
        /// compute it using makeGrayQuantColormapArb().<para/>
        ///
        /// (5) Returns an error if there are not enough levels in the
        /// output colormap for the number of bins.  The number
        /// of bins must not exceed 2^outdepth.
        ///  </summary>
        ///  <remarks>
        ///  </remarks>
        ///  <include file="..\CHM_Help\IncludeComments.xml" path="Comments/makeGrayQuantTableArb/*"/>
        ///  <param name="na">[in] - numa of bin boundaries</param>
        ///  <param name="outdepth">[in] - of colormap: 1, 2, 4 or 8</param>
        ///  <param name="ptab">[out] - table mapping input gray level to cmap index</param>
        ///  <param name="pcmap">[out] - colormap</param>
        ///   <returns>0 if OK, 1 on error</returns>
        public static int makeGrayQuantTableArb(
            Numa na,
            int outdepth,
            out List <int[]> ptab,
            out PixColormap pcmap)
        {
            if (na == null)
            {
                throw new ArgumentNullException("na cannot be Nothing");
            }

            IntPtr ptabPtr  = IntPtr.Zero;
            IntPtr pcmapPtr = IntPtr.Zero;
            int    _Result  = Natives.makeGrayQuantTableArb(na.Pointer, outdepth, out ptabPtr, out pcmapPtr);

            if (ptabPtr == null)
            {
                ptab = null;
            }
            else
            {
                ptab = null;
            };
            if (pcmapPtr == IntPtr.Zero)
            {
                pcmap = null;
            }
            else
            {
                pcmap = new PixColormap(pcmapPtr);
            };

            return(_Result);
        }
Example #2
0
        // pngio.c (805, 1)
        // fgetPngColormapInfo(fp, pcmap, ptransparency) as int
        // fgetPngColormapInfo(FILE *, PIXCMAP **, l_int32 *) as l_ok
        ///  <remarks>
        ///  </remarks>
        ///  <include file="..\CHM_Help\IncludeComments.xml" path="Comments/fgetPngColormapInfo/*"/>
        ///   <returns></returns>
        public static int fgetPngColormapInfo(
            FILE fp,
            PixColormap pcmap,
            object ptransparency)
        {
            if (fp == null)
            {
                throw new ArgumentNullException("fp cannot be Nothing");
            }

            if (pcmap == null)
            {
                throw new ArgumentNullException("pcmap cannot be Nothing");
            }

            if (ptransparency == null)
            {
                throw new ArgumentNullException("ptransparency cannot be Nothing");
            }

            IntPtr fpPtr = IntPtr.Zero; if (fp != null)
            {
                fpPtr = fp.Pointer;
            }
            IntPtr pcmapPtr = IntPtr.Zero;  if (pcmap != null)
            {
                pcmapPtr = pcmap.Pointer;
            }
            int _Result = Natives.fgetPngColormapInfo(fp.Pointer, pcmapPtr, ptransparency);

            return(_Result);
        }
Example #3
0
        // paintcmap.c (492, 1)
        // addColorizedGrayToCmap(cmap, type, rval, gval, bval, pna) as int
        // addColorizedGrayToCmap(PIXCMAP *, l_int32, l_int32, l_int32, l_int32, NUMA **) as l_ok
        ///  <summary>
        /// (1) If type == L_PAINT_LIGHT, it colorizes non-black pixels,
        /// preserving antialiasing.
        /// If type == L_PAINT_DARK, it colorizes non-white pixels,
        /// preserving antialiasing.<para/>
        ///
        /// (2) This increases the colormap size by the number of
        /// different gray (non-black or non-white) colors in the
        /// input colormap.  If there is not enough room in the colormap
        /// for this expansion, it returns 1 (treated as a warning)
        /// the caller should check the return value.<para/>
        ///
        /// (3) This can be used to determine if the new colors will fit in
        /// the cmap, using null for [and]na.  Returns 0 if they fit 2 if
        /// they don't fit.<para/>
        ///
        /// (4) The mapping table contains, for each gray color found, the
        /// index of the corresponding colorized pixel.  Non-gray
        /// pixels are assigned the invalid index 256.<para/>
        ///
        /// (5) See pixColorGrayCmap() for usage.
        ///  </summary>
        ///  <remarks>
        ///  </remarks>
        ///  <include file="..\CHM_Help\IncludeComments.xml" path="Comments/addColorizedGrayToCmap/*"/>
        ///  <param name="cmap">[in] - from 2 or 4 bpp pix</param>
        ///  <param name="type">[in] - L_PAINT_LIGHT, L_PAINT_DARK</param>
        ///  <param name="rval">[in] - target color</param>
        ///  <param name="gval">[in] - target color</param>
        ///  <param name="bval">[in] - target color</param>
        ///  <param name="pna">[out][optional] - table for mapping new cmap entries</param>
        ///   <returns>0 if OK 1 on error 2 if new colors will not fit in cmap.</returns>
        public static int addColorizedGrayToCmap(
            PixColormap cmap,
            int type,
            int rval,
            int gval,
            int bval,
            out Numa pna)
        {
            if (cmap == null)
            {
                throw new ArgumentNullException("cmap cannot be Nothing");
            }

            IntPtr pnaPtr  = IntPtr.Zero;
            int    _Result = Natives.addColorizedGrayToCmap(cmap.Pointer, type, rval, gval, bval, out pnaPtr);

            if (pnaPtr == IntPtr.Zero)
            {
                pna = null;
            }
            else
            {
                pna = new Numa(pnaPtr);
            };

            return(_Result);
        }
Example #4
0
        // colorcontent.c (1395, 1)
        // pixGetMostPopulatedColors(pixs, sigbits, factor, ncolors, parray, pcmap) as int
        // pixGetMostPopulatedColors(PIX *, l_int32, l_int32, l_int32, l_uint32 **, PIXCMAP **) as l_ok
        ///  <summary>
        /// (1) This finds the %ncolors most populated cubes in rgb colorspace,
        /// where the cube size depends on %sigbits as
        /// cube side = (256  is greater  is greater  sigbits)<para/>
        ///
        /// (2) The rgb color components are found at the center of the cube.<para/>
        ///
        /// (3) The output array of colors can be displayed using
        /// pixDisplayColorArray(array, ncolors, ...)
        ///  </summary>
        ///  <remarks>
        ///  </remarks>
        ///  <include file="..\CHM_Help\IncludeComments.xml" path="Comments/pixGetMostPopulatedColors/*"/>
        ///  <param name="pixs">[in] - 32 bpp rgb</param>
        ///  <param name="sigbits">[in] - 2-6, significant bits retained in the quantizer for each component of the input image</param>
        ///  <param name="factor">[in] - subsampling factor use 1 for no subsampling</param>
        ///  <param name="ncolors">[in] - the number of most populated colors to select</param>
        ///  <param name="parray">[out][optional] - array of colors, each as 0xrrggbb00</param>
        ///  <param name="pcmap">[out][optional] - colormap of the colors</param>
        ///   <returns>0 if OK, 1 on error</returns>
        public static int pixGetMostPopulatedColors(
            Pix pixs,
            int sigbits,
            int factor,
            int ncolors,
            out Byte[] parray,
            out PixColormap pcmap)
        {
            if (pixs == null)
            {
                throw new ArgumentNullException("pixs cannot be Nothing");
            }

            if ((new List <int> {
                32
            }).Contains((int)pixs.d) == false)
            {
                throw new ArgumentException("32 bpp rgb");
            }
            IntPtr parrayPtr = IntPtr.Zero;
            IntPtr pcmapPtr  = IntPtr.Zero;
            int    _Result   = Natives.pixGetMostPopulatedColors(pixs.Pointer, sigbits, factor, ncolors, out parrayPtr, out pcmapPtr);

            Byte[] parrayGen = new Byte[1];

            if (parrayPtr != IntPtr.Zero)
            {
                Marshal.Copy(parrayPtr, parrayGen, 0, parrayGen.Length);
            }

            parray = parrayGen;
            if (pcmapPtr == IntPtr.Zero)
            {
                pcmap = null;
            }
            else
            {
                pcmap = new PixColormap(pcmapPtr);
            };

            return(_Result);
        }
Example #5
0
        /// <summary>
        /// Remove speckles using Leptonica library.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="selStr"></param>
        /// <param name="selSize"></param>
        /// <returns></returns>
        public static Bitmap RemoveSpeckles(Bitmap image, string selStr, int selSize)
        {
            //var sourcePixFilename = @"processing\w91frag.jpg";
            using (Pix pix = PixConverter.ToPix(image))
            {
                PixColormap map = pix.Colormap;
                pix.Colormap = null;  // work around NPE during despeckle

                // remove speckles
                using (Pix result = pix.Despeckle(selStr, selSize))
                {
                    if (result == null)
                    {
                        return(image);
                    }

                    return(InvertColor(PixConverter.ToBitmap(result))); // somehow, invert is needed
                }
            }
        }
Example #6
0
        private static void CopyColormap(Bitmap img, Pix pix)
        {
            Color[]     entries     = img.Palette.Entries;
            PixColormap pixColormap = PixColormap.Create(pix.Depth);

            try
            {
                for (int index = 0; index < entries.Length; ++index)
                {
                    if (!pixColormap.AddColor(entries[index].ToPixColor()))
                    {
                        throw new InvalidOperationException(string.Format("Failed to add colormap entry {0}.", (object)index));
                    }
                }
                pix.Colormap = pixColormap;
            }
            catch (Exception)
            {
                pixColormap.Dispose();
                throw;
            }
        }
Example #7
0
        private void CopyColormap(Bitmap img, Pix pix)
        {
            var imgPalette        = img.Palette;
            var imgPaletteEntries = imgPalette.Entries;
            var pixColormap       = PixColormap.Create(pix.Depth);

            try
            {
                for (int i = 0; i < imgPaletteEntries.Length; i++)
                {
                    if (!pixColormap.AddColor(ToPixColor(imgPaletteEntries[i])))
                    {
                        throw new InvalidOperationException(String.Format("Failed to add colormap entry {0}.", i));
                    }
                }
                pix.Colormap = pixColormap;
            }
            catch (Exception)
            {
                pixColormap.Dispose();
                throw;
            }
        }
Example #8
0
        // grayquant.c (2564, 1)
        // pixGrayQuantFromCmap(pixs, cmap, mindepth) as Pix
        // pixGrayQuantFromCmap(PIX *, PIXCMAP *, l_int32) as PIX *
        ///  <summary>
        /// (1) In use, pixs is an 8 bpp grayscale image without a colormap.
        /// If there is an existing colormap, a warning is issued and
        /// a copy of the input pixs is returned.
        ///  </summary>
        ///  <remarks>
        ///  </remarks>
        ///  <include file="..\CHM_Help\IncludeComments.xml" path="Comments/pixGrayQuantFromCmap/*"/>
        ///  <param name="pixs">[in] - 8 bpp grayscale without cmap</param>
        ///  <param name="cmap">[in] - to quantize to of dest pix</param>
        ///  <param name="mindepth">[in] - minimum depth of pixd: can be 2, 4 or 8 bpp</param>
        ///   <returns>pixd 2, 4 or 8 bpp, colormapped, or NULL on error</returns>
        public static Pix pixGrayQuantFromCmap(
            Pix pixs,
            PixColormap cmap,
            int mindepth)
        {
            if (pixs == null)
            {
                throw new ArgumentNullException("pixs cannot be Nothing");
            }

            if (cmap == null)
            {
                throw new ArgumentNullException("cmap cannot be Nothing");
            }

            IntPtr _Result = Natives.pixGrayQuantFromCmap(pixs.Pointer, cmap.Pointer, mindepth);

            if (_Result == IntPtr.Zero)
            {
                return(null);
            }

            return(new Pix(_Result));
        }