/// <param name="cacheColors"> /// Set this based on which types of methods will be used /// True: Cache colors /// False: Cache bytes /// </param> public BitmapCustomCachedColors(BitmapStreamInfo info, bool cacheColors) { _width = info.Width; _height = info.Height; _outOfBoundsColor = info.OutOfBoundsColor; _outOfBoundsBytes = info.OutOfBoundsBytes; if (cacheColors) { _colors = new BitmapCustomCachedBytes(info).GetColors(); _bytes = null; } else { _bytes = new BitmapCustomCachedBytes(info).GetColors_Byte(); _colors = null; } }
public BitmapCustomCachedBytes(BitmapStreamInfo info) { _info = info; }
/// <param name="convertToColors"> /// True: The entire byte array will be converted into Color structs up front (or byte[][]) /// Use this if you want color structs (expensive, but useful) /// This takes an up front cache hit, but repeated requests for colors are cheap /// Only useful if you plan to do many gets from this class /// /// False: The byte array is stored in the file's format, and any requests for colors are done on the fly /// This is more useful if you plan to get the colors in other formats (byte[][] or convolution), or if you just want an array of color[] /// Also good if only a subset of the pixels will be looked at /// Another use for this is if you want another thread to take the hit /// </param> /// <param name="outOfBoundsColor">If requests for pixels outside of width/height are made, this is the color that should be returned (probably either use transparent or black)</param> /// <param name="convertToColors_IsColor"> /// True: cached as colors /// False: cached as bytes -- there may be a case where this is more efficient than using BitmapCustomCachedBytes??? /// </param> public static IBitmapCustom ConvertToColorArray(BitmapSource bitmap, bool convertToColors, Color outOfBoundsColor, bool convertToColors_IsColor = true) { if (bitmap.Format != PixelFormats.Pbgra32 && bitmap.Format != PixelFormats.Bgr32) { // I've only coded against the above two formats, so put it in that bitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Pbgra32, null, 0); //http://stackoverflow.com/questions/1176910/finding-specific-pixel-colors-of-a-bitmapimage } // Get a byte array int stride = (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8; //http://msdn.microsoft.com/en-us/magazine/cc534995.aspx byte[] bytes = new byte[stride * bitmap.PixelHeight]; bitmap.CopyPixels(bytes, stride, 0); BitmapStreamInfo info = new BitmapStreamInfo(bytes, bitmap.PixelWidth, bitmap.PixelHeight, stride, bitmap.Format, outOfBoundsColor); // Exit Function if (convertToColors) { return new BitmapCustomCachedColors(info, convertToColors_IsColor); } else { return new BitmapCustomCachedBytes(info); } }