This question is related to: How to convert Bitmap to byte[,,] faster?
I have byte[] which has:
[r0, g0, b0, r1, g1, b1 ... ]
(r0 is the r-value of zeroth pixel and so on)
How can I copy this quickly into byte[,,]?
Or maybe I can get byte[,,] ditectly from BitmapData?
Based on Martinho's answer but perhaps a bit faster(don't have time for benchmarking now):
struct BitmapDataAccessor
{
private readonly byte[] data;
private readonly int[] rowStarts;
public readonly int Height;
public readonly int Width;
public BitmapDataAccessor(byte[] data, int width, int height)
{
this.data = data;
this.Height = height;
this.Width = width;
rowStarts = new int[height];
for(int y=0;y<height;y++)
rowStarts[y]=y*width;
}
public byte this[int x, int y, int color] // Maybe use an enum with Red = 0, Green = 1, and Blue = 2 members?
{
get { return data[(rowStarts[y] + x) *3 + color]; }
set { data[(rowStarts[y] + x) *3 + color] = value; }
}
public byte[] Data
{
get { return data; }
}
}
Ok, let's say you've got your data in a one-dimensional byte array. Do you really need to push it over into a three-dimensional array? If all you want is an easier way to access the pixel data, why don't you simply write such a simple interface to that array? Something along these lines:
class BitmapDataAccessor
{
private readonly byte[] data;
private readonly int rows;
private readonly int columns;
public BitmapDataAccessor(byte[] data, int rows, int columns)
{
this.data = data;
this.rows = rows;
this.columns = columns;
}
public byte this[int row, int column, int color] // Maybe use an enum with Red = 0, Green = 1, and Blue = 2 members?
{
get { return data[(row * columns + column) * 3 + color]; }
set { data[(row * columns + column) * 3 + color] = value; }
}
public byte[] Data
{
get { return data; }
}
}
How about using something like this for ease of accessing the discrete bytes:
class ByteIndexer
{
private readonly byte[] _bits;
private readonly int _width;
public ByteIndexer(byte[] bits, int width)
{
_bits = bits;
_width = width;
}
public byte this[int x, int y, int c]
{ get { return _bits[(((_width * y) + x) * 3) + c]; } }
}
You could even make it easier, overload this[] with:
public Color this[int x, int y]
{ get { return Color.FromArgb(this[x,y,0], this[x,y,1], this[x,y,2]); }
You could also consider using the built-in Bitmap in System.Drawing. Here's an example with a 4x3 image.
var image = new byte[] {255,255,255,0,0,0,255,255,255,0,0,0,
255,255,255,0,0,0,255,127,255,0,0,0,
0,0,0,255,255,255,0,0,0,255,255,255};
Bitmap bmp = new Bitmap(4, 3, PixelFormat.Format24bppRgb);
BitmapData bmpData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.WriteOnly, bmp.PixelFormat);
Marshal.Copy(image, 0, bmpData.Scan0, image.Length);
bmp.UnlockBits(bmpData);
var testPixel = bmp.GetPixel(2, 1);
testPixel will be a System.Drawing.Color set to {Color [A=255, R=255, G=127, B=255]}
Related
I have this simple class to plot points on an image. It is limited to 24bbp. but at certain widths the image breaks, colors are distorted and the image moves towards the right the lower down in the image it gets. its only correct when the width is a multiple of 4 and I cant figure out why. can someone please point out my mistake? Thanks guys.
Example of problem
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using static System.Math;
public class TImage
{
private readonly Bitmap source = null;
private IntPtr Iptr = IntPtr.Zero;
private BitmapData bitmapData = null;
private bool locked = false;
private readonly int PixelCount;
private byte[] pixels;
public int Width { get; private set; }
public int Height { get; private set; }
public TImage(int width, int height)
{
source = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Width = width; Height = height;
PixelCount = Width * Height;
}
public TImage(Bitmap image)
{
if (image.PixelFormat != PixelFormat.Format24bppRgb) throw new FormatException("Only 24bppRgb can be used");
source = image;
Width = source.Width; Height = source.Height;
PixelCount = Width * Height;
}
private void Lock()
{
if (!locked)
{
Rectangle rect = new Rectangle(0, 0, Width, Height);
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// create byte array to copy pixel values
pixels = new byte[PixelCount * 3];
Iptr = bitmapData.Scan0;
// Copy data from pointer to array
Marshal.Copy(Iptr, pixels, 0, pixels.Length);
locked = true;
}
}
private void Unlock()
{
if (locked)
{
Marshal.Copy(pixels, 0, Iptr, pixels.Length);
source.UnlockBits(bitmapData);
locked = false;
}
}
public Color GetPixel(int x, int y)
{
if (!locked) Lock();
// Get start index of the pixel
int i = (y * Width + x) * 3;
if (i+2 > pixels.Length || i <0) throw new IndexOutOfRangeException();
byte b = pixels[i];
byte g = pixels[i + 1];
byte r = pixels[i + 2];
return Color.FromArgb(r, g, b);
}
public void SetPixel(int x, int y, Color color)
{
if (!locked) Lock();
int i = ((y * Width + x) * 3);
if (i + 2 < pixels.Length && i >= 0)
{
pixels[i] = color.B;
pixels[i + 1] = color.G;
pixels[i + 2] = color.R;
}
}
public void SetPixel(double x, double y, Color color)
{
SetPixel((int)Round(x), (int)Round(y), color);
}
public static implicit operator Bitmap(TImage img)
{
img.Unlock();
return img.source;
}
}
I have a Xaramin.Android project and I am trying to create a Custom TileProvider to use offline maps from mbtiles sqlite database and found an example that does what i want to do here https://github.com/antoniocarlon/MapUtils however this example is done in java so i started creating my own based on the above code but in Xamarin.Android in c#.
So i have created a Class MBTilesProvider that inherits from Android.Gms.Maps.Model.TileProvider however the : base(width, height) shows an error that 'TileProvider' does not contain a constructor that takes 2 arguments. I know that error is pretty self explanatory but i can't seem to find any documentation around what the constructor arguments are for TileProvider ? if i remove the :base from my class i get the error message that 'TileProvider' does not contain a constructor that contains 0 arguments.
public class MBTilesProvider : TileProvider
{
private int _width;
private int _height;
public MBTilesProvider(int width, int height) : base(width, height)
{
this._width = width;
this._height = height;
}
public Tile GetTile(int x, int y, int zoom)
{
y = tms2gmaps(y, zoom);
byte[] bitmap = RetreiveImage(x, y, zoom);
Tile tile = new Tile(_width, _height, bitmap);
return tile;
}
private byte[] RetreiveImage(int x, int y, int zoom)
{
byte[] image = null;
if(zoom > 0)
{
image = ReadImageFromDB(x, y, zoom);
if(image == null)
{
image = Crop(RetreiveImage(x / 2, y / 2, zoom - 1), x, y);
}
return image;
} else
{
return image;
}
}
private byte[] ReadImageFromDB(int x, int y, int zoom)
{
DataClient.MBTilesClient mBTilesClient = new DataClient.MBTilesClient();
return mBTilesClient.GetTile(x, y, zoom);
}
private int tms2gmaps(int y, int zoom)
{
int ymax = 1 << zoom;
return ymax - y - 1;
}
private byte[] Crop(byte[] image, int x, int y)
{
byte[] output = image;
if(image == null)
{
return output;
}
Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length);
bitmap = Bitmap.CreateBitmap(
bitmap,
x % 2 == 0 ? 0 : bitmap.Width / 2,
y % 2 == 0 ? bitmap.Height / 2 : 0,
bitmap.Width,
bitmap.Height
);
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
bitmap.Recycle();
output = stream.ToArray();
return output;
}
}
Any Help would be greatly appreciated.
Eddie
Use the ITileProvider interface instead of the TileProvider abstract class. Then you won't have issues with constructors.
public class MBTilesProvider : Java.Lang.Object, ITileProvider
I don't know why Xamarin creates both or how the abstract class should be used. I had the same issue, but this solved it for me. The Java.Lang.Object is so you don't have to override the Handle and Dispose methods.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
here is the code is there any way to make it faster cause its slowerden single
public Bitmap pSetInvert(Bitmap _currentBitmap)
{
Bitmap temp = (Bitmap)_currentBitmap;
Bitmap bmap = (Bitmap)temp.Clone();
Color c;
Parallel.For(0, bmap.Width, i =>
{
lock (bmap)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
bmap.SetPixel(i, j, Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B));
}
}
});
return (Bitmap)bmap.Clone();
}
By using lock you are making parallel part of your code to work serially, exactly like a single thread application with synchronization overhead so it actually would be slower.
here is a helper class that access bitmap data directly and you can manipulate image in concurrently.
FastImage Helper Class:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
namespace ImageManipulation
{
class FastImage : IDisposable
{
private Bitmap _buffer;
private byte[] _rawData;
private GCHandle _rawHandle;
private int _formatSize;
private int _width;
private int _height;
public int Width
{
get { return _width; }
}
public int Height
{
get { return _height; }
}
public byte[] GetRawData()
{
return _rawData;
}
public byte this[int index]
{
get { return _rawData[index]; }
set { _rawData[index] = value; }
}
public Color this[int x, int y]
{
get
{
return GetPixel(x, y);
}
set
{
SetPixel(x, y, value);
}
}
public Color GetPixel(int x, int y)
{
var offset = y*_width*_formatSize;
offset += x*_formatSize;
return Color.FromArgb(_rawData[offset + 3], _rawData[offset + 2], _rawData[offset + 1], _rawData[offset]);
}
public void SetPixel(int x, int y, Color value)
{
var offset = y*_width*_formatSize;
offset += x*_formatSize;
_rawData[offset] = value.B;
_rawData[offset + 1] = value.G;
_rawData[offset + 2] = value.R;
_rawData[offset + 3] = value.A;
}
private FastImage() { }
public static FastImage Create(Image source)
{
var image = new FastImage();
var bmpSource = new Bitmap(source);
var bmpData = bmpSource.LockBits(new Rectangle(0, 0, source.Width, source.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmpSource.PixelFormat);
image._width = source.Width;
image._height = source.Height;
image._formatSize = 4;
var stride = bmpSource.Width * image._formatSize;
image._rawData = new byte[stride * bmpSource.Height];
image._rawHandle = GCHandle.Alloc(image._rawData, GCHandleType.Pinned);
var pointer = Marshal.UnsafeAddrOfPinnedArrayElement(image._rawData, 0);
image._buffer = new Bitmap(bmpSource.Width, bmpSource.Height, stride, PixelFormat.Format32bppArgb /*bmpSource.PixelFormat*/, pointer);
bmpSource.UnlockBits(bmpData);
var graphics = Graphics.FromImage(image._buffer);
graphics.DrawImageUnscaledAndClipped(bmpSource, new Rectangle(0, 0, source.Width, source.Height));
graphics.Dispose();
return image;
}
public void Dispose()
{
_rawHandle.Free();
_buffer.Dispose();
}
public void Save(Stream stream)
{
_buffer.Save(stream, ImageFormat.Bmp);
}
public Bitmap ToBitmap()
{
return (Bitmap)_buffer.Clone();
}
}
}
and here is your code using FastImage class:
public Bitmap pSetInvert(Bitmap _currentBitmap)
{
using (var bmap = FastImage.Create(_currentBitmap))
{
Parallel.For(0, bmap.Width, i =>
{
for (int j = 0; j < bmap.Height; j++)
{
var c = bmap.GetPixel(i, j);
bmap.SetPixel(i, j, Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B));
}
});
return bmap.ToBitmap();
}
}
The lock in the Parallel.For is going to cause the code to run slower than with a single-threaded loop. The lock only allows one thread at a time to do useful work, with the added cost of acquiring the lock.
Additionally, GetPixel and SetPixel are extremely slow. They are also not guaranteed to be thread-safe, which is probably why you are getting the InvalidOperationException
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
http://msdn.microsoft.com/en-us/library/System.Drawing.Bitmap(v=vs.110).aspx
Have a look instead at WriteableBitmap. Though the class was introduced with WPF, you can use it from a variety of environments. I recently used it in a console application. WriteableBitmap can be converted to a standard bitmap if needed, or written to a ".bmp" file.
Alternatively, you can use unsafe code to directly access the Bitmap buffer.
If you need to, you can use multiple threads for either WriteableBitmap or unsafe access to the Bitmap buffer since you are directly reading/writing memory.
Here are two versions of your filter routine you can play with. They take the image of one PictureBox and after running through the filter assign it to a second PictureBox.
The first one uses LockBits and can do 10.000 (!) loops for a ca 400x400 image in 40 seconds on my machine.
The second uses Parallel.For in addition to the LockBits and does the same job in 35 seconds here.
Of course these timings are strongly influenced by overhead (and maybe by dumb bugs I made.) But really, using Lockbits is key to speed here as there is so little to 'compute' the parallelization management itself eats up most of the cores' time..
using System.Runtime.InteropServices;
// ..
public void filter1()
{
if (pictureBox2.Image != null) pictureBox2.Image.Dispose();
Bitmap bmp = new Bitmap(pictureBox1.Image);
Size s1 = bmp.Size;
PixelFormat fmt1 = bmp.PixelFormat;
Rectangle rect = new Rectangle(0, 0, s1.Width, s1.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, fmt1);
byte bpp = 4; // <<-------------------set to 3 for 24bbp !!
int size1 = bmpData.Stride * bmpData.Height;
byte[] data = new byte[size1];
Marshal.Copy(bmpData.Scan0, data, 0, size1);
for (int y = 0; y < s1.Height; y++)
{
for (int x = 0; x < s1.Width; x++)
{
int index = y * bmpData.Stride + x * bpp;
data[index + 0] = (byte) (255 - data[index + 0]); // Blue
data[index + 1] = (byte) (255 - data[index + 1]); // Green
data[index + 2] = (byte) (255 - data[index + 2]); // Red
data[index + 3] = 255; // Alpha, comment out for 24 bpp!
}
}
Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
bmp.UnlockBits(bmpData);
pictureBox2.Image = bmp;
}
public void filter2()
{
if (pictureBox2.Image != null) pictureBox2.Image.Dispose();
Bitmap bmp1 = new Bitmap(pictureBox1.Image);
Size s1 = bmp1.Size;
Bitmap bmp2 = new Bitmap(s1.Width, s1.Height);
PixelFormat fmt1 = bmp1.PixelFormat;
Rectangle rect = new Rectangle(0, 0, s1.Width, s1.Height);
BitmapData bmpData1 = bmp1.LockBits(rect, ImageLockMode.ReadOnly, fmt1);
BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.WriteOnly, fmt1);
byte bpp = 4; // set to 3 for 24bbp !!
int size1 = bmpData1.Stride * bmpData1.Height;
byte[] data1 = new byte[size1];
byte[] data2 = new byte[size1];
Marshal.Copy(bmpData1.Scan0, data1, 0, size1);
Marshal.Copy(bmpData2.Scan0, data2, 0, size1);
int degreeOfParallelism = Environment.ProcessorCount - 1;
var options = new ParallelOptions();
options.MaxDegreeOfParallelism = degreeOfParallelism;
Parallel.For(0, bmp1.Width, options, y =>
{
{
for (int x = 0; x < s1.Width; x++)
{
int index = y * bmpData1.Stride + x * bpp;
data2[index + 0] = (byte)(255 - data1[index + 0]); // Blue
data2[index + 1] = (byte)(255 - data1[index + 1]); // Green
data2[index + 2] = (byte)(255 - data1[index + 2]); // Red
data2[index + 3] = 255; // Alpha, comment out for 24 bpp!
}
}
});
Marshal.Copy(data2, 0, bmpData2.Scan0, data2.Length);
bmp1.UnlockBits(bmpData1);
bmp2.UnlockBits(bmpData2);
pictureBox2.Image = bmp2;
}
Note that the 2nd filter routine needs to work on a second Bitmap for general purpose filter algorithms. This simple inversion filter doesn't really need it, but once you write things that access neighbouring pixels like eg.g a blur filter you do..
Also note the order of the channel bytes!
In this post: Possible memory leak in Zxing's System.Drawing.Bitmap a question is asked about a memory leak in the ZXing library. I've downloaded and amended the library with the free of the allocated memory. I've even introduced using(){} statements where applicable but I still get a memory leak.
I have a suspicion. Does the Marshal.Copy maybe do more then just copy the data from source to destination. Do I maybe also have to free the destination after the copy?
As you can see in the code below I even tried allocating the buffer only once and only reallocating it when an image larger than the previous one is requested, but that did not solve the problem.
Regards
Paul
My altered code:
using System;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace System.Drawing
{
public class Bitmap : IDisposable
{
byte[] pixelData = new byte[0];
int width = 0;
int height = 0;
static IntPtr m_BufferPointer = default(IntPtr);
static int m_Size;
/// <summary>
/// Reallocs the buffer when it becomes too small
/// </summary>
private IntPtr ReallocBuffer(int size)
{
if(m_BufferPointer != default(IntPtr))
{
if(m_Size < size)
{
Marshal.FreeHGlobal(m_BufferPointer);
m_BufferPointer = Marshal.AllocHGlobal(size);
}
}
else
{
m_BufferPointer = Marshal.AllocHGlobal(size);
}
m_Size = size;
return m_BufferPointer;
}
public Bitmap (UIImage image)
{
UIImage backingImage = image;
IntPtr rawData;
using (CGImage imageRef = backingImage.CGImage)
{
width = imageRef.Width;
height = imageRef.Height;
using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ())
{
int size = height * width * 4;
rawData = ReallocBuffer(size); //Marshal.AllocHGlobal (height * width * 4);
using (CGContext context = new CGBitmapContext (rawData, width, height, 8, 4 * width, colorSpace, CGImageAlphaInfo.PremultipliedLast))
{
context.DrawImage (new RectangleF (0.0f, 0.0f, (float)width, (float)height), imageRef);
pixelData = new byte[height * width * 4];
Marshal.Copy (rawData, pixelData, 0, pixelData.Length);
}
}
}
}
private static int CountCalled;
private static int LastCountCalled = 20000000; //30411000;
public Color GetPixel (int x, int y)
{
try
{
CountCalled++;
if (CountCalled - LastCountCalled > 100000)
{
Debug.WriteLine (CountCalled);
LastCountCalled += 1000000;
}
byte bytesPerPixel = 4;
int bytesPerRow = width * bytesPerPixel;
int rowOffset = y * bytesPerRow;
int colOffset = x * bytesPerPixel;
int pixelDataLoc = rowOffset + colOffset;
Color ret = Color.FromArgb (pixelData [pixelDataLoc + 3], pixelData [pixelDataLoc + 0], pixelData [pixelDataLoc + 1], pixelData [pixelDataLoc + 2]);
return ret;
}
catch (Exception ex)
{
Console.WriteLine ("Req: {0}x{1}", x, y);
throw ex;
}
}
#region IDisposable implementation
public void Dispose ()
{
pixelData = null;
GC.Collect(0);
}
#endregion
}
}
You need to free the native buffer, CGBitmapContext won't do it for you:
IntPtr rawData = Marshal.AllocHGlobal (height * width * 4);
try {
using (CGContext context = new CGBitmapContext (rawData, width, height, 8, 4 * width, colorSpace, CGImageAlphaInfo.PremultipliedLast))
{
context.DrawImage (new RectangleF (0.0f, 0.0f, (float)width, (float)height), imageRef);
pixelData = new byte[height * width * 4];
Marshal.Copy (rawData, pixelData, 0, pixelData.Length);
}
} finally {
Marshal.FreeHGlobal (rawData);
}
Updated according to Jonathan.Peppers try-finally suggestion in the comments
I was wondering if there was an inexpensive way to get the width and height of a JPEG after loading an array of bytes.
I know JpegBitmapDecoder can get the JPEG's pixel width and height but it loads alot of information as well, which I assume would be an expensive operation.
Is there another way to get the width and height from the array of bytes without decoding it?
Thanks
For some unknown reason, instead of going to bed, I went to work on this.
Here's some code that solves this with minimal storage requirements.
void Main()
{
var filePath=#"path\to\my.jpg";
var bytes=File.ReadAllBytes(filePath);
var dimensions=GetJpegDimensions(bytes);
//or
//var dimensions=GetJpegDimensions(filePath);
Console.WriteLine(dimensions);
}
public static Dimensions GetJpegDimensions(byte[] bytes)
{
using(var ms=new MemoryStream(bytes))
{
return GetJpegDimensions(ms);
}
}
public static Dimensions GetJpegDimensions(string filePath)
{
using(var fs=File.OpenRead(filePath))
{
return GetJpegDimensions(fs);
}
}
public static Dimensions GetJpegDimensions(Stream fs)
{
if(!fs.CanSeek) throw new ArgumentException("Stream must be seekable");
long blockStart;
var buf = new byte[4];
fs.Read(buf, 0, 4);
if(buf.SequenceEqual(new byte[]{0xff, 0xd8, 0xff, 0xe0}))
{
blockStart = fs.Position;
fs.Read(buf, 0, 2);
var blockLength = ((buf[0] << 8) + buf[1]);
fs.Read(buf, 0, 4);
if(Encoding.ASCII.GetString(buf, 0, 4) == "JFIF"
&& fs.ReadByte() == 0)
{
blockStart += blockLength;
while(blockStart < fs.Length)
{
fs.Position = blockStart;
fs.Read(buf, 0, 4);
blockLength = ((buf[2] << 8) + buf[3]);
if(blockLength >= 7 && buf[0] == 0xff && buf[1] == 0xc0)
{
fs.Position += 1;
fs.Read(buf, 0, 4);
var height = (buf[0] << 8) + buf[1];
var width = (buf[2] << 8) + buf[3];
return new Dimensions(width, height);
}
blockStart += blockLength + 2;
}
}
}
return null;
}
public class Dimensions
{
private readonly int width;
private readonly int height;
public Dimensions(int width, int height)
{
this.width = width;
this.height = height;
}
public int Width
{
get{return width;}
}
public int Height
{
get{return height;}
}
public override string ToString()
{
return string.Format("width:{0}, height:{1}", Width, Height);
}
}
I've read a CodeProject article about it a couple years back :)
I'm not 100% sure how good it is, and haven't tested it myself, but the author's definitely happy with it; also his tests prove it's a LOT faster than reading the whole image, as you'd expect :)
Here's the article itself.. Hope it's what you need!
http://www.codeproject.com/KB/cs/ReadingImageHeaders.aspx
The piece of code you're looking for starts about here:
http://www.codeproject.com/KB/cs/ReadingImageHeaders.aspx#premain3
UPD: Also, check the comments in the bottom.. Especially the last (top) one there.. Might be useful to make it more generic
Also, more in-depth, advanced info can be picked up here: http://www.codeproject.com/KB/graphics/iptc.aspx