C # TIFF Bit Wise Check

I'm trying to determine if the use of TIFF index indexing 1BPP white pixel or a black pixel. To check if my code was correct, I made an application to paint the same image as a new image. This is when I noticed some problems, and I feel like beaten head, trying to figure it out.

I'm sure it has something to do with my bit-check!

the original image

origninal image A

Processed Image

enter image description here

Test project can be downloaded at http://www.unclickable.net/code/tiffTest.zip
unsafe {// flipStartPoint

int y; for (y = 0; y < tiffSource.Height; y++) { var Column = (byte*)tiffSource.GetScanlinePointer(y); int x; for (x = 0; x < (tiffSource.Width / 8); x++) { int xm = x * 8; byte b = Column[xm]; if (b > 0) { for (int Z = 0; Z < 8; Z++) { if (((b & (128 >> Z)) != 0)) { if (lowisWhite) { image1.SetPixel((xm + Z), y, Color.FromArgb(0, 255, 255,255)); } } else { if (!lowisWhite) { image1.SetPixel((xm + Z), y, Color.FromArgb(0, 255,255, 255)); } } } } else { if (!lowisWhite) { for (int Z = 0; Z < 8; Z++) { image1.SetPixel((xm + Z), y, Color.FromArgb(0, 255, 255, 255)); } } } } } } 
+4
source share
1 answer

User

it looks like a gimmick, the code below. Keep this in mind for the rest of life: if you're hoping for a response to a discussion board, the code must be cut and is available for all to use and the ads!

Now, I did not include the rest of the partial class, because it is automatically generated C #, when the project starts. You make people jump through hoops, they will not work for you.

I could not find a strange feature GetScanLine. If it is from another library, that is, and what is the fastest way I can check?

Before you run it, I saved your image in the c: \ temp \ bw.tif, making sure that it is set as 1bpp in MS Paint. I also set a breakpoint after downloading the file to prove that the property still .ImageFormat 1bpp. The result appears in the c: \ temp \ out.jpg.

It seems that there are several reasons for the original failed. How are you doing x * 8, or not, it seems to double or curious. I used a different approach to move from x and y to pixel.

 using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace WindowsFormsApplication2 { unsafe public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap tiffSource = new Bitmap("c:\\temp\\bw.tif"); Bitmap image1 = new Bitmap(tiffSource.Width, tiffSource.Height); BitmapData d = tiffSource.LockBits( new Rectangle(new Point(0, 0), tiffSource.Size), ImageLockMode.ReadOnly,tiffSource.PixelFormat); for (int y = 0; y < tiffSource.Height; y++) { byte* Column = (byte*)d.Scan0 + y*d.Stride; for (int x = 0; x < (tiffSource.Width ); x++) if ((Column[(int)(x / 8)] & (128 >> (x % 8))) !=0 ) image1.SetPixel((x), y, Color.FromArgb(0, 0, 0, 0)); else image1.SetPixel((x), y, Color.FromArgb(0, 255, 255, 255)); } tiffSource.UnlockBits(d); image1.Save("c:\\temp\\out.jpg"); } } } : \\ temp \\ bw.tif"); using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace WindowsFormsApplication2 { unsafe public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap tiffSource = new Bitmap("c:\\temp\\bw.tif"); Bitmap image1 = new Bitmap(tiffSource.Width, tiffSource.Height); BitmapData d = tiffSource.LockBits( new Rectangle(new Point(0, 0), tiffSource.Size), ImageLockMode.ReadOnly,tiffSource.PixelFormat); for (int y = 0; y < tiffSource.Height; y++) { byte* Column = (byte*)d.Scan0 + y*d.Stride; for (int x = 0; x < (tiffSource.Width ); x++) if ((Column[(int)(x / 8)] & (128 >> (x % 8))) !=0 ) image1.SetPixel((x), y, Color.FromArgb(0, 0, 0, 0)); else image1.SetPixel((x), y, Color.FromArgb(0, 255, 255, 255)); } tiffSource.UnlockBits(d); image1.Save("c:\\temp\\out.jpg"); } } } tiffSource.Height); using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace WindowsFormsApplication2 { unsafe public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap tiffSource = new Bitmap("c:\\temp\\bw.tif"); Bitmap image1 = new Bitmap(tiffSource.Width, tiffSource.Height); BitmapData d = tiffSource.LockBits( new Rectangle(new Point(0, 0), tiffSource.Size), ImageLockMode.ReadOnly,tiffSource.PixelFormat); for (int y = 0; y < tiffSource.Height; y++) { byte* Column = (byte*)d.Scan0 + y*d.Stride; for (int x = 0; x < (tiffSource.Width ); x++) if ((Column[(int)(x / 8)] & (128 >> (x % 8))) !=0 ) image1.SetPixel((x), y, Color.FromArgb(0, 0, 0, 0)); else image1.SetPixel((x), y, Color.FromArgb(0, 255, 255, 255)); } tiffSource.UnlockBits(d); image1.Save("c:\\temp\\out.jpg"); } } } d.Scan0 + y * d.Stride; using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace WindowsFormsApplication2 { unsafe public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap tiffSource = new Bitmap("c:\\temp\\bw.tif"); Bitmap image1 = new Bitmap(tiffSource.Width, tiffSource.Height); BitmapData d = tiffSource.LockBits( new Rectangle(new Point(0, 0), tiffSource.Size), ImageLockMode.ReadOnly,tiffSource.PixelFormat); for (int y = 0; y < tiffSource.Height; y++) { byte* Column = (byte*)d.Scan0 + y*d.Stride; for (int x = 0; x < (tiffSource.Width ); x++) if ((Column[(int)(x / 8)] & (128 >> (x % 8))) !=0 ) image1.SetPixel((x), y, Color.FromArgb(0, 0, 0, 0)); else image1.SetPixel((x), y, Color.FromArgb(0, 255, 255, 255)); } tiffSource.UnlockBits(d); image1.Save("c:\\temp\\out.jpg"); } } } ++) using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace WindowsFormsApplication2 { unsafe public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap tiffSource = new Bitmap("c:\\temp\\bw.tif"); Bitmap image1 = new Bitmap(tiffSource.Width, tiffSource.Height); BitmapData d = tiffSource.LockBits( new Rectangle(new Point(0, 0), tiffSource.Size), ImageLockMode.ReadOnly,tiffSource.PixelFormat); for (int y = 0; y < tiffSource.Height; y++) { byte* Column = (byte*)d.Scan0 + y*d.Stride; for (int x = 0; x < (tiffSource.Width ); x++) if ((Column[(int)(x / 8)] & (128 >> (x % 8))) !=0 ) image1.SetPixel((x), y, Color.FromArgb(0, 0, 0, 0)); else image1.SetPixel((x), y, Color.FromArgb(0, 255, 255, 255)); } tiffSource.UnlockBits(d); image1.Save("c:\\temp\\out.jpg"); } } } (x / using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace WindowsFormsApplication2 { unsafe public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap tiffSource = new Bitmap("c:\\temp\\bw.tif"); Bitmap image1 = new Bitmap(tiffSource.Width, tiffSource.Height); BitmapData d = tiffSource.LockBits( new Rectangle(new Point(0, 0), tiffSource.Size), ImageLockMode.ReadOnly,tiffSource.PixelFormat); for (int y = 0; y < tiffSource.Height; y++) { byte* Column = (byte*)d.Scan0 + y*d.Stride; for (int x = 0; x < (tiffSource.Width ); x++) if ((Column[(int)(x / 8)] & (128 >> (x % 8))) !=0 ) image1.SetPixel((x), y, Color.FromArgb(0, 0, 0, 0)); else image1.SetPixel((x), y, Color.FromArgb(0, 255, 255, 255)); } tiffSource.UnlockBits(d); image1.Save("c:\\temp\\out.jpg"); } } } Color.FromArgb ( using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace WindowsFormsApplication2 { unsafe public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap tiffSource = new Bitmap("c:\\temp\\bw.tif"); Bitmap image1 = new Bitmap(tiffSource.Width, tiffSource.Height); BitmapData d = tiffSource.LockBits( new Rectangle(new Point(0, 0), tiffSource.Size), ImageLockMode.ReadOnly,tiffSource.PixelFormat); for (int y = 0; y < tiffSource.Height; y++) { byte* Column = (byte*)d.Scan0 + y*d.Stride; for (int x = 0; x < (tiffSource.Width ); x++) if ((Column[(int)(x / 8)] & (128 >> (x % 8))) !=0 ) image1.SetPixel((x), y, Color.FromArgb(0, 0, 0, 0)); else image1.SetPixel((x), y, Color.FromArgb(0, 255, 255, 255)); } tiffSource.UnlockBits(d); image1.Save("c:\\temp\\out.jpg"); } } } Color.FromArgb ( using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace WindowsFormsApplication2 { unsafe public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap tiffSource = new Bitmap("c:\\temp\\bw.tif"); Bitmap image1 = new Bitmap(tiffSource.Width, tiffSource.Height); BitmapData d = tiffSource.LockBits( new Rectangle(new Point(0, 0), tiffSource.Size), ImageLockMode.ReadOnly,tiffSource.PixelFormat); for (int y = 0; y < tiffSource.Height; y++) { byte* Column = (byte*)d.Scan0 + y*d.Stride; for (int x = 0; x < (tiffSource.Width ); x++) if ((Column[(int)(x / 8)] & (128 >> (x % 8))) !=0 ) image1.SetPixel((x), y, Color.FromArgb(0, 0, 0, 0)); else image1.SetPixel((x), y, Color.FromArgb(0, 255, 255, 255)); } tiffSource.UnlockBits(d); image1.Save("c:\\temp\\out.jpg"); } } } out.jpg"); using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace WindowsFormsApplication2 { unsafe public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Bitmap tiffSource = new Bitmap("c:\\temp\\bw.tif"); Bitmap image1 = new Bitmap(tiffSource.Width, tiffSource.Height); BitmapData d = tiffSource.LockBits( new Rectangle(new Point(0, 0), tiffSource.Size), ImageLockMode.ReadOnly,tiffSource.PixelFormat); for (int y = 0; y < tiffSource.Height; y++) { byte* Column = (byte*)d.Scan0 + y*d.Stride; for (int x = 0; x < (tiffSource.Width ); x++) if ((Column[(int)(x / 8)] & (128 >> (x % 8))) !=0 ) image1.SetPixel((x), y, Color.FromArgb(0, 0, 0, 0)); else image1.SetPixel((x), y, Color.FromArgb(0, 255, 255, 255)); } tiffSource.UnlockBits(d); image1.Save("c:\\temp\\out.jpg"); } } } 
+2
source

All Articles