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); }