private void button1_Click(object sender, EventArgs e) { int rib = (int)Math.Sqrt(KSVD_Depth); if (rib * rib < KSVD_Depth) { rib = (int)Math.Sqrt(KSVD_Depth) + 1; } Bitmap InputBitmap = (Bitmap)Bitmap.FromFile("Kawasaki_Valencia_2007_09_320x240.bmp"); Bitmap SparseDicBitmap = new Bitmap((N + 1) * rib, (N + 1) * rib, System.Drawing.Imaging.PixelFormat.Format24bppRgb); int[,] memory = new int[InputBitmap.Width, InputBitmap.Height]; for (points A = new points(InputBitmap.Width, InputBitmap.Height); A.DoIt; A.Inc()) { memory[A.x, A.y] = (int)(InputBitmap.GetPixel(A.x, A.y).GetBrightness() * 255.0); } Matrix[] Dictionairy = new Matrix[N * N]; Matrix PatchOut = new Matrix(N, 0); double[,] pixels = new double[InputBitmap.Width, InputBitmap.Height]; for (int i = 0; i < N * N; i++) { Dictionairy[i] = new Matrix(N, 0); } for (int p = 0; p < KSVD_Depth; p++) { Dictionairy[p].Fill(0); for (points A = new points(InputBitmap.Width / N, InputBitmap.Height / N); A.DoIt; A.Inc()) { Matrix PatchIn = GetMemory(A, memory); if (p == 0) { PatchOut = PatchIn.Average(); setPatch(A, pixels, PatchOut); } else { getpatch(A, pixels, ref PatchOut); PatchOut += (Matrix.Dot(PatchIn - PatchOut, Dictionairy[p - 1]) * Dictionairy[p - 1]); setPatch(A, pixels, PatchOut); } Matrix d = (PatchIn - PatchOut); d.Normalize(); Dictionairy[p] = Dictionairy[p] + Matrix.Sign(d, Dictionairy[p]); DrawPatch(A, InputBitmap, PatchOut); } PicImage.Image = Helper.scaler(InputBitmap, 4, InterpolationMode.NearestNeighbor); PicImage.Refresh(); Dictionairy[p].Normalize(); } DrawCoefs(SparseDicBitmap, Dictionairy, rib, KSVD_Depth); }
public void ReadVGA(byte[] data) { MemoryStream ms = new MemoryStream(data); var loopsCount = ms.ReadB(); ms.Position++; // Skip const 80 var mask = ms.ReadUShortBE(); ms.Position += 2; // Skip unkown var palOffset = ms.ReadUShortBE(); ushort[] offsets = new ushort[loopsCount]; for (int i = 0; i < loopsCount; i++) { offsets[i] = ms.ReadUShortBE(); } for (int i = 0; i < loopsCount; i++) { var loop = new Loop(); Loops.Add(loop); ms.Position = offsets[i]; var cellsCount = ms.ReadUShortBE(); ms.Position += 2; // Skip unknown ushort[] cellOffsets = new ushort[cellsCount]; for (int j = 0; j < cellsCount; j++) { cellOffsets[j] = ms.ReadUShortBE(); } for (int j = 0; j < cellsCount; j++) { var cell = new Cell(this); loop.Cells.Add(cell); ms.Position = cellOffsets[j]; cell.Width = ms.ReadUShortBE(); cell.Height = ms.ReadUShortBE(); cell.X = ms.ReadB(); cell.Y = ms.ReadB(); cell.TransparentColor = ms.ReadB(); ms.Position++; // Skip unknown cell.Pixels = new byte[cell.Width * cell.Height]; PicImage.ReadImageData(ms, ms, cell.Pixels, cell.TransparentColor); } } ms.Position = palOffset; ReadPalette(ms); }
/// <summary> /// 设置图像的缩放模式 /// </summary> /// <param name="mode">AutoSize=2,Zoom=4</param> public void SetSizeMode(int mode) { PicImage.SizeMode = (PictureBoxSizeMode)mode; if (mode == 4 && mImgSource != null) { PicImage.SetZoomScale(1, new Point(mImgSource.Width / 2, mImgSource.Height / 2)); Check100Percent.Checked = false; } else { Check100Percent.Checked = true; } }
public void ReadVGA11(byte[] data) { MemoryStream ms = new MemoryStream(data); var headerSize = ms.ReadUShortBE(); if (headerSize < 14) { throw new FormatException(); } var loopsCount = ms.ReadB(); var scaleFlags = ms.ReadB(); var testb = ms.ReadB(); //if (testb != 1) throw new FormatException(); var sci32ScaleRes = ms.ReadB(); var totalNumberOfCells = ms.ReadUShortBE(); var palOffset = ms.ReadUIntBE(); var loopHeaderSize = ms.ReadB(); if (loopHeaderSize < 16) { throw new FormatException(); } var cellHeaderSize = ms.ReadB(); if (cellHeaderSize != 36 && cellHeaderSize != 52) { throw new FormatException(); } var nativeResolution = ms.ReadUShortBE(); if (palOffset > 0) { ms.Position = palOffset; ReadPalette(ms); } for (int l = 0; l < loopsCount; l++) { var loop = new Loop(); Loops.Add(loop); ms.Position = headerSize + 2 + loopHeaderSize * l; var mirrorInfo = ms.ReadB(); var isMirror = ms.ReadB(); var cellCount = ms.ReadB(); ms.Position += 9; var cellOffset = ms.ReadUIntBE(); if (mirrorInfo == 255) { for (int c = 0; c < cellCount; c++) { var cell = new Cell(this); loop.Cells.Add(cell); ms.Position = cellOffset + cellHeaderSize * c; cell.Width = ms.ReadUShortBE(); cell.Height = ms.ReadUShortBE(); cell.X = ms.ReadShortBE(); cell.Y = ms.ReadShortBE(); cell.TransparentColor = ms.ReadB(); var always_0xa = ms.ReadB(); var temp2 = ms.ReadB(); var temp3 = ms.ReadB(); var totalCellDataSize = ms.ReadUIntBE(); var rleCellDataSize = ms.ReadUIntBE(); var paletteOffset = ms.ReadUIntBE(); var offsetRLE = ms.ReadUIntBE(); if (offsetRLE == 0) { throw new FormatException(); } var offsetLiteral = ms.ReadUIntBE(); var perRowOffsets = ms.ReadUIntBE(); if (always_0xa == 0xa || always_0xa == 0x8a) { if (offsetLiteral == 0) { throw new FormatException(); } } else { if (offsetLiteral != 0) { throw new FormatException(); } } ms.Position = offsetRLE; if (offsetLiteral == 0) { cell.Pixels = ms.ReadBytes(cell.Width * cell.Height); } else { ms.Position = offsetRLE; var msLiteral = new MemoryStream(data); msLiteral.Position = offsetLiteral; cell.Pixels = new byte[cell.Width * cell.Height]; PicImage.ReadImageData(ms, msLiteral, cell.Pixels, cell.TransparentColor); } } } else { foreach (var c in Loops[mirrorInfo].Cells) { var cell = new Cell(this); loop.Cells.Add(cell); cell.Width = c.Width; cell.Height = c.Height; cell.X = (short)-c.X; cell.Y = c.Y; cell.Pixels = new byte[cell.Width * cell.Height]; for (int y = 0; y < cell.Height; y++) { for (int x = 0; x < cell.Width; x++) { cell.Pixels[y * cell.Width + x] = c.Pixels[y * cell.Width + (cell.Width - 1 - x)]; } } } } } }
//---------------------------------- private void ButtonStart_Click(object sender, EventArgs e) { KSVD_Depth = System.Convert.ToInt32(cbDepth.Text); int rib = (int)Math.Sqrt(CoefsN); if (rib * rib < CoefsN) { rib = (int)Math.Sqrt(CoefsN) + 1; } Bitmap InputBitmap = (Bitmap)Bitmap.FromFile("Kawasaki_Valencia_2007_09_320x240.bmp"); Bitmap SparseDicBitmap = new Bitmap((N + 1) * rib, (N + 1) * rib, System.Drawing.Imaging.PixelFormat.Format24bppRgb); PicImage.Image = Helper.scaler(InputBitmap, 2, InterpolationMode.NearestNeighbor); PicCoefs.Image = Helper.scaler(SparseDicBitmap, 256, 256, InterpolationMode.NearestNeighbor); int[,] memory = new int[InputBitmap.Width, InputBitmap.Height]; for (points A = new points(InputBitmap.Width, InputBitmap.Height); A.DoIt; A.Inc()) { memory[A.x, A.y] = (int)(InputBitmap.GetPixel(A.x, A.y).GetBrightness() * 255.0); } Random rnd = new Random(); Matrix[] CopyDictionairy = Matrix.GetMatrixArrayRandom(N, CoefsN, rnd); Matrix[] Dictionairy = Matrix.GetMatrixArrayRandom(N, CoefsN, rnd); Matrix PatchOut; Int32 index = 0; Int32[] Cnt = new Int32[CoefsN]; for (int i = 0; i < CoefsN; i++) { Cnt[i] = 0; } double quant = System.Convert.ToInt32(cbQuant.Text); for (int repeat = 0; repeat < 50; repeat++) { for (points A = new points(InputBitmap.Width / N, InputBitmap.Height / N); A.DoIt; A.Inc()) { Matrix PatchIn = GetMemory(A, memory); PatchOut = PatchIn.Average(); for (int p = 0; p < KSVD_Depth; p++) { Matrix Norm = PatchIn - PatchOut; Matrix Atom = Matrix.MaxDot(Norm, CopyDictionairy, ref index); double pickvalue = Math.Round(Matrix.Dot(PatchIn - PatchOut, Atom) / quant) * quant; PatchOut += (pickvalue * Atom); //FeedBack Norm.Normalize(); Dictionairy[index] = Dictionairy[index] + pickvalue * Norm; Cnt[index] += 1; } DrawPatch(A, InputBitmap, PatchOut); } PicImage.Image = Helper.scaler(InputBitmap, 4, InterpolationMode.NearestNeighbor); PicImage.Refresh(); for (int i = 0; i < CopyDictionairy.Length; i++) { if (Cnt[i] > 2) { Dictionairy[i].Normalize(); CopyDictionairy[i].CopyFrom(Dictionairy[i]); } else { Dictionairy[i].FillRnd(rnd, memory); Dictionairy[i].Normalize(); CopyDictionairy[i].CopyFrom(Dictionairy[i]); } } DrawCoefs(SparseDicBitmap, CopyDictionairy, rib, CoefsN); } }