示例#1
0
        private void GrayFilterThread(object parameter)
        {
            GrayFilterThreadParameter input = parameter as GrayFilterThreadParameter;

            if (input == null)
            {
                throw new ArgumentException("Неправильный формат параметра потока");
            }

            var grayResult = input.Result;
            var data       = input.Data;
            int delta      = input.Stride / Width;

            for (int y = input.From; y < input.To; ++y)
            {
                int offset = y * input.Stride;
                for (int x = 0; x < Width; ++x)
                {
                    int  index = offset + x * delta;
                    byte color = (byte)((data[index] + data[index + 1] + data[index + 2]) / 3);
                    grayResult[x, y] = color;
                }
            }
            input.ResetEvent.Set();
        }
示例#2
0
        private byte[,] GrayFilter(byte[] data, int stride)
        {
            var grayResult       = new byte[width, height];
            int threadCount      = Environment.ProcessorCount;
            int threadPart       = height / threadCount;
            var resetEvents      = new ManualResetEvent[threadCount];
            var threadParameters = new GrayFilterThreadParameter[threadCount];

            for (int i = 0; i < threadCount; ++i)
            {
                resetEvents[i]      = new ManualResetEvent(false);
                threadParameters[i] = new GrayFilterThreadParameter(
                    data, grayResult, stride,
                    threadPart * i,
                    ((i + 1) < threadCount) ? (threadPart * (i + 1)) : (height),
                    resetEvents[i]);
                ThreadPool.QueueUserWorkItem(new WaitCallback(GrayFilterThread), threadParameters[i]);
            }
            WaitHandle.WaitAll(resetEvents);
            return(grayResult);
        }
示例#3
0
 private byte[,] GrayFilter(byte[] data, int stride)
 {
     var grayResult = new byte[width, height];
     int threadCount = Environment.ProcessorCount;
     int threadPart = height / threadCount;
     var resetEvents = new ManualResetEvent[threadCount];
     var threadParameters = new GrayFilterThreadParameter[threadCount];
     for (int i = 0; i < threadCount; ++i)
     {
         resetEvents[i] = new ManualResetEvent(false);
         threadParameters[i] = new GrayFilterThreadParameter(
             data, grayResult, stride,
             threadPart * i,
             ((i + 1) < threadCount) ? (threadPart * (i + 1)) : (height),
             resetEvents[i]);
         ThreadPool.QueueUserWorkItem(new WaitCallback(GrayFilterThread), threadParameters[i]);
     }
     WaitHandle.WaitAll(resetEvents);
     return grayResult;
 }