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
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