/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2013 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program 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, either version 1 of the License, or
* (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization;
namespace GreenshotPlugin.Core {
///
/// The BitmapBuffer is exactly what it says, it buffers a Bitmap.
/// And it is possible to Draw on the Bitmap with direct memory access for better performance
///
public unsafe class BitmapBuffer : IDisposable {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(BitmapBuffer));
private bool clone;
private Bitmap bitmap;
// Used for indexed images
private Color[] colorEntries;
public static Color BackgroundBlendColor {
get;
set;
}
static BitmapBuffer() {
BackgroundBlendColor = Color.White;
}
///
/// Get the bitmap, you will always need to dispose the returned bitmap!!
/// Only works, and makes sense, if cloned and not locked!
///
public Bitmap Bitmap {
get {
if (bitsLocked) {
throw new NotSupportedException("Can't get a locked bitmap!");
}
if (!clone) {
throw new NotSupportedException("Can't return a not cloned bitmap!");
} else {
// Make sure the bitmap isn't disposed when this object is closed
clone = false;
return bitmap;
}
}
}
public Bitmap UnlockAndReturnBitmap() {
Unlock();
return Bitmap;
}
public PixelFormat PixelFormat {
get {
return bitmap.PixelFormat;
}
}
private BitmapData bmData;
private Rectangle rect;
private byte* pointer;
private int* intPointer;
private int stride; /* bytes per pixel row */
private int aIndex = -1;
private int rIndex = -1;
private int gIndex = -1;
private int bIndex = -1;
private int bytesPerPixel;
private bool bitsLocked = false;
public Size Size {
get {return rect.Size;}
}
public int Width {
get {return rect.Width;}
}
public int Height {
get {return rect.Height;}
}
///
/// Create a BitmapBuffer from a Bitmap
///
/// Bitmap
public BitmapBuffer(Image sourceBmp) : this(sourceBmp, Rectangle.Empty) {
}
///
/// Create a BitmapBuffer from a Bitmap a flag if we need a clone
///
/// Bitmap
/// bool specifying if the bitmap needs to be cloned
public BitmapBuffer(Image sourceBmp, bool clone) : this(sourceBmp, Rectangle.Empty, clone) {
}
///
/// Create a BitmapBuffer from a Bitmap and a Rectangle specifying what part from the Bitmap to take.
///
/// Bitmap
/// Rectangle
public BitmapBuffer(Image sourceBmp, Rectangle applyRect) : this(sourceBmp, applyRect, true) {
}
///
/// Create a BitmapBuffer from a Bitmap, a Rectangle specifying what part from the Bitmap to take and a flag if we need a clone
///
/// Image, will be cloned if it's not a bitmap!!!
/// Rectangle
/// bool specifying if the bitmap needs to be cloned
public BitmapBuffer(Image sourceImage, Rectangle applyRect, bool clone) {
this.clone = clone;
Rectangle sourceRect = new Rectangle(applyRect.X, applyRect.Y, applyRect.Width, applyRect.Height);
Rectangle bitmapRect = new Rectangle(0,0, sourceImage.Width, sourceImage.Height);
if (sourceRect.IsEmpty) {
sourceRect = bitmapRect;
} else {
sourceRect.Intersect(bitmapRect);
}
// Does the rect have any pixels?
if (sourceRect.Height <= 0 || sourceRect.Width <= 0) {
return;
}
if (!(sourceImage is Bitmap) && !clone) {
LOG.Warn("Chancing clone to true, as the image is not a bitmap");
clone = true;
}
if (clone) {
this.bitmap = ImageHelper.CloneArea(sourceImage, sourceRect, PixelFormat.DontCare);
// Set "this" rect to location 0,0
// as the Cloned Bitmap is only the part we want to work with
this.rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
} else if (!ImageHelper.SupportsPixelFormat(sourceImage) && PixelFormat.Format8bppIndexed != sourceImage.PixelFormat) {
throw new ArgumentException("Unsupported pixel format: " + sourceImage.PixelFormat + " set clone to true!");
} else {
this.bitmap = sourceImage as Bitmap;
this.rect = sourceRect;
}
if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed) {
colorEntries = bitmap.Palette.Entries;
}
}
/**
* Destructor
*/
~BitmapBuffer() {
Dispose(false);
}
/**
* The public accessible Dispose
* Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice
*/
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
// The bulk of the clean-up code is implemented in Dispose(bool)
/**
* This Dispose is called from the Dispose and the Destructor.
* When disposing==true all non-managed resources should be freed too!
*/
protected virtual void Dispose(bool disposing) {
Unlock();
if (disposing) {
if (bitmap != null && clone) {
bitmap.Dispose();
}
}
bitmap = null;
bmData = null;
pointer = null;
}
/**
* Lock the bitmap so we have direct access to the memory
*/
public void Lock() {
if(rect.Width > 0 && rect.Height > 0 && !bitsLocked) {
bmData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
bitsLocked = true;
IntPtr Scan0 = bmData.Scan0;
pointer = (byte*)(void*)Scan0;
intPointer = (int*)(void*)Scan0;
PrepareForPixelFormat();
stride = bmData.Stride;
}
}
///
/// Unlock the System Memory
///
public void Unlock() {
if (bitsLocked) {
bitmap.UnlockBits(bmData);
bitsLocked = false;
}
}
///
/// Draw the stored bitmap to the destionation bitmap at the supplied point
///
///
///
public void DrawTo(Graphics graphics, Point destination) {
DrawTo(graphics, null, destination);
}
///
/// Draw the stored Bitmap on the Destination bitmap with the specified rectangle
/// Be aware that the stored bitmap will be resized to the specified rectangle!!
///
///
///
public void DrawTo(Graphics graphics, Rectangle destinationRect) {
DrawTo(graphics, destinationRect, null);
}
///
/// private helper to draw the bitmap
///
///
///
///
private void DrawTo(Graphics graphics, Rectangle? destinationRect, Point? destination) {
if (destinationRect.HasValue) {
// Does the rect have any pixels?
if (destinationRect.Value.Height <= 0 || destinationRect.Value.Width <= 0) {
return;
}
}
// Make sure this.bitmap is unlocked, if it was locked
bool isLocked = bitsLocked;
if (isLocked) {
Unlock();
}
if (destinationRect.HasValue) {
graphics.DrawImage(this.bitmap, destinationRect.Value);
} else if (destination.HasValue) {
graphics.DrawImageUnscaled(this.bitmap, destination.Value);
}
// If it was locked, lock it again
if (isLocked) {
Lock();
}
}
///
/// Use only when 32-bit bitmap!
///
/// x
/// y
/// argb value
public void SetARGB(int x, int y, int argb) {
int offset = (y * (stride>>2)) + x;
intPointer[offset] = argb;
}
///
/// Retrieve the color at location x,y
/// Before the first time this is called the Lock() should be called once!
///
/// X coordinate
/// Y Coordinate
/// Color
public Color GetColorAt(int x, int y) {
if(x>=0 && y>=0 && x
/// Set the color at location x,y
/// Before the first time this is called the Lock() should be called once!
///
///
///
///
public void SetColorAt(int x, int y, Color color) {
if(x>=0 && y>=0 && x
/// Retrieve the color at location x,y to a byte[]
/// Before the first time this is called the Lock() should be called once!
///
///
///
///
public void GetUncheckedColorIn(int x, int y, byte[] color) {
int offset = x * bytesPerPixel + y * stride;
color[0] = (aIndex == -1) ? (byte)255 : (byte)pointer[aIndex + offset];
color[1] = pointer[rIndex + offset];
color[2] = pointer[gIndex + offset];
color[3] = pointer[bIndex + offset];
}
///
/// Retrieve the color at location x,y to a byte[]
/// Before the first time this is called the Lock() should be called once!
///
///
///
///
public void GetColorAt(int x, int y, byte[] color) {
if (x >= 0 && y >= 0 && x < rect.Width && y < rect.Height) {
int offset = x * bytesPerPixel + y * stride;
color[0] = (aIndex == -1) ? (byte)255 : (byte)pointer[aIndex + offset];
color[1] = pointer[rIndex + offset];
color[2] = pointer[gIndex + offset];
color[3] = pointer[bIndex + offset];
} else {
color[0] = 0;
color[1] = 0;
color[2] = 0;
color[3] = 0;
}
}
///
/// Set the color at location x,y as an array
/// Before the first time this is called the Lock() should be called once!
///
///
///
///
public void SetColorAt(int x, int y, byte[] colors) {
if(x>=0 && y>=0 && x
/// Set some internal values for accessing the bitmap according to the PixelFormat
///
private void PrepareForPixelFormat() {
// aIndex is only set if the pixel format supports "A".
aIndex = -1;
switch(bitmap.PixelFormat) {
case PixelFormat.Format32bppPArgb:
case PixelFormat.Format32bppArgb:
bIndex = 0;
gIndex = 1;
rIndex = 2;
aIndex = 3;
bytesPerPixel = 4;
break;
case PixelFormat.Format32bppRgb:
bIndex = 0;
gIndex = 1;
rIndex = 2;
bytesPerPixel = 4;
break;
case PixelFormat.Format24bppRgb:
bIndex = 0;
gIndex = 1;
rIndex = 2;
bytesPerPixel = 3;
break;
case PixelFormat.Format8bppIndexed:
bytesPerPixel = 1;
break;
default:
throw new FormatException("Bitmap.Pixelformat."+bitmap.PixelFormat+" is currently not supported. Supported: Format32bpp(A)Rgb, Format24bppRgb");
}
}
}
}