private Color GetAverageColor(FreeImageBitmap bmp) { if (bmp == null) { return(Color.Black); } if (bmp.Width < 2) { return(bmp.GetPixel(0, 0)); } var colors = new[] { bmp.GetPixel(0, 0), bmp.GetPixel(0, bmp.Height - 1), bmp.GetPixel(bmp.Width - 1, 0), bmp.GetPixel(bmp.Width - 1, bmp.Height - 1), bmp.GetPixel(bmp.Width / 2, 0), bmp.GetPixel(0, bmp.Height / 2), bmp.GetPixel(bmp.Width / 2, bmp.Height - 1), bmp.GetPixel(bmp.Width - 1, bmp.Height / 2), }; return(Color.FromArgb( (int)colors.Average(c => c.R), (int)colors.Average(c => c.G), (int)colors.Average(c => c.B) )); }
public void writeText(FreeImageBitmap bitmap, string p, int fontSize) { p = p.Replace("\n", "<br/>"); int width = Math.Min(bitmap.Width, 2048); int height = Math.Min(bitmap.Height, 256); ImageBox imageBox = (ImageBox)Gui.Instance.createWidgetT("ImageBox", "ImageBox", 0, 0, width, height, Align.Default, "Overlapped", "TempImageBox"); RocketWidget rocketWidget = null; try { imageBox.Visible = false; rocketWidget = new RocketWidget(imageBox, true); var cornerColor = Engine.Color.FromARGB(bitmap.GetPixel(0, 0).ToArgb()); rocketWidget.resized(); StringBuilder sb = new StringBuilder(); sb.AppendFormat(DocumentStart, cornerColor.ToHexString(), fontSize); sb.AppendFormat("<p>{0}</p>", p); sb.Append(DocumentEnd); var resourceLoader = new RocketAssemblyResourceLoader(this.GetType().Assembly); RocketInterface.Instance.FileInterface.addExtension(resourceLoader); var document = rocketWidget.Context.LoadDocumentFromMemory(sb.ToString()); if (document != null) { document.Show(ElementDocument.FocusFlags.NONE); } RocketInterface.Instance.FileInterface.removeExtension(resourceLoader); using (FreeImageBitmap copyrightBitmap = new FreeImageBitmap(imageBox.Width, imageBox.Height, PixelFormat.Format32bppArgb)) { rocketWidget.writeToGraphics(copyrightBitmap, new Rectangle(0, 0, copyrightBitmap.Width, copyrightBitmap.Height)); using (FreeImageBitmap background = bitmap.Copy(0, 0, Math.Min(copyrightBitmap.Width, bitmap.Width), Math.Min(copyrightBitmap.Height, bitmap.Height))) { background.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP); copyrightBitmap.Composite(false, null, background); bitmap.Paste(copyrightBitmap, 0, 0, int.MaxValue); } } } finally { //Using block does not work (wtf?), dispose manually if (rocketWidget != null) { rocketWidget.Dispose(); } Gui.Instance.destroyWidget(imageBox); } }
public bool Load(string filename) { //ファイルからの画像データの読み込み try { using (FreeImageBitmap img = new FreeImageBitmap(filename)) { Width = img.Width; Height = img.Height; data = new float[Height, Width, 4]; for (int j = 0; j < Height; j++) { for (int i = 0; i < Width; i++) { var col = img.GetPixel(i, j); data[j, i, 0] = (float)col.R / 256; data[j, i, 1] = (float)col.G / 256; data[j, i, 2] = (float)col.B / 256; data[j, i, 3] = (float)col.A / 256; } } } } catch (Exception e) { Console.WriteLine("Error on loading file : " + filename); Console.WriteLine(e.Data.ToString()); return(false); } //画像データをまだ読み込んでないなら失敗 if (data == null) { return(false); } //テクスチャをすでに確保しているなら解放 if (texture != 0) { GL.DeleteTexture(texture); } //テクスチャの確保、束縛、設定 texture = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texture); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Rgba, PixelType.Float, data); return(true); }
private unsafe byte getColor(int x, int y, FreeImageBitmap src, Channel channel) { var c = src.GetPixel(x, y); switch (channel) { case Channel.Alpha: return(c.A); case Channel.Red: return(c.R); case Channel.Green: return(c.G); case Channel.Blue: return(c.B); } //uint* pixel = (uint*)src.Scan0.ToPointer(); //pixel += src.Stride * y + x; //int bpp = 4; //if(src.PixelFormat == FreeImageAPI.PixelFormat.Format24bppRgb) //{ // bpp = 3; //} //byte* pixel = (byte*)src.Bits.ToPointer(); //pixel += (src.Pitch * (src.Height - y - 1) + x) * bpp; //switch (channel) //{ // case Channel.Alpha: // return pixel[3]; // case Channel.Red: // return pixel[2]; // case Channel.Green: // return pixel[1]; // case Channel.Blue: // return pixel[0]; //} throw new NotSupportedException(); //Won't get here }
static void Main(string[] args) { // string path = (@"E:\C\work\2019-07-00\贴图流程化测试\eeee.tga"); string path = args[0]; FreeImageBitmap ne = new FreeImageBitmap(path, FREE_IMAGE_FORMAT.FIF_TARGA); Size b = ne.Size; int image_h = b.Height; int image_w = b.Width; /// 这里抛出移除是因为会当做方块来循环, 应该是小 // FreeImageBitmap new_tem = new FreeImageBitmap(image_w, image_h); Bitmap new_tem = new Bitmap(image_w, image_h); Color tt = ne.GetPixel(0, 0); /// 优先获得高度 在高度上去处理 宽度 Console.WriteLine(image_w); Bitmap ne_type = (Bitmap)ne.ToBitmap(); ne.Dispose(); for (int i = 0; i < image_h; i++) { for (int j = 0; j < image_w; j++) { //Color t = ne.GetPixel(i, j); /// 优先获得高度 在高度上去处理 宽度 Color t = ne_type.GetPixel(j, i); Program pro = new Program(); Color srgbcolor = pro.linetosrgb(t); new_tem.SetPixel(j, i, srgbcolor); } } ///对图形处理完成 /// FreeImageBitmap yy = new FreeImageBitmap(new_tem); yy.Save(path); }
public unsafe void GetSetPixel() { Random rand = new Random(); FreeImageBitmap fib = new FreeImageBitmap(2, 1, PixelFormat.Format1bppIndexed); Palette palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(16, 1, PixelFormat.Format4bppIndexed); palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(256, 1, PixelFormat.Format8bppIndexed); palette = fib.Palette; for (int i = 0; i < palette.Length; i++) { palette[i] = (uint)rand.Next(int.MinValue, int.MaxValue); fib.SetPixel(i, 0, palette[i]); } for (int i = 0; i < palette.Length; i++) { Assert.AreEqual(fib.GetPixel(i, 0), palette[i].Color); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format16bppRgb555); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.That(Math.Abs(orgColor.B - newColor.B) <= 8); Assert.That(Math.Abs(orgColor.G - newColor.G) <= 8); Assert.That(Math.Abs(orgColor.R - newColor.R) <= 8); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format24bppRgb); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.AreEqual(orgColor.B, newColor.B); Assert.AreEqual(orgColor.G, newColor.G); Assert.AreEqual(orgColor.R, newColor.R); } fib.Dispose(); fib = new FreeImageBitmap(1000, 1, PixelFormat.Format32bppArgb); for (int i = 0; i < 1000; i++) { Color orgColor = Color.FromArgb(rand.Next(int.MinValue, int.MaxValue)); fib.SetPixel(i, 0, orgColor); Color newColor = fib.GetPixel(i, 0); Assert.AreEqual(orgColor.B, newColor.B); Assert.AreEqual(orgColor.G, newColor.G); Assert.AreEqual(orgColor.R, newColor.R); Assert.AreEqual(orgColor.A, newColor.A); } fib.Dispose(); }