Detail when rendering 1bpp TIF Images


J

Jay Dee

Detail when rendering 1bpp TIF Images

Windows Picture and Fax Viewer on Microsoft Windows 2000, is capable
of rendering large TIF Images to a good level of detail with minimal
memory usage.

Windows Picture and Fax Viewer on Microsoft Windows XP and Vista douse
not seem to be able to achieve the same standard. Memory usage and
load times are considerably higher; I am assuming this is due to the
image being converted to a 32bpp image for some unknown reason.


I am creating an application to view these large black and white TIF
images.

System.Drawing.Graphics douse not have much support for 1bpp indexed
images so I attempted to manually scale images where required.

I have been working on the following method for some time now but I am
struggling when it comes to fine detail.

I would like to be able to render my new scaled image with the same
level of detail as Windows 2000 did.

Can anyone suggest any improvements or suggestions to improve image
quality.

Many thanks

Jay Dee

<CODE>

private Image sizeImage(Image image, int width, int height, bool
colorCorrection)
{
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)(width) / (float)image.Width);
nPercentH = ((float)(height) / (float)image.Height);

if (nPercentH < nPercentW) { nPercent = nPercentH; }
else { nPercent = nPercentW; }

width = (int)(image.Width * nPercent);
height = (int)(image.Height * nPercent);

if (image.PixelFormat == PixelFormat.Format32bppRgb)
{
return new Bitmap(image, width, height);
}
else if (image.PixelFormat == PixelFormat.Format1bppIndexed)
{
unsafe
{
#region

Bitmap bitmap = (Bitmap)image;
Bitmap bitmap2 = new Bitmap(width, height,
bitmap.PixelFormat);

BitmapData bitmapData = bitmap.LockBits(new Rectangle(0,
0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite,
PixelFormat.Format1bppIndexed);
BitmapData bitmapData2 = bitmap2.LockBits(new Rectangle(0,
0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadWrite,
PixelFormat.Format1bppIndexed);

bitmap2.SetResolution(bitmap.VerticalResolution,
bitmap.HorizontalResolution);

float scaleX = ((float)bitmap.Width) / (float)(width);
float scaleY = ((float)bitmap.Height) / (float)(height);

byte* p = (byte*)bitmapData.Scan0;
byte* p2 = (byte*)bitmapData2.Scan0;


if (colorCorrection)
{
// use color corection
#region

for (int y = 0; y < bitmap2.Height; y++)
{
int yStride = y * bitmapData2.Stride;

int y2 = (int)(y * scaleY);
int y2Stride = y2 * bitmapData.Stride;

for (int x = 0; x < bitmap2.Width; x++)
{
int x2 = (int)(x * scaleX);

// get pixel
int index = y2Stride + (x2 >> 3);
byte mask = (byte)(0x80 >> (x2 & 0x7));
int n = p[index] & mask;
bool white = n > 0 ? true : false;

if (white)
{
// get pixel
index = y2Stride + (x2 + 1 >> 3);
mask = (byte)(0x80 >> (x2 + 1 & 0x7));
n = p[index] & mask;
white = n > 0 ? true : false;

if (white)
{
// get pixel
index = (y2 + 1) * bitmapData.Stride +
(x2 + 1 >> 3);
mask = (byte)(0x80 >> (x2 + 1 & 0x7));
n = p[index] & mask;
white = n > 0 ? true : false;

int index2 = yStride + (x >> 3);
byte mask2 = (byte)(0x80 >> (x &
0x7));

if (white)
{
p2[index2] |= mask2;
}
else
{
p2[index2] &= (byte)(mask2 ^
0xff);
}
}
else
{
// set pixel
int index2 = yStride + (x >> 3);
byte mask2 = (byte)(0x80 >> (x &
0x7));

p2[index2] &= (byte)(mask2 ^ 0xff);
}
}
else
{
// set pixel
int index2 = yStride + (x >> 3);
byte mask2 = (byte)(0x80 >> (x & 0x7));
if (white)
{
p2[index2] |= mask2;
}
else
{
p2[index2] &= (byte)(mask2 ^ 0xff);
}
}
}
}


#endregion
}
else
{
#region

for (int y = 0; y < bitmap2.Height; y++)
{
int yStride = y * bitmapData2.Stride;

int y2 = (int)(y * scaleY);
int y2Stride = y2 * bitmapData.Stride;

for (int x = 0; x < bitmap2.Width; x++)
{
int x2 = (int)(x * scaleX);

// get pixel
int index = y2Stride + (x2 >> 3);
byte mask = (byte)(0x80 >> (x2 & 0x7));
int n = p[index] & mask;
bool white = n > 0 ? true : false;

// set pixel
int index2 = yStride + (x >> 3);
byte mask2 = (byte)(0x80 >> (x & 0x7));
if (white)
{
p2[index2] |= mask2;
}
else
{
p2[index2] &= (byte)(mask2 ^ 0xff);
}
}
}

#endregion
}

bitmap.UnlockBits(bitmapData);
bitmap2.UnlockBits(bitmapData2);

return bitmap2;

#endregion
}
}
else
{
throw new Exception("Unsupported PixelFormat");
}
}

</CODE>
 
Ad

Advertisements

P

Peter Duniho

[...]
Can anyone suggest any improvements or suggestions to improve image
quality.

You should post your question in a forum where questions about image
processing are on-topic. The only interesting part of your code with
respect to the question is everything that appears within the "unsafe"
block, which in turn is strictly about image processing, and has nothing
to do with C# or .NET. You'll find you get much better replies by posting
to a forum specifically for the topic.

Pete
 

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