public unsafe void PopulateCharColorData(CharInfo ci, IFontProvider font)
        {
            ColorF charRGB = ColorF.Init;

            for (int ty = 0; ty < LumaTiles.Height; ++ty)
            {
                for (int tx = 0; tx < LumaTiles.Width; ++tx)
                {
                    Size  tileSize;
                    Point tilePos;

                    GetTileInfo(font.CharSizeNoPadding, LumaTiles, tx, ty, out tilePos, out tileSize);
                    // process this single tile of this char.
                    // grab all pixels for this tile and calculate Y component for each
                    ColorF tileRGB = font.GetRegionColor(ci.srcIndex, tilePos, tileSize, LumaTiles, tx, ty);

                    charRGB = charRGB.Add(tileRGB);
                    LCCColorDenorm tileLAB = Colorspace.RGBToLCC(tileRGB);
                    ci.actualValues.DenormalizedValues[GetValueLIndex(tx, ty)] = (float)tileLAB.L;
                    ci.actualValues.NormalizedValues[GetValueLIndex(tx, ty)]   = (float)Colorspace.NormalizeL(ci.actualValues.DenormalizedValues[GetValueLIndex(tx, ty)]);
                }
            }

            if (UseChroma)
            {
                charRGB = charRGB.Div(Utils.Product(LumaTiles));
                LCCColorDenorm charLAB = Colorspace.RGBToLCC(charRGB);
                ci.actualValues.DenormalizedValues[GetValueC1Index()] = (float)charLAB.C1;
                ci.actualValues.DenormalizedValues[GetValueC2Index()] = (float)charLAB.C2;
                ci.actualValues.NormalizedValues[GetValueC1Index()]   = (float)Colorspace.NormalizeC1(ci.actualValues.DenormalizedValues[GetValueC1Index()]);
                ci.actualValues.NormalizedValues[GetValueC2Index()]   = (float)Colorspace.NormalizeC2(ci.actualValues.DenormalizedValues[GetValueC2Index()]);
            }
        }
Example #2
0
        public ColorF GetRegionColor(int ichar, Point topLeft, Size size, Size cellsPerChar, int cellOffsetX, int cellOffsetY)
        {
            Point oc = GetCharPosInChars(ichar);
            Point o  = GetCharOriginInPixels(ichar);

            o = Utils.Add(o, topLeft);
            int    tilePixelCount = 0;
            ColorF tileC          = ColorF.Init;

            for (int py = 0; py < size.Height; ++py)
            {
                for (int px = 0; px < size.Width; ++px)
                {
                    var c = ColorF.From(this.Bitmap.GetPixel(o.X + px, o.Y + py));

                    if (this.DitherProvider != null)
                    {
                        int absoluteCellX = cellsPerChar.Width * oc.X + cellOffsetX;
                        int absoluteCellY = cellsPerChar.Height * oc.Y + cellOffsetY;
                        c = this.DitherProvider.TransformColor(absoluteCellX, absoluteCellY, c);
                    }

                    tileC = tileC.Add(c);
                    tilePixelCount++;
                }
            }
            return(tileC.Div(tilePixelCount));
        }
        public unsafe void PopulateCharColorData(CharInfo ci, IFontProvider font)
        {
            ColorF charRGB = ColorF.Init;

            ColorF[] lumaRGB     = new ColorF[LumaComponentCount];
            int[]    pixelCounts = new int[LumaComponentCount];
            for (int i = 0; i < LumaComponentCount; ++i)
            {
                lumaRGB[i]     = ColorF.Init;
                pixelCounts[i] = 0;
            }

            for (int py = 0; py < font.CharSizeNoPadding.Height; ++py)
            {
                for (int px = 0; px < font.CharSizeNoPadding.Width; ++px)
                {
                    ColorF pc = font.GetPixel(ci.srcIndex, px, py);
                    charRGB = charRGB.Add(pc);
                    int lumaIdx = Tessellator.GetLumaTileIndexOfPixelPosInCell(px, py, font.CharSizeNoPadding);
                    lumaRGB[lumaIdx] = lumaRGB[lumaIdx].Add(pc);
                    pixelCounts[lumaIdx]++;
                }
            }

            for (int i = 0; i < LumaComponentCount; ++i)
            {
                var pc = pixelCounts[i];
                var lc = lumaRGB[i];
                if (pixelCounts[i] < 1)
                {
                    throw new Exception("!!!!!! Your fonts are just too small; i can't sample them properly.");
                }
                lc = lc.Div(pc);
                LCCColorDenorm lccc = Colorspace.RGBToLCC(lc);
                ci.actualValues.DenormalizedValues[i] = (float)lccc.L;
                ci.actualValues.NormalizedValues[i]   = (float)Colorspace.NormalizeL(ci.actualValues.DenormalizedValues[i]);
            }

            if (UseChroma)
            {
                charRGB = charRGB.Div(Utils.Product(font.CharSizeNoPadding));
                LCCColorDenorm charLAB = Colorspace.RGBToLCC(charRGB);
                ci.actualValues.DenormalizedValues[GetValueC1Index()] = (float)charLAB.C1;
                ci.actualValues.DenormalizedValues[GetValueC2Index()] = (float)charLAB.C2;
                ci.actualValues.NormalizedValues[GetValueC1Index()]   = (float)Colorspace.NormalizeC1(ci.actualValues.DenormalizedValues[GetValueC1Index()]);
                ci.actualValues.NormalizedValues[GetValueC2Index()]   = (float)Colorspace.NormalizeC2(ci.actualValues.DenormalizedValues[GetValueC2Index()]);
            }
        }
        /*
         * for analyzing an image, we don't want to sample every single pixel. but we
         * also don't want to only sample once per tile. since conceptually each
         * outer tile is supposed to cover multiple important regions, sample those
         * regions and average.
         */
        public int GetMapIndexOfRegion(Bitmap img, int x, int y, Size sz)
        {
            ColorF charRGB = ColorF.Init;

            ColorF[] lumaRGB     = new ColorF[LumaComponentCount];
            int[]    pixelCounts = new int[LumaComponentCount];
            for (int i = 0; i < LumaComponentCount; ++i)
            {
                lumaRGB[i]     = ColorF.Init;
                pixelCounts[i] = 0;
            }

            float[] vals = new float[DimensionCount];

            for (int py = 0; py < sz.Height; ++py)
            {
                for (int px = 0; px < sz.Width; ++px)
                {
                    ColorF pc = ColorF.From(img.GetPixel(x + px, y + py));
                    charRGB = charRGB.Add(pc);
                    int lumaIdx = Tessellator.GetLumaTileIndexOfPixelPosInCell(px, py, sz);
                    lumaRGB[lumaIdx] = lumaRGB[lumaIdx].Add(pc);
                    pixelCounts[lumaIdx]++;
                }
            }

            for (int i = 0; i < LumaComponentCount; ++i)
            {
                ColorF       lc   = lumaRGB[i].Div(pixelCounts[i]);
                LCCColorNorm lccc = RGBToNormalizedLCC(lc);
                vals[i] = (float)lccc.L;
            }

            if (UseChroma)
            {
                charRGB = charRGB.Div(Utils.Product(sz));
                LCCColorNorm charLAB = RGBToNormalizedLCC(charRGB);
                vals[GetValueC1Index()] = (float)charLAB.C1;
                vals[GetValueC2Index()] = (float)charLAB.C2;
            }

            int ID = NormalizedValueSetToMapID(vals);

            return(ID);
        }
Example #5
0
        public ColorF GetRegionColor(int ichar, Point topLeft, Size size, Size cellsPerChar, int cellOffsetX, int cellOffsetY)
        {
            //Point oc = GetCharPosInChars(ichar);
            //Point o = GetCharOriginInPixels(ichar);
            //o = Utils.Add(o, topLeft);
            int    tilePixelCount = 0;
            ColorF tileC          = ColorF.Init;

            for (int py = 0; py < size.Height; ++py)
            {
                for (int px = 0; px < size.Width; ++px)
                {
                    //var c = this.Bitmap.GetPixel(o.X + px, o.Y + py);
                    var c = GetPixel(ichar, topLeft.X + px, topLeft.Y + py);
                    tileC = tileC.Add(c);
                    tilePixelCount++;
                }
            }
            return(tileC.Div(tilePixelCount));
        }
        public int GetMapIndexOfRegion(Bitmap img, int x, int y, Size sz)
        {
            ColorF         rgb  = ColorF.Init;
            LCCColorDenorm lab  = LCCColorDenorm.Init;
            LCCColorNorm   norm = LCCColorNorm.Init;

            float[] vals    = new float[DimensionCount];
            ColorF  charRGB = ColorF.Init;

            for (int ty = LumaTiles.Height - 1; ty >= 0; --ty)
            {
                for (int tx = LumaTiles.Width - 1; tx >= 0; --tx)
                {
                    Point tilePos = GetTileOrigin(sz, LumaTiles, tx, ty);
                    // YES this just gets 1 pixel per char-sized area.
                    rgb = ColorF.From(img.GetPixel(x + tilePos.X, y + tilePos.Y));
                    lab = Colorspace.RGBToLCC(rgb);

                    norm = RGBToNormalizedLCC(rgb);
                    vals[GetValueLIndex(tx, ty)] = (float)norm.L;
                    charRGB = charRGB.Add(rgb);
                }
            }

            if (UseChroma)
            {
                int numTiles = Utils.Product(LumaTiles);
                charRGB = charRGB.Div(numTiles);
                norm    = RGBToNormalizedLCC(charRGB);
                vals[GetValueC1Index()] = (float)norm.C1;
                vals[GetValueC2Index()] = (float)norm.C2;
            }

            int ID = NormalizedValueSetToMapID(vals);

            return(ID);
        }