This draws scanline of the image to System.Drawing.Graphics
. It's quite simple and it's optimized in order to merge neigbour samples with the same color into single rectangle (instead of drawing sample-by-sample in 1x1 rectangles).
I don't like the duplication of this code inside and outside the loop:
updateAlpha(brush, previousColor);
graphics.FillRectangle(brush, previousIndex + positionX, y, i - previousIndex, 1);
Do you know the good way how to follow DRY principle here and to keep code easy to understand? Or this is a kind of perfectionism and all is already fine?
private static void drawGlyphRowToGraphics(int rowIndex, FT_Bitmap glyphBitmap,
Graphics graphics, float positionX, float positionY, SolidBrush brush)
{
float y = rowIndex + positionY;
byte previousColor = 0;
int previousIndex = 0;
for (int i = 0; i < glyphBitmap.Width; ++i)
{
byte color = (byte)(255 - glyphBitmap.Buffer[i + rowIndex * glyphBitmap.Width]);
if (i == 0)
previousColor = color;
if (color != previousColor)
{
updateAlpha(brush, previousColor);
graphics.FillRectangle(brush, previousIndex + positionX, y, i - previousIndex, 1);
previousColor = color;
previousIndex = i;
}
}
if (glyphBitmap.Width > 0)
{
updateAlpha(brush, previousColor);
graphics.FillRectangle(brush, previousIndex + positionX, y, glyphBitmap.Width - previousIndex, 1);
}
}
private static void updateAlpha(SolidBrush brush, byte gray)
{
brush.Color = Color.FromArgb(255 - gray, brush.Color.R, brush.Color.G, brush.Color.B);
}