For a mobile application I have to generate a 1-bit-bitmap out of a 24-bit-bitmap. The problem is, that the result isn't correct, so i made this little project to try it on my desktop-pc. the creation works, but the result isn't ok, as you can see.
You can hardly read anything because a lot of bits aren't at the right position anymore but moved some pixels left or right.
This is the code i use for creation:
int z = 0;
int bitNumber = 0;
//the new 1Bit-byte-Array
byte[] oneBitImage = new byte[(bmp.Height * bmp.Width) / 8];
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)(void*)bmData.Scan0.ToPointer();
int stopAddress = (int)p + bmp.Height * bmData.Stride;
while ((int)p != stopAddress)
{
if (*p < 128) // is black
oneBitImage[z] = (byte)(oneBitImage[z] | Exp(bitNumber)); //Set a Bit on the specified position
p += 3;
bitNumber++;
if (bitNumber == 8)
{
bitNumber = 0;
z++;
}
}
}
bmp.UnlockBits(bmData);
//Convert into 1-bit-bmp to check result
Bitmap newbmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format1bppIndexed);
BitmapData bmpData = newbmp.LockBits(
new Rectangle(0, 0, newbmp.Width, newbmp.Height),
ImageLockMode.WriteOnly, newbmp.PixelFormat);
Marshal.Copy(oneBitImage, 0, bmpData.Scan0, oneBitImage.Length);
newbmp.UnlockBits(bmpData);
newbmp.Save(fileName, ImageFormat.Bmp);
Short explanation:
I run through every third byte, and if this byte - the first one of a 3-byte-group (pixel in 24-bit) - is lower than 128 I put a bit at the specified position.
EXP gives me the exponent...
Switch the bits in every output byte around. Bit 7 should be bit 0, etc.
I would convert three bytes to a "real" color (long value or similar) and see if the result is bigger than half of 16.7m .
Related
We are using a camera that acquires up to 60 frames per second, providing Bitmaps for us to use in our codebase.
As our wpf-app requires, these bitmaps are scaled based on a scaling factor; That scaling-process is by far the most limiting factor when it comes to actually displaying 60 fps. I am aware of new Bitmap(Bitmap source, int width, int height) which is obviously the simplest way to resize a Bitmap;
Nevertheless, I am trying to implement a "manual" approach using BitmapData and pointers. I have come up with the following:
public static Bitmap /*myMoBetta*/ResizeBitmap(this Bitmap bmp, double scaleFactor)
{
int desiredWidth = (int)(bmp.Width * scaleFactor),
desiredHeight = (int)(bmp.Height * scaleFactor);
var scaled = new Bitmap(desiredWidth, desiredHeight, bmp.PixelFormat);
int formatSize = (int)Math.Ceiling(Image.GetPixelFormatSize(bmp.PixelFormat)/8.0);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
BitmapData scaledData = scaled.LockBits(new Rectangle(0, 0, scaled.Width, scaled.Height), ImageLockMode.WriteOnly, scaled.PixelFormat);
unsafe
{
var srcPtr = (byte*)bmpData.Scan0.ToPointer();
var destPtr = (byte*)scaledData.Scan0.ToPointer();
int scaledDataSize = scaledData.Stride * scaledData.Height;
int nextPixel = (int)(1 / scaleFactor)*formatSize;
Parallel.For(0, scaledDataSize - formatSize,
i =>
{
for (int j = 0; j < formatSize; j++)
{
destPtr[i + j] = srcPtr[i * nextPixel + j];
}
});
}
bmp.UnlockBits(bmpData);
bmp.Dispose();
scaled.UnlockBits(scaledData);
return scaled;
}
Given scalingFactor < 1.
Actually using this algorithm does not seem to work, though. How are the bits of each pixel arranged in memory, exactly? My guess was that calling Image.GetPixelFormatSize() and deviding its result by 8 returns the number of bytes per pixel; But continuing to copy only formatSize amout of bytes every 1 / scaleFactor * formatSize byte results in a corrupted image.
What am I missing?
After some more research I bumped into OpenCV that has it's own .NET implementation with Emgu.CV, containing relevant methods for faster resizing.
My ResizeBitmap()-function has shrinked significantly:
public static Bitmap ResizeBitmap(this Bitmap bmp, int width, int height)
{
var desiredSize = new Size(width, height);
var src = new Emgu.CV.Image<Rgb, byte>(bmp);
var dest = new Emgu.CV.Image<Rgb, byte>(desiredSize);
Emgu.CV.CvInvoke.Resize(src, dest, desiredSize);
bmp.Dispose();
src.Dispose();
return dest.ToBitmap();
}
I have not tested performance thouroughly, but while debugging, this implementation reduced executiontime from 22ms with new Bitmap(source, width, height) to about 7ms.
I have a running code, which gets any filetypes and show that file in a picturebox full of black and white pixels representing opened file bits
////////////////
int bmpWidth = 128000;
int startIndex = 0;
Int64 tempz = Convert.ToInt64(bmpWidth);
long tmp2 = fs.Length / tempz + 1;
int bmpHeight = Convert.ToInt32(tmp2);
Bitmap bmp = new Bitmap(bmpWidth, bmpHeight, PixelFormat.Format1bppIndexed);
////////////////
long all = 0;
byte[] array = new byte[bmpWidth];
int read = 0;
fs.Seek(startIndex, SeekOrigin.Begin);
all += startIndex;
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
Int64 ptrFirstPixel = bmpData.Scan0.ToInt64();
int y = 0;
while ((read = fs.Read(array, 0, array.Length)) > 0)
{
if (read < array.Length)
{
byte[] arrayNew = new byte[read];
Array.Copy(array, arrayNew, read);
array = arrayNew;
}
Marshal.Copy(array, 0, new IntPtr(ptrFirstPixel + y * bmpData.Stride), array.Length/8);
y++;
all += read;
}
bmp.UnlockBits(bmpData);
imageBox.Image = bmp;
I have a 4GB ram, how its possible that I'm opening a 4.5GB file?
And also, when I try opening 2 or 4 GB file, only 200MB of memory is used, where are the rest of the data coming from?
During marshal.copy and filestream.seek the system use memory for byte by byte for hole filestream and bitmap or its coming from hard disk?
I have a logical and semantic misunderstanding in my mind, any help!?
Some of my resulting images are slanted, some are not.
Expected Result: (529x22)
Actual Result: (529x22)
Don't mind the different image sizes, these are screenshots. They are both 529x22.
The code I am using, I just got this from an answer on a question here at SO.
// some other method
byte[] pixels = new byte[size - 16];
Array.Copy(this.data, offset, pixels, 0, pixels.Length);
this.ByteToImage(w, h, pixels);
// builds the pixels to a image
private Bitmap ByteToImage(int w, int h, byte[] pixels)
{
var bmp = new Bitmap(w, h, PixelFormat.Format16bppRgb565);
var BoundsRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(BoundsRect,
ImageLockMode.WriteOnly,
bmp.PixelFormat);
// bytes => not using this because it gives error
// eg. pixel.Length = 16032, bytes = 16064
int bytes = bmpData.Stride * bmp.Height;
Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
bmp.UnlockBits(bmpData);
return bmp;
}
I'm confused because some works ok, not slanted. But others are slanted. What did I miss?
Update
As stated in the comments and answers, the problem is how I'm calculating stride. I'm still confused on how to do it but I tried this:
public static void RemovePadding(this Bitmap bitmap)
{
int bytesPerPixel = Image.GetPixelFormatSize(bitmap.PixelFormat) / 8;
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
var pixels = new byte[bitmapData.Width * bitmapData.Height * bytesPerPixel];
for (int row = 0; row < bitmapData.Height; row++)
{
var dataBeginPointer = IntPtr.Add(bitmapData.Scan0, row * bitmapData.Stride);
Marshal.Copy(dataBeginPointer, pixels, row * bitmapData.Width * bytesPerPixel, bitmapData.Width * bytesPerPixel);
}
Marshal.Copy(pixels, 0, bitmapData.Scan0, pixels.Length);
bitmap.UnlockBits(bitmapData);
}
But the result is (more slanted):
This seems to work here:
private Bitmap ByteToImage(int w, int h, byte[] pixels)
{
var bmp = new Bitmap(w, h, PixelFormat.Format16bppRgb565);
byte bpp = 2;
var BoundsRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(BoundsRect,
ImageLockMode.WriteOnly,
bmp.PixelFormat);
// copy line by line:
for (int y = 0; y < h; y++ )
Marshal.Copy(pixels, y * w * bpp, bmpData.Scan0 + bmpData.Stride * y, w * bpp);
bmp.UnlockBits(bmpData);
return bmp;
}
I use a loop to place each row of data at the right spot. The data do not include the padding, but the target address must do so.
Therefore we need to multiply the data access by the actual width * bytePerPixel but the target adress by the Stride, i.e. the length of the scanline, padded to the next multiple of four bytes. For width=300 it is stride=300, for width=301 it is stride=304..
Moving all pixel data in one step can only work when there is no padding, i.e. when the width is a multiple of 4.
This expects the stride to correspond to the width, without padding. There can be padding. The padding would "eat" some of the next line, which will therefore appear to shift left.
Since the padding breaks up the lines, the only real way to deal with it (other than using the same padding everywhere) is copying line by line. You can calculate the starting address of a line with bmpData.Scan0 + y * bmpData.Stride. Copy starting there, for every y.
// bytes => not using this because it gives error
Yes, because your array does not have padding. So you were telling it to copy more data than the array held.
hope you all doing well. I did write a bit of codes in C# using Aforge library. I wanted to crop my main image captured from webcam so as to have a nice ROI. When I use threshold value of 0 everything should be in white pixels (total of lets say 26880 pixels) but it seems that I have some black pixels (578 pixels) within my cropped image. any idea of what may caused it? when I don't crop my image everything is fine.
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
Bitmap bmp = new Bitmap(x2box, y2box);
bmp = img.Clone(new Rectangle(x1box, y1box, x2box, y2box), eventArgs.Frame.PixelFormat);
Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
Bitmap img1 = filter.Apply(bmp);
Threshold tresh = new Threshold((int)tresh1); // tresh1 is 0-255 but is set to zero here
tresh.ApplyInPlace(img1);
int iterator = 1; int xrow = 0; // here i use these constant to calculate location of the pixels
byte[] arraybyte = BitmapToByteArray(img1);
for (int i = 0; i < arraybyte.Length; i++)
{
if (i - iterator * img1.Width == 0)
{
xrow++;
iterator++;
}
if (arraybyte[i] == 0) // if pixel is black
{
X_val.Add(i - xrow * img1.Width);
Y_val.Add(iterator);
}
}
for (int i = 0; i < X_val.Count; i++)
{
YAve += Y_val[i];
XAve += X_val[i];
}
MessageBox.Show(X_val.Count.ToString()); // shows non-zero value!
the BitmapToByteArray method is as follow:
public static byte[] BitmapToByteArray(Bitmap bitmap)
{
BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
int numbytes = bmpdata.Stride * bitmap.Height;
byte[] bytedata = new byte[numbytes];
IntPtr ptr = bmpdata.Scan0;
Marshal.Copy(ptr, bytedata, 0, numbytes);
bitmap.UnlockBits(bmpdata);
return bytedata;
}
The number of bytes for each row of the Bitmap will be enforced to be a multiple of 4. If roi width * bytes per pixel is not a multiple of 4, you will have padding bytes at the end of each row.
They will not be thresholded as they aren't really part of the Bitmap, so their value may be 0. Your BitmapToByteArray method might not be padding-aware and read every byte.
I am writing a function to get difference between two bitmap images in visual studio 2010. I have a function that takes two bitmap images as parameters, I use unlock bits to get data of each pixel,both the images are of equal resolution and dimensions.
When I use unlock bits for only one image it works well, but when I use it for both simultaneously in the same function it gives an exception
BITMAP REGION IS ALREADY LOCKED
code:
public Bitmap Invert(Bitmap b,Bitmap c)
{
BitmapData bmData =
b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
// for image 2
BitmapData data2 =
c.LockBits(new System.Drawing.Rectangle(0,
0,
c.Width,
c.Height),
ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
int stride1 = data2.Stride;
System.IntPtr Scan1 = data2.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
byte* q = (byte*)(void*)Scan1;
nOffset = stride - b.Width * 3;
nWidth = b.Width * 3;
for (y = 0; y < b.Height; ++y)
{
for (x = 0; x < nWidth; ++x)
{
p[0] = (byte)(p[0]-q[0]);
++p;
++q;
}
p += nOffset;
q += nOffset;
}
}
b.UnlockBits(bmData);
c.UnlockBits(data2);
return b;
}
Becuase you only need to compare images , I would suggesst opening them in read mode only i.e:
BitmapData data2 = c.LockBits(new System.Drawing.Rectangle(0,
0,
c.Width,
c.Height),
ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);