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 ColorF GetPixel(int ichar, int px, int py) { Point o = GetCharOriginInPixels(ichar); var c = this.Bitmap.GetPixel(o.X + px, o.Y + py); c = SelectColor(ichar, c); return(ColorF.From(c)); }
public ColorF GetPixel(int ichar, int px, int py) { Point o = GetCharOriginInPixels(ichar); var c = ColorF.From(this.Bitmap.GetPixel(o.X + px, o.Y + py)); // TODO: dither return(c); }
/* * 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); }
public ColorF GetPixel(int ichar, int px, int py) { var e = charMap.AllCells[ichar]; px += e.blitSourcRect.Left; py += e.blitSourcRect.Top; if (px < 0 || py < 0) { return(ColorF.From(e.bgColor)); } if (px >= e.bmp.Width || py >= e.bmp.Height) { return(ColorF.From(e.bgColor)); } var c = e.bmp.GetPixel(px, py); //Point o = GetCharOriginInPixels(ichar); //var c = ColorF.From(this.Bitmap.GetPixel(o.X + px, o.Y + py)); return(ColorF.From(c)); }
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); c = SelectColor(ichar, c); tileC = tileC.Add(ColorF.From(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); }
static void Main2(string[] args) { #if !DEBUG try { #else { #endif Log.WriteLine("----------------------------------------"); //PartitionManager partitionManager = null;// = new PartitionManager(1, 1, discreteValues); int partitionsPerDimension = 1; int?partitionDepth = null; IPixelFormatProvider pixelFormat = null; IFontProvider fontProvider = null; string outputDir = null; List <string> processImages = new List <string>(); int coresToUtilize = System.Environment.ProcessorCount; List <System.Drawing.Color> testColors = new List <System.Drawing.Color>(); args.ProcessArg(new string[] { "-help", "-?", "-h" }, s => { var assem = System.Reflection.Assembly.GetExecutingAssembly();// typeof(PartitionManager).Assembly; var ns = assem.EntryPoint.DeclaringType.Namespace; using (Stream stream = assem.GetManifestResourceStream(ns + ".cmdhelp.txt")) { using (var reader = new StreamReader(stream)) { while (!reader.EndOfStream) { string ln = reader.ReadLine(); Log.WriteLine(ln); } } } }); bool didluts = false; args.ProcessArg("-createlut", s => { var colorSpace = Utils.ParseRequiredLCCColorSpaceArgs(args, true); string outfile = null; args.ProcessArg("-o", s2 => { outfile = s2; }); List <System.Drawing.Color> palette = new List <System.Drawing.Color>(); string paletteName = null; args.ProcessArg("-palette", s2 => { paletteName = s2; palette.AddRange(Utils.GetNamedPalette(s2)); }); int levels = 32; args.ProcessArg("-levels", s2 => { levels = int.Parse(s2); }); bool useChroma = false; args.ProcessArg("-lcc", s2 => { useChroma = true; }); bool neutral = false; args.ProcessArg("-neutral", s2 => { neutral = true; }); CreateLUT(paletteName, palette.ToArray(), outfile, colorSpace, levels, useChroma, neutral); didluts = true; return; }); if (didluts) { return; } args.ProcessArg("-listpalettes", s => { Log.WriteLine("Listing palettes:"); foreach (var p in typeof(Palettes).GetProperties()) { Log.WriteLine(" {0}", p.Name); } }); args.ProcessArg("-viewpalette", s => { Log.WriteLine("Listing palette entries for palette:"); Log.WriteLine("{0}", s); var p = Utils.GetNamedPalette(s); Log.WriteLine("{0} entries", p.Length); for (int i = 0; i < p.Length; ++i) { var c = p[i]; Log.WriteLine("{0:000}: {1} {2}", i, ColorTranslator.ToHtml(c), ColorMapper.GetNearestName(c)); } }); args.ProcessArg("-argsfile", s => { Log.WriteLine("Reading args from file: {0}" + s); var lines = System.IO.File.ReadAllLines(s) .Select(l => l.Split('#')[0]) // remove comments .Where(l => !string.IsNullOrWhiteSpace(l)); // remove empty lines args = lines.Concat(args).ToArray(); }); args.ProcessArg("-outdir", o => { outputDir = o; }); MapSource mapSource = MapSource.Create; args.ProcessArg("-testpalette", s => { testColors.AddRange(Utils.GetNamedPalette(s)); }); args.ProcessArg("-testcolor", s => { testColors.Add(System.Drawing.ColorTranslator.FromHtml(s)); }); args.ProcessArg("-partitions", s => { if (s.Contains('x')) { partitionsPerDimension = int.Parse(s.Split('x')[0]); partitionDepth = int.Parse(s.Split('x')[1]); } else { partitionsPerDimension = int.Parse(s); } }); args.ProcessArg("-processImagesInDir", o => { var files = System.IO.Directory.EnumerateFiles(o, "*", System.IO.SearchOption.TopDirectoryOnly); foreach (var file in files) { processImages.Add(file); } }); args.ProcessArg("-processImage", o => { processImages.Add(o); }); args.ProcessArg("-cores", o => { int a = int.Parse(o); if (a < 1) { a = System.Environment.ProcessorCount - a; } coresToUtilize = a; }); FontFamilyFontProvider fontFamilyProvider = null; args.ProcessArg("-fonttype", s => { switch (s.ToLowerInvariant()) { case "mono": fontProvider = MonoPaletteFontProvider.ProcessArgs(args); break; case "normal": fontProvider = FontProvider.ProcessArgs(args); break; case "colorkey": fontProvider = ColorKeyFontProvider.ProcessArgs(args); break; case "fontfamily": fontProvider = fontFamilyProvider = FontFamilyFontProvider.ProcessArgs(args); break; default: throw new Exception("Unknown font type: " + s); } }); args.ProcessArg("-pf", s => { switch (s.ToLowerInvariant()) { case "square": pixelFormat = SquareLCCPixelFormat.ProcessArgs(args);// HSLPixelFormat.ProcessArgs(args); break; default: case "fivetile": pixelFormat = FiveTilePixelFormat.ProcessArgs(args, fontProvider); break; } }); if (pixelFormat == null) { Log.WriteLine("Pixel format not specified."); return; //pixelFormat = NaiveYUVPixelFormat.ProcessArgs(args); } // emoji12-C64_YUV-2v5x5+2 if (fontProvider == null) { Log.WriteLine("Font information not specified."); return; } if (pixelFormat == null) { Log.WriteLine("Pixel format not specified."); return; } args.ProcessArg("-calcn", s => { ulong maxMapKeys = ulong.Parse(s); //partitionManager.Init(); //ulong partitionCount = (ulong)partitionManager.PartitionCount; //Log.WriteLine("Partition count: {0:N0}", partitionCount); // so the thing about partition count. You can't just divide by partition count, // because in deeper levels most partitions are simply unused / empty. // a decent conservative approximation is to take the first N levels //partitionCount = (ulong)Math.Pow(partitionManager.PartitionsPerDimension, 2.5);// n = 2.5 //Log.WriteLine("Adjusted partition count: {0:N0}", partitionCount); Log.WriteLine("Charset count: {0:N0}", fontProvider.CharCount); Log.WriteLine("Cores to utilize: {0:N0}", coresToUtilize); Log.WriteLine("Luma + chroma components: {0:N0}", pixelFormat.DimensionCount); ulong NbasedOnMapSize = (ulong)Math.Floor(Math.Pow(maxMapKeys, 1.0 / pixelFormat.DimensionCount)); Log.WriteLine("======================"); Log.WriteLine("== THEREFORE, use N={0:N0}", NbasedOnMapSize); Log.WriteLine("======================"); System.Environment.Exit(0); }); if (outputDir == null) { Log.WriteLine("No output directory was specified."); return; } outputDir = System.IO.Path.GetFullPath(outputDir); Log.WriteLine("Output directory: {0}", outputDir); if (!System.IO.Directory.Exists(outputDir)) { System.IO.Directory.CreateDirectory(outputDir); } args.ProcessArg("-loadmap", _ => { mapSource = MapSource.Load; // if you're loading, then we want to process the args from that directory. //outputDir = System.IO.Path.GetDirectoryName(s); string argspath = System.IO.Path.Combine(outputDir, "args.txt"); var lines = System.IO.File.ReadAllLines(argspath) .Select(l => l.Split('#')[0]) // remove comments .Where(l => !string.IsNullOrWhiteSpace(l)); // remove empty lines args = lines.Concat(args).ToArray(); }); string partitionConfigTag = string.Format("p{0}x{1}", partitionsPerDimension, partitionDepth.HasValue ? partitionDepth.ToString() : "N"); string configTag = string.Format("{0}_{1}_{2}", fontProvider.DisplayName, pixelFormat.PixelFormatString, partitionConfigTag); outputDir = System.IO.Path.Combine(outputDir, configTag); Log.WriteLine("Ensuring directory exists: {0}", outputDir); System.IO.Directory.CreateDirectory(outputDir); string logPath = System.IO.Path.Combine(outputDir, "log.txt"); Log.SetLogFile(logPath); string infopath = System.IO.Path.Combine(outputDir, "args.txt"); string mapFullPath = System.IO.Path.Combine(outputDir, string.Format("mapfull_{0}.png", configTag)); string mapRefPath = System.IO.Path.Combine(outputDir, string.Format("mapref_{0}.png", configTag)); string mapFontPath = System.IO.Path.Combine(outputDir, string.Format("mapfont_{0}.png", configTag)); args.ProcessArg("-loadOrCreateMap", _ => { if (System.IO.File.Exists(mapRefPath) && System.IO.File.Exists(mapFontPath)) { Log.WriteLine("-loadOrCreateMap: Loading existing map."); mapSource = MapSource.Load; } else { Log.WriteLine("-loadOrCreateMap: Looks like we have to create the map."); mapSource = MapSource.Create; } }); if (mapSource == MapSource.Create) { using (var infoFile = new StreamWriter(infopath)) { foreach (var arg in args) { infoFile.WriteLine(arg); } } } HybridMap2 map = null; switch (mapSource) { case MapSource.Create: Log.EnterTask("--- MAP GENERATION"); map = new HybridMap2(fontProvider, pixelFormat, mapFullPath, mapRefPath, mapFontPath, coresToUtilize, partitionsPerDimension, partitionDepth); Log.EndTask(); break; case MapSource.Load: Log.EnterTask("--- MAP LOAD"); map = HybridMap2.LoadFromDisk(outputDir, fontProvider, pixelFormat); Log.EndTask(); break; } if (fontFamilyProvider != null) { //string fontImgPath = System.IO.Path.Combine(outputDir, "font.png"); //fontFamilyProvider.SaveFontImage(fontImgPath); } Log.EnterTask("processing images"); foreach (var c in testColors) { map.TestColor(outputDir, ColorF.From(c)); } using (var refMapImage = new Bitmap(mapRefPath)) using (var refFontImage = new Bitmap(mapFontPath)) { foreach (var file in processImages) { Log.WriteLine("Processing {0}", file); string destFile = string.Format("test-{0}.png", System.IO.Path.GetFileNameWithoutExtension(file)); string destfullp = System.IO.Path.Combine(outputDir, destFile); using (var testImg = new Bitmap(file)) { var rv = map.ProcessImageUsingRef(refMapImage, refFontImage, testImg, destfullp); if (fontFamilyProvider != null) { string str = fontFamilyProvider.ConvertToText(rv); string txtpath = System.IO.Path.Combine(outputDir, string.Format("test-{0}.txt", System.IO.Path.GetFileNameWithoutExtension(file))); System.IO.File.WriteAllText(txtpath, str); } } } } Log.EndTask(); #if !DEBUG } catch (Exception e) { Log.WriteLine("Exception occurred:\r\n{0}", e); } #else }
// assumes 24 bits per pixel RGB public static unsafe ColorF GetPixel(this BitmapData data, long x, long y) { byte *p = data.GetRGBPointer(x, y); return(ColorF.From(Color.FromArgb(p[2], p[1], p[0]))); }