Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,I have written code like this:

C#
private static unsafe Point[] Hproject(Bitmap bmp)
       {
           int height = bmp.Height;
           int width = bmp.Width;
           Point[] result = new Point[height];

           var data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
           var startPtr = (PixelColor*)data.Scan0.ToPointer();

           Parallel.ForEach(Partitioner.Create(0, height), (h) =>
           {
               var ptr = startPtr + h.Item1 * width;

               int lBlackNumber = new int();
               for (int y = h.Item1; y < h.Item2; y++)
               {
                   lBlackNumber = 0;
                   for (int x = 0; x < width; x++)
                   {
                       var c = *ptr;
                       if (c.Blue == 255)
                       {
                           lBlackNumber++;
                       }

                       ptr++;
                   }
                   result[y].X = lBlackNumber;
               }

           });
           bmp.UnlockBits(data);
           return result;
       }

it work well.but I find that it's hard to written code in bitmap's height direction.can you help me?
I write one,but it's bad.
C#
private static unsafe Point[] VProjectByParallelForEach(Bitmap bmp)
        {
            int height = bmp.Height;
            int width = bmp.Width;
            Point[] result = new Point[height];
            var data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            var startPtr = (PixelColor*)data.Scan0.ToPointer();
            result = VProjectParallelForEach(startPtr, width, height);
            bmp.UnlockBits(data);

            return result;
        }
        private static unsafe Point[] VProjectParallelForEach(PixelColor* startPtr, int width, int height)
        {
            Point[] result = new Point[width];
            Parallel.ForEach(Partitioner.Create(0, width), (w) =>
            {
                var ptr = startPtr + w.Item1 * height;

                int lBlackNumber = new int();
                for (int x = w.Item1; x < w.Item2; x++)
                {
                    lBlackNumber = 0;
                    for (int y = 0; y < height; y++)
                    {
                        var c = *ptr;
                        if (c.Blue == 255)
                        {
                            lBlackNumber++;
                        }

                        ptr++;
                    }
                    result[x].X = lBlackNumber;
                }

            });
            return result;
        }
Posted
Updated 4-Nov-12 15:02pm
v2
Comments
Andreas Gieriet 4-Nov-12 21:16pm    
You have to re-phrase your question.
It is not clear what you want to achieve.
What means "is bad"? Wrong results, crash, dead-lock, ...?
The hurdle to try the code out on our side is a bit high (need to create a project, get some test data, etc.).

By comparing the code blocks I see that you do several things at once. Do one change after the other and try each change individually.
1) Changing from PixelFormat.Format24bppRgb to PixelFormat.Format32bppArgb.
2) Refactor into a separate method: VProjectParallelForEach (why the heck do you use overloading without cause?).
3) Swapping of processing: from row x columns to columns x rows (why?).

Cheers
Andi

1 solution

Is anyone help me to check the upper code?
 
Share this answer
 
Comments
Andreas Gieriet 4-Nov-12 21:04pm    
Please remove this "solution" - a question is not a solution.
Use: [Have a Question or Comment?] button instead.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900