private void myTestFun() { Mapper map = new Mapper(0.9, new Point(10, 10), new Point(5, 5), 4, 16, 10, 11, 12, 13, 14, 15, 16, 17, false); MappedPoint mp = map.MapPoint(11, 10, 20); Bitmap bitmap = (Bitmap)Bitmap.FromFile(@"C:\Documents and Settings\Marcin\Pulpit\lena.jpg"); int sdelta = 4; int bdelta = 16; Domain d = new Domain(new Point(0,0),new Point(0,bdelta), new Point(bdelta,bdelta),new Point(bdelta,0), bdelta / sdelta); Structure.Region r = new Structure.Region(new Point(0,0),new Point(0,sdelta), new Point(sdelta,sdelta),new Point(sdelta,0)); double s = MNTools.ComputeContractivityFactor(d, r, bitmap); }
public Bitmap DecompressImage(out Bitmap bitmapWithGrid) { int steps = (int)Math.Truncate(Math.Log(smallDelta, 2) / Math.Log(a, 2)); Bitmap bit = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); foreach (MappedPoint p in interpolationPoints) { bit.SetPixel(p.X, p.Y, Color.FromArgb((int)p.Val, (int)p.Val, (int)p.Val)); interPixel++; } for (int t = 0; t < steps; t++) { for (int i = 0; i < Math.Min(steps, dMax); i++) { for (int j = 0; j < this.interpolationPoints.Count; j += 4) { int coresspondingDomain = addresses[j / 4]; if (coresspondingDomain != -1) { double contractivityFactor = contractivityFactors[j / 4]; //contractivityFactor = 0; MyDomain md = FindDomainByAddress(coresspondingDomain); Mapper mapper = new Mapper(contractivityFactor, md.Domain.Vertices[1], interpolationPoints[j + 1], this.smallDelta, this.bigDelta, md.Vals[0], md.Vals[1], md.Vals[2], md.Vals[3], (int)interpolationPoints[j].Val, (int)interpolationPoints[j + 1].Val, (int)interpolationPoints[j + 2].Val, (int)interpolationPoints[j + 3].Val, false); Point[] prevPoint = md.Domain.Vertices; int[] prevVal = md.Vals; for (int k = 0; k < md.Domain.Size * md.Domain.Size; k++) { MappedPoint[] newPoint = new MappedPoint[prevPoint.Length]; for (int pp = 1; pp <= 1; pp++) { newPoint[pp] = mapper.MapPoint(prevPoint[pp].X, prevPoint[pp].Y, (double)prevVal[pp]); Structure.Region tempRegion = FindRegion(newPoint[pp]); if (tempAdd != -1 && tempS != -1) { md = FindDomainByAddress(tempAdd); mapper = new Mapper(tempS, md.Domain.Vertices[1], tempRegion.Vertices[1], this.smallDelta, this.bigDelta, md.Vals[0], md.Vals[1], md.Vals[2], md.Vals[3], (int)tempRegion[0, tempRegion.Size], (int)tempRegion[0, 0], (int)tempRegion[tempRegion.Size, 0], (int)tempRegion[tempRegion.Size, tempRegion.Size], true); Console.Out.WriteLine(md.Domain.Vertices[1].X + " " + md.Domain.Vertices[1].Y + " " + md.Domain.Vertices[2].X + " " + md.Domain.Vertices[2].Y + " " + md.Domain.Vertices[3].X + " " + md.Domain.Vertices[3].Y + " " + md.Domain.Vertices[0].X + " " + md.Domain.Vertices[0].Y); } SafePutPixel(newPoint[pp].X, newPoint[pp].Y, (int)newPoint[pp].Val , bit); prevPoint[pp] = newPoint[pp]; prevVal[pp] = (int)newPoint[pp].Val; } } } } } } Console.Out.WriteLine(goodPixel + " " + badPixel); bitmapWithGrid = (Bitmap)bit.Clone(); for (int i = 0; i < interpolationPoints.Count; i += 4) { for (int k = interpolationPoints[i + 1].Y; k < interpolationPoints[i].Y; k++) { bitmapWithGrid.SetPixel(interpolationPoints[i + 1].X, k, Color.Red); bitmapWithGrid.SetPixel(interpolationPoints[i + 2].X, k, Color.Red); } for (int k = interpolationPoints[i + 1].X; k < interpolationPoints[i + 2].X; k++) { bitmapWithGrid.SetPixel(k, interpolationPoints[i + 1].Y, Color.Red); bitmapWithGrid.SetPixel(k, interpolationPoints[i].Y, Color.Red); } } return bit; }
public void Compress() { int contErr = 0, divErr=0; do { double s = 0; double[] nh = new double[domains.Length]; Random rand = new Random(); while (squeue.Count != 0) { FractalCompression.Structure.Region r = squeue.Dequeue(); for (int i = 0; i < nh.Length; ++i) nh[i] = -1; for (int i = 0; i <= domains.GetUpperBound(0); i++) for (int j = 0; j <= domains.GetUpperBound(1); j++) { Domain dom = domains[i, j]; if (dom != null) { s = MNTools.ComputeContractivityFactor(dom, r, bitmap); //s = rand.NextDouble(); if (Math.Abs(s) >= 1) { //aqueue.Enqueue(-1); //cqueue.Enqueue(-1); contErr++; //Console.WriteLine("csErr: " + contErr++); continue; } divErr++; if(i!=domains.GetUpperBound(0) && j!=domains.GetUpperBound(1)) if (!POTools.CheckConditionOfContinuity(domains, i, j, a, r, bitmap)) continue; Point pk = dom.Vertices[1], pi = r.Vertices[1]; Mapper mapper = new Mapper(s, pk, pi, smallDelta, bigDelta, MNTools.GetBitmapValue(pk.X, pk.Y, bitmap), MNTools.GetBitmapValue(pk.X + bigDelta - 1, pk.Y, bitmap), MNTools.GetBitmapValue(pk.X, pk.Y + bigDelta - 1, bitmap), MNTools.GetBitmapValue(pk.X + bigDelta - 1, pk.Y + bigDelta - 1, bitmap), MNTools.GetBitmapValue(pi.X, pi.Y, bitmap), MNTools.GetBitmapValue(pi.X + r.Size, pi.Y, bitmap), MNTools.GetBitmapValue(pi.X , pi.Y + r.Size, bitmap), MNTools.GetBitmapValue(pi.X + r.Size, pi.Y + r.Size, bitmap), false); FractalCompression.Structure.Region mappedRegion = POTools.MapDomainToRegion(dom, r, bitmap, mapper, a); nh[(domains.GetUpperBound(0)+1) * i + j] = MNTools.ComputeDistance(mappedRegion, r, bitmap); //Console.WriteLine("nh[{0}]={1}", domains.GetUpperBound(0) * i + j, nh[domains.GetUpperBound(0) * i + j]); } } /*for (int j = 0; j < nh.Length; j++) Console.Write(nh[j] + " "); Console.WriteLine();*/ int minHj = 0; double minH = nh[0]; for (int j = 0; j < nh.Length; j++) { if (nh[j] >= 0) { minHj = j; minH = nh[j]; break; } } for (int j = minHj + 1; j < nh.Length; j++) { if (nh[j] < minH && nh[j] >= 0) { minH = nh[j]; minHj = j; } } //Console.WriteLine("minH = {0} - nh[{1}]", minH, minHj); if ((minH > eps || minH==-1) && d < dmax) { DivideRegion(r); aqueue.Enqueue(-1); cqueue.Enqueue(-1); //Console.WriteLine("divErr: " + divErr++); minHQueue.Add(minH); } else if (minH >= 0) //store j with the min distance inside aqueue and s inside cqueue { //Console.WriteLine("minHj: {0}, s: {1}", minHj, s); /*for (int j = 0; j < nh.Length; j++) Console.Write((int)nh[j] + " "); Console.WriteLine();*/ aqueue.Enqueue(minHj); cqueue.Enqueue(s); minHQueue.Add(minH); } // else // Console.WriteLine("uups"); } if (squeue2.Count != 0) { squeue = squeue2; d++; } } while (squeue.Count != 0); Console.WriteLine(contErr); Console.WriteLine(divErr); //this.optimizedAQueue = POTools.OptimizeAdressesList(aqueue, minHQueue, domains.Length - 1); }
public Bitmap DecompressImageVerII(out Bitmap bitmapWithGrid) { Bitmap bit = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Queue<MappedPoint>[] domainsPoints = PrepareLists(); Graphics.FromImage(bit).FillRectangle(Brushes.Green, 0, 0, this.width, this.height); //punkty interpolacyjne foreach (MappedPoint p in interpolationPoints) { bit.SetPixel(p.X, p.Y, Color.FromArgb((int)p.Val, (int)p.Val, (int)p.Val)); interPixel++; } //g³owna petla bool newPixel = true; int cos=0; Random rand = new Random(); while (newPixel) // while(cos<10) { newPixel = false; Console.Out.WriteLine("Iteration " + cos++); for (int i = 0; i < interpolationPoints.Count; i+=4) { int address = addresses[i / 4]; double s = contractivityFactors[i / 4]; // address = rand.Next(numberOfDomainInHeight * numberOfDomainInWidth); // s = rand.NextDouble(); if (address != -1) { MyDomain domain = FindDomainByAddress(address); Mapper mapper = new Mapper(s, domain.Domain.Vertices[1], interpolationPoints[i + 1], smallDelta, bigDelta, domain.Vals[0], domain.Vals[1], domain.Vals[2], domain.Vals[3], (int)interpolationPoints[i].Val, (int)interpolationPoints[i + 1].Val, (int)interpolationPoints[i + 2].Val, (int)interpolationPoints[i + 3].Val, false); int sizeOfQueue = domainsPoints[address].Count; for (int j = 0; j < sizeOfQueue; j++) { MappedPoint point = domainsPoints[address].Dequeue(); domainsPoints[address].Enqueue(point); MappedPoint newPoint = mapper.MapPoint(point.X, point.Y, point.Val); if (SafePutPixel((int)newPoint.X, (int)newPoint.Y, (int)newPoint.Val, bit)) { newPixel = true; int domainAddress = FindDomainByPoint(newPoint); domainsPoints[domainAddress].Enqueue(newPoint); } } } } } //tworzenie siatki bitmapWithGrid = (Bitmap)bit.Clone(); for (int i = 0; i < interpolationPoints.Count; i += 4) { for (int k = interpolationPoints[i + 1].Y; k < interpolationPoints[i].Y; k++) { bitmapWithGrid.SetPixel(interpolationPoints[i + 1].X, k, Color.Red); bitmapWithGrid.SetPixel(interpolationPoints[i + 2].X, k, Color.Red); } for (int k = interpolationPoints[i + 1].X; k < interpolationPoints[i + 2].X; k++) { bitmapWithGrid.SetPixel(k, interpolationPoints[i + 1].Y, Color.Red); bitmapWithGrid.SetPixel(k, interpolationPoints[i].Y, Color.Red); } } Console.Out.WriteLine(goodPixel + " " + badPixel+" "+interPixel); return bit; }
public static FractalCompression.Structure.Region MapDomainToRegion(Domain domain, FractalCompression.Structure.Region region, Bitmap bitmap, Mapper mapper, int a) { //int smallDelta = region.Size + 1; int bigDelta = domain.Size + 1; int smallDelta = bigDelta / a; int domainMaxX = domain.Vertices[3].X; Point regCorner = region.Vertices[1]; double[,] mappedVals = new double [smallDelta, smallDelta]; int x, y; for (int i = 0; i < bigDelta; i++) { y = domain.Vertices[1].Y + i; x = domain.Vertices[1].X; if (i == 0) { mappedVals[0, 0] = MNTools.GetBitmapValue(region.Vertices[1].X, region.Vertices[1].Y, bitmap); for (int j = 1; j < bigDelta; ++j) { x += a; if (x > domainMaxX) break; MappedPoint mp = mapper.MapPoint(x, y, MNTools.GetBitmapValue(x, y, bitmap)); mappedVals[mp.X - regCorner.X, mp.Y - regCorner.Y] = mp.Val; } mappedVals[0, smallDelta - 1] = MNTools.GetBitmapValue(region.Vertices[2].X, region.Vertices[2].Y, bitmap); } else if (i == bigDelta - 1) { mappedVals[0, smallDelta - 1] = MNTools.GetBitmapValue(region.Vertices[0].X, region.Vertices[0].Y, bitmap); for (int j = 1; j < bigDelta; ++j) { x += a; if (x > domainMaxX) break; MappedPoint mp = mapper.MapPoint(x, y, MNTools.GetBitmapValue(x, y, bitmap)); mappedVals[mp.X - regCorner.X, mp.Y - regCorner.Y] = mp.Val; } mappedVals[smallDelta - 1, smallDelta - 1] = MNTools.GetBitmapValue(region.Vertices[3].X, region.Vertices[3].Y, bitmap); } else { for (int j = 0; j < bigDelta; ++j) { x += a; if (x > domainMaxX) break; MappedPoint mp = mapper.MapPoint(x, y, MNTools.GetBitmapValue(x, y, bitmap)); mappedVals[mp.X - regCorner.X, mp.Y - regCorner.Y] = mp.Val; } } } FractalCompression.Structure.Region mappedRegion = new FractalCompression.Structure.Region(region.Vertices, mappedVals); return mappedRegion; }