?? displayimage.cs
字號:
// Waveblend - complex dualtree based image fusion// (C) Copyright 2004 -- Sebastian Nowozin <nowozin@cs.tu-berlin.de>//// This file is part of Waveblend.//// Waveblend is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published// by the Free Software Foundation; version 2 of the License.//// Waveblend is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// The license is included with the distribution in the file 'LICENSE'.///* DisplayImage.cs * * Gateway classes between Gtk# GUI classes and Image* image classes. */using System;// GUI Image display class, showing a single picture and handling the// image loading.public class DisplayImage{ protected Gdk.Pixbuf pbuf; public Gdk.Pixbuf Pbuf { get { return (pbuf); } } internal DisplayImage () { } string filename; public DisplayImage (string filename) { this.filename = filename; pbuf = new Gdk.Pixbuf (filename); } public DisplayImage (string filename, Gdk.Pixbuf pbuf) { this.filename = filename; this.pbuf = pbuf; } // initialize the image from an image map, with 'setMax' being the // white-value to use public DisplayImage (ImageMap map, double setMax) { // generate a 24 bit rgb pixbuf without alphachannel pbuf = new Gdk.Pixbuf (Gdk.Colorspace.Rgb, false, 8, map.XDim, map.YDim); // set to black (highest 8 bits are ignored, as no alpha channel is used) pbuf.Fill (0x000000); // FIXME unsafe { byte *pbufPixels = (byte *) pbuf.Pixels; for (int y = 0 ; y < map.YDim ; ++y) { for (int x = 0 ; x < map.XDim ; ++x) { byte grayVal = (byte) ((map[x, y] * 255.0) / setMax); for (int n = 0 ; n < 3 ; ++n) pbufPixels[y * pbuf.Rowstride + x * pbuf.NChannels + n] = grayVal; } } } } public void Save (string filename) { string filetype = "png"; int li = filename.LastIndexOfAny (new char[] { '.' }); if (li < 0) { Console.WriteLine ("DisplayImage.Save: Filename \"{0}\" has no extension, defaulting to \".png\"", filename); } else { filetype = filename.Substring (li + 1); } pbuf.Savev (filename, filetype, null, null); } public int ReplaceSelective (DisplayImage source, int[,] map, int index) { int replaced = 0; // FIXME unsafe { byte *pbufPixels = (byte *) pbuf.Pixels; byte *sourcePixels = (byte *) source.pbuf.Pixels; for (int y = 0 ; y < map.GetLength (0) ; ++y) { for (int x = 0 ; x < map.GetLength (1) ; ++x) { if (map[y, x] != index) continue; replaced += 1; for (int n = 0 ; n < 3 ; ++n) pbufPixels[y * pbuf.Rowstride + x * pbuf.NChannels + n] = sourcePixels[y * pbuf.Rowstride + x * pbuf.NChannels + n]; } } } return (replaced); } public double ScaleWithin (int dim) { if (pbuf.Width <= dim && pbuf.Height <= dim) return (1.0); double xScale = ((double) dim / pbuf.Width); double yScale = ((double) dim / pbuf.Height); double smallestScale = xScale <= yScale ? xScale : yScale; pbuf = pbuf.ScaleSimple ((int) (pbuf.Width * smallestScale), (int) (pbuf.Height * smallestScale), Gdk.InterpType.Hyper); return (smallestScale); } public ImageMap ConvertToImageMap () { return (ConvertToImageMap (new CanonicalPixelConverter ())); } // Convert the current image to an ImageMap object using the per-pixel // converter 'pconv'. In case 'pconv' is null, the default 1/3 grayscale // operator is used. public ImageMap ConvertToImageMap (IPixelConverter pconv) { if (pconv == null) pconv = new CanonicalPixelConverter (); ImageMap res = new ImageMap (pbuf.Width, pbuf.Height); // FIXME: remove unsafe unsafe { byte *pbufPixels = (byte *) pbuf.Pixels; for (int y = 0 ; y < pbuf.Height ; ++y) { for (int x = 0 ; x < pbuf.Width ; ++x) { int byteOffset = y * pbuf.Rowstride + x * pbuf.NChannels; res[x, y] = pconv.Convert (pbufPixels[byteOffset + 0], pbufPixels[byteOffset + 1], pbufPixels[byteOffset + 2]); } } } return (res); } public class CanonicalPixelConverter : IPixelConverter { public double Convert (byte r, byte g, byte b) { // 1/3 and normalize to 0.0 - 1.0 range return ((r + g + b) / (255.0 * 3.0)); } } public interface IPixelConverter { double Convert (byte r, byte g, byte b); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -