Exemplo n.º 1
0
        protected virtual void ThumbProcessing()
        {
            double            ld = Math.Log(2);
            long              index = 0;
            ColorRGB          crgb = new ColorRGB(new RGBColorspacesRGB());
            ColorLab          cl = new ColorLab();
            ConversionRoutine routLab = ColorConverter.FindRoutine(crgb, cl);
            ConversionRoutine routRGB = ColorConverter.FindRoutine(cl, crgb);
            BitmapEx          bmp, bmpE;

            using (ColorConverter Converter = new ColorConverter())
            {
                for (int i = 0; i < Frames.Count; i++)
                {
                    double exposure = Math.Log(Frames[i].NewBrightness / Frames[i].AlternativeBrightness, 2);
                    bmp  = GetThumb(i, false);
                    bmpE = GetThumbEdited(i, false);

                    unsafe
                    {
                        bmp.LockBits();
                        bmpE.LockBits();
                        byte *pix1 = (byte *)bmp.Scan0;
                        byte *pix2 = (byte *)bmpE.Scan0;

                        for (uint y = 0; y < bmp.Height; y++)
                        {
                            for (uint x = 0; x < bmp.Stride; x += bmp.ChannelCount)
                            {
                                index = y * bmp.Stride + x;

                                //LTODO: doesn't calculate correctly
                                crgb.R = pix1[index] / 255d; crgb.G = pix1[index + 1] / 255d; crgb.B = pix1[index + 2] / 255d;
                                Converter.Convert(crgb, cl, routLab);
                                if (exposure < 0)
                                {
                                    cl.L = (cl.L < 0.94) ? cl.L : Math.Exp(exposure * ld) * cl.L;
                                }
                                else if (exposure > 0)
                                {
                                    cl.L = (cl.L > 0.06) ? cl.L : Math.Exp(exposure * ld) * cl.L;
                                }
                                Converter.Convert(cl, crgb, routRGB);

                                pix2[index]     = (byte)(crgb.R * byte.MaxValue);
                                pix2[index + 1] = (byte)(crgb.G * byte.MaxValue);
                                pix2[index + 2] = (byte)(crgb.B * byte.MaxValue);
                            }
                        }
                        bmp.UnlockBits();
                        bmpE.UnlockBits();
                    }

                    SetThumbEdited(i, bmpE);
                    MainWorker.ReportProgress(0, new ProgressChangeEventArgs(i * 100 / (Frames.Count - 1), ProgressType.ProcessingThumbs));
                }
            }
        }
Exemplo n.º 2
0
        private void BrCalc_Lab()
        {
            BitmapEx  bmp1, bmp2, bmp3;
            const int min = 5;
            const int max = 95;

            bmp1 = GetThumb(0, true);

            const uint ThumbWidth  = 300;
            const uint ThumbHeight = 200;
            uint       rowstride;
            int        n = bmp1.ChannelCount;

            long            index = 0;
            uint            x, y;
            int             x1, y1, maxCol = 8;
            double          fact, d1, d2;
            List <double[]> PixelBrightness;

            bool[,] NonUseMask = new bool[ThumbWidth, ThumbHeight];

            using (ColorConverter Converter = new ColorConverter())
            {
                ColorLab[,] labimg1 = new ColorLab[ThumbWidth, ThumbHeight];
                ColorLab[,] labimg2 = new ColorLab[ThumbWidth, ThumbHeight];
                ColorLab[,] labimg3 = new ColorLab[ThumbWidth, ThumbHeight];
                ColorRGB          colrgb = new ColorRGB(new RGBColorspacesRGB());
                ConversionRoutine rout   = ColorConverter.FindRoutine(colrgb, labimg1[0, 0]);

                for (int f = 1; f < Frames.Count; f += 2)
                {
                    if (MainWorker.CancellationPending)
                    {
                        return;
                    }

                    if (f + 2 >= Frames.Count)
                    {
                        f = Frames.Count - 2;
                    }

                    bmp1      = GetThumb(f - 1, false).Scale(ThumbWidth, ThumbHeight);
                    bmp2      = GetThumb(f, false).Scale(ThumbWidth, ThumbHeight);
                    bmp3      = GetThumb(f + 1, false).Scale(ThumbWidth, ThumbHeight);
                    rowstride = bmp1.Stride;

                    unsafe
                    {
                        bmp1.LockBits();
                        bmp2.LockBits();
                        bmp3.LockBits();

                        byte *pix1 = (byte *)bmp1.Scan0;
                        byte *pix2 = (byte *)bmp2.Scan0;
                        byte *pix3 = (byte *)bmp3.Scan0;

                        //Non use Mask and Lab conversion
                        for (y = 0; y < ThumbHeight; y++)
                        {
                            for (x = 0; x < ThumbWidth; x++)
                            {
                                index = y * rowstride + (x * n);

                                colrgb.R = pix1[index];
                                colrgb.G = pix1[index + 1];
                                colrgb.B = pix1[index + 2];
                                Converter.Convert(colrgb, labimg1[x, y], rout);
                                colrgb.R = pix2[index];
                                colrgb.G = pix2[index + 1];
                                colrgb.B = pix2[index + 2];
                                Converter.Convert(colrgb, labimg2[x, y], rout);
                                colrgb.R = pix3[index];
                                colrgb.G = pix3[index + 1];
                                colrgb.B = pix3[index + 2];
                                Converter.Convert(colrgb, labimg3[x, y], rout);


                                if (labimg1[x, y].L <min || labimg1[x, y].L> max || labimg2[x, y].L <min || labimg2[x, y].L> max || labimg3[x, y].L <min || labimg3[x, y].L> max ||
                                    Math.Abs(labimg2[x, y].a - labimg1[x, y].a) > maxCol || Math.Abs(labimg3[x, y].a - labimg2[x, y].a) > maxCol ||
                                    Math.Abs(labimg2[x, y].b - labimg1[x, y].b) > maxCol || Math.Abs(labimg3[x, y].b - labimg2[x, y].b) > maxCol)
                                {
                                    NonUseMask[x, y] = true;
                                }
                            }
                        }
                        bmp1.UnlockBits();
                        bmp2.UnlockBits();
                        bmp3.UnlockBits();
                    }

                    PixelBrightness = new List <double[]>();

                    //Brightness calculation
                    for (y = 0; y < ThumbHeight; y++)
                    {
                        for (x = 0; x < ThumbWidth; x++)
                        {
                            if (!NonUseMask[x, y])
                            {
                                fact = 0;
                                if (y > 0 && x > 0 && y < ThumbHeight - 1 && x < ThumbWidth - 1)
                                {
                                    d1 = 0;
                                    d2 = 0;
                                    for (y1 = -1; y1 <= 1; y1++)
                                    {
                                        for (x1 = -1; x1 <= 1; x1++)
                                        {
                                            d1 += Math.Abs(labimg1[x, y].L - labimg2[x, y].L);
                                            d2 += Math.Abs(labimg2[x, y].L - labimg3[x, y].L);
                                        }
                                    }
                                    fact = Math.Max(d1 / 9d, d2 / 9d);
                                }
                                if (fact > 0.2)
                                {
                                    PixelBrightness.Add(new double[] { fact *Math.Log(labimg1[x, y].L), fact * Math.Log(labimg2[x, y].L), fact * Math.Log(labimg3[x, y].L) });
                                }
                            }
                        }
                    }

                    Frames[f - 1].OriginalBrightness = PixelBrightness.Average(p => p[0]);
                    Frames[f].OriginalBrightness     = PixelBrightness.Average(p => p[1]);
                    Frames[f + 1].OriginalBrightness = PixelBrightness.Average(p => p[2]);

                    Frames[f - 1].AlternativeBrightness = Frames[f - 1].OriginalBrightness;
                    Frames[f].AlternativeBrightness     = Frames[f].OriginalBrightness;
                    Frames[f + 1].AlternativeBrightness = Frames[f + 1].OriginalBrightness;

                    Frames[f - 1].NewBrightness = Frames[f - 1].OriginalBrightness;
                    Frames[f].NewBrightness     = Frames[f].OriginalBrightness;
                    Frames[f + 1].NewBrightness = Frames[f + 1].OriginalBrightness;

                    MainWorker.ReportProgress(0, new ProgressChangeEventArgs(f * 100 / (Frames.Count - 1), ProgressType.CalculateBrightness));
                }
            }
        }