Does anyone know what the problem is?
I received this warning. The xxx field is never assigned and will always have a default value of null onprivate static Quantizer quantit;
I do not know what to do to fix it, because when I try to use quantit.Quantize()debug it says: "The reference to the object is not set to the instance of the object." and point toau = quantit.Quantize();
The code:
public class Quantization : System.Windows.Forms.Form
{
private static Quantizer quantit;
private Button btnLoad;
private PictureBox imgPhoto;
public Quantization()
{
btnLoad = new Button();
btnLoad.Text = "&Load";
btnLoad.Left = 10;
btnLoad.Top = 10;
btnLoad.Click += new System.EventHandler(this.OnLoadClick);
imgPhoto = new PictureBox();
imgPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
imgPhoto.Width = this.Width / 2;
imgPhoto.Height = this.Height / 2;
imgPhoto.Left = (this.Width - imgPhoto.Width) / 2;
imgPhoto.Top = (this.Height - imgPhoto.Height) / 2;
imgPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(btnLoad);
this.Controls.Add(imgPhoto);
}
protected void OnLoadClick(object sender, System.EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open Image";
dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;
if (dlg.ShowDialog() == DialogResult.OK)
{
Bitmap au;
au = quantit.Quantize();
imgPhoto.Image = au;
}
dlg.Dispose();
}
[STAThread]
static void Main(string[] args)
{
Application.Run(new Quantization());
}
}
Grade:
namespace ImageManipulation
{
public unsafe abstract class Quantizer
{
public Quantizer(bool singlePass)
{
_singlePass = singlePass;
}
public Bitmap Quantize()
{
Image source = Image.FromFile("C:\\Users\\crashboy\\Downloads\\image009.jpg");
int height = source.Height;
int width = source.Width;
Rectangle bounds = new Rectangle(0, 0, width, height);
Bitmap copy = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Bitmap output = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
using (Graphics g = Graphics.FromImage(copy))
{
g.PageUnit = GraphicsUnit.Pixel;
g.DrawImage(source, bounds);
}
BitmapData sourceData = null;
try
{
sourceData = copy.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
if (!_singlePass)
FirstPass(sourceData, width, height);
output.Palette = this.GetPalette(output.Palette);
SecondPass(sourceData, output, width, height, bounds);
}
finally
{
copy.UnlockBits(sourceData);
}
return output;
}
protected virtual void FirstPass(BitmapData sourceData, int width, int height)
{
byte* pSourceRow = (byte*)sourceData.Scan0.ToPointer();
Int32* pSourcePixel;
for (int row = 0; row < height; row++)
{
pSourcePixel = (Int32*)pSourceRow;
for (int col = 0; col < width; col++, pSourcePixel++)
InitialQuantizePixel((Color32*)pSourcePixel);
pSourceRow += sourceData.Stride;
}
}
protected virtual void SecondPass(BitmapData sourceData, Bitmap output, int width, int height, Rectangle bounds)
{
BitmapData outputData = null;
try
{
outputData = output.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
byte* pSourceRow = (byte*)sourceData.Scan0.ToPointer();
Int32* pSourcePixel = (Int32*)pSourceRow;
Int32* pPreviousPixel = pSourcePixel;
byte* pDestinationRow = (byte*)outputData.Scan0.ToPointer();
byte* pDestinationPixel = pDestinationRow;
byte pixelValue = QuantizePixel((Color32*)pSourcePixel);
*pDestinationPixel = pixelValue;
for (int row = 0; row < height; row++)
{
pSourcePixel = (Int32*)pSourceRow;
pDestinationPixel = pDestinationRow;
for (int col = 0; col < width; col++, pSourcePixel++, pDestinationPixel++)
{
if (*pPreviousPixel != *pSourcePixel)
{
pixelValue = QuantizePixel((Color32*)pSourcePixel);
pPreviousPixel = pSourcePixel;
}
*pDestinationPixel = pixelValue;
}
pSourceRow += sourceData.Stride;
pDestinationRow += outputData.Stride;
}
}
finally
{
output.UnlockBits(outputData);
}
}
protected virtual void InitialQuantizePixel(Color32* pixel)
{
}
protected abstract byte QuantizePixel(Color32* pixel);
protected abstract ColorPalette GetPalette(ColorPalette original);
private bool _singlePass;
[StructLayout(LayoutKind.Explicit)]
public struct Color32
{
[FieldOffset(0)]
public byte Blue;
[FieldOffset(1)]
public byte Green;
[FieldOffset(2)]
public byte Red;
[FieldOffset(3)]
public byte Alpha;
[FieldOffset(0)]
public int ARGB;
public Color Color
{
get { return Color.FromArgb(Alpha, Red, Green, Blue); }
}
}
}
}