processing huge bitmap pixel by pixel performance

B

Buthrakaur

Hi,
I'm developing a printing library for a printer, which supports only
raster image data. So, I need to work with a bitmap of A4 size in
300DPI (it's some 2400x3000 in total). I work with with the bitmap data
in smaller pieces (small bitmap for each row of printed text), so I
don't face any problems with memory allocation etc. My problem is the
performance of a method, which transforms pixels of the bitmap to the
format accepted by the printer. The method processes the bitmaps pixel
by pixel and transforms information from 8 neighbour pixels to 1 byte
(1bit means 1 print dot). Is there any faster approach than calling
bitmap.GetPixel(x, y)? It looks like this method consumes a majority of
the processing time...

My method for processing one row of a bitmap looks like this (I use
pixel doubling here, so 4 bitmap pixels fill 8 bits/1B):

<pre>
protected void BitmapRowToBytes(Bitmap bmp, int rowIdx,
List<byte> arr)
{
int curShift = 0;
int curB = 0;
int val = 0, valOrig = 0;
for (int x = 0; x < bmp.Width; x++)
{
if (bmp.GetPixel(x, rowIdx) == Color.Black)
val = 3;
else
val = 0;
valOrig = val;
val = val << 2 * (3 - curShift);
curB = (curB | val);
if (curShift >= 3)
{
arr.Add((byte)curB);
curShift = 0;
curB = 0;
}
else
{
curShift++;
}
}
}
</pre>

thanks in advance for any advice how to speed it up,
Filip
 
B

Buthrakaur

I tried to use the LockBits method, but I always get ArgumentException
even if I use right PixelFormat. My code looks like this:

Bitmap bmp = new Bitmap(bmpWidth, bmpHeight,
PixelFormat.Format16bppRgb555);
Graphics g = Graphics.FromImage(bmp);
//some Graphics operations
g.Disopose();

BitmapData bd = bmp.LockBits(new Rectangle(0, rowIdx, bmp.Width, 1),
ImageLockMode.ReadOnly, PixelFormat.Format16bppRgb555);

try
{
byte[] rgbValues = new byte[bmp.Width * 2];//16bpp
IntPtr ptr = bd.Scan0;
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0,
rgbValues.Length);
for (int i = 0; i < rgbValues.Length-1; i+=2)
{
//some pixel processing logic
}
}
finally
{
bmp.UnlockBits(bd);
}

bmp.Dispose();

What am I doing wrong? Or is this a problem with DIB vs DDB bitmap?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top