unsafe public static Bitmap WskaźnikNaObraz(byte *ws, int x, int y)
        {
            int        lb = x * y;
            Bitmap     zw = new Bitmap(x, y);
            BitmapData bd = zw.LockBits(new Rectangle(0, 0, x, y), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

            RGB[] LisKol = new RGB[256];
            LisKol[0] = new RGB()
            {
                R = 0, G = 0, B = 0
            };
            for (byte i = 1; i != 0; i++)
            {
                LisKol[i] = new RGB()
                {
                    R = i, G = i, B = i
                };
            }
            int[] l = new int[256];
            for (int i = 0; i < y; i++)
            {
                RGB *tb = (RGB *)(byte *)(bd.Scan0 + i * bd.Stride);
                for (int j = 0; j < x; j++, tb++, ws++)
                {
                    *tb = LisKol[*ws];
                }
            }

            zw.UnlockBits(bd);
            return(zw);
        }
Exemple #2
0
        /// <summary>
        /// Convert a 32-bit ARGB image to AYUV
        /// </summary>
        /// <param name="dwWidthInPixels">Width to copy in pixels</param>
        /// <param name="dwHeightInPixels">Height to copy in pixels</param>
        /// <param name="dwDestStride">Stride of target in bytes</param>
        /// <param name="ipSrc">Pointer to source data</param>
        /// <param name="ipDest">Pointer to destination data</param>
        unsafe static public int ARGB32_To_AYUV(
            int dwWidthInPixels,
            int dwHeightInPixels,
            int dwDestStride,
            IntPtr ipSrc,
            IntPtr ipDest
            )
        {
            Debug.Assert(dwDestStride >= (dwWidthInPixels * Marshal.SizeOf(typeof(AYUV))));

            RGB * pSrcPixel  = (RGB *)ipSrc;
            AYUV *pDestPixel = (AYUV *)ipDest;

            for (int y = 0; y < dwHeightInPixels; y++)
            {
                for (int x = 0; x < dwWidthInPixels; x++)
                {
                    pDestPixel[x].V = (byte)(((112 * pSrcPixel->R - 94 * pSrcPixel->G - 18 * pSrcPixel->B + 128) >> 8) + 128);
                    pDestPixel[x].U = (byte)((((-38 * pSrcPixel->R - 74 * pSrcPixel->G + 112 * pSrcPixel->B + 128) >> 8) + 128));
                    pDestPixel[x].Y = (byte)((((66 * pSrcPixel->R + 129 * pSrcPixel->G + 25 * pSrcPixel->B + 128) >> 8) + 16));
                    pDestPixel[x].A = pSrcPixel->A;

                    pSrcPixel++;
                }
                pDestPixel = &pDestPixel[dwDestStride / sizeof(AYUV)];
            }

            return((int)((byte *)pDestPixel - (byte *)ipDest));
        }
Exemple #3
0
        // Return the point within a bitmap that matches 'target' the closest
        private Point FindTargetColour(Bitmap bitmap, RGB target, out int distance)
        {
            Rectangle  rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bm   = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
            Point      pt   = new Point(0, 0);

            distance = int.MaxValue;
            unsafe
            {
                //Bitmap test_bm = new Bitmap(rect.Width, rect.Height, bm.PixelFormat);
                RGB *px = (RGB *)bm.Scan0.ToPointer();
                for (int y = 0; y != bitmap.Height; ++y)
                {
                    RGB *col = px + y * bitmap.Width;
                    for (int x = 0; x != bitmap.Width; ++x, ++col)
                    {
                        //test_bm.SetPixel(x,y,Color.FromArgb(0xff, col->r, col->g, col->b));
                        int distsq = RGB.DistanceSq(target, *col);
                        if (distsq < distance)
                        {
                            pt.X     = x;
                            pt.Y     = y;
                            distance = distsq;
                        }
                    }
                }
                //test_bm.Save("D:/deleteme/screen_grap2.bmp");
            }
            bitmap.UnlockBits(bm);
            distance = (int)Math.Sqrt(distance);
            return(pt);
        }
        private unsafe Bitmap PobierzObraz(int x, int y, Bitmap b)
        {
            Wielkość = new Size(x, y);
            Bitmap     zwracana          = new Bitmap(x, y);
            BitmapData bd                = zwracana.LockBits(new Rectangle(0, 0, x, y), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
            RGB *      MiejsceZapisane   = (RGB *)bd.Scan0;
            PointF     MinimalnyXF       = XNYN.NaPointF();
            PointF     MaksymalnyYF      = XPYN.NaPointF();
            PointF     PrzesuniecieLewe  = XNYP.Odejmij(XNYN).NaPointF().Dziel(y);
            PointF     PrzesunieciePrawe = XPYP.Odejmij(XPYN).NaPointF().Dziel(y);

            for (int i = 0; i < y; i++)
            {
                PointF PunktPoStronieLewej  = MinimalnyXF.Dodaj(PrzesuniecieLewe.Razy(i));
                PointF PunktPoStroniePrawej = MaksymalnyYF.Dodaj(PrzesunieciePrawe.Razy(i));

                PointF PrzesuniecieMiedzyStronami = PunktPoStroniePrawej.Odejmij(PunktPoStronieLewej).Dziel(x);
                for (int j = 0; j < x; j++, MiejsceZapisane++)
                {
                    PointF p = PunktPoStronieLewej.Dodaj(PrzesuniecieMiedzyStronami.Razy(j));
                    *      MiejsceZapisane = b.Weź(p).NaRgb();
                }
            }
            zwracana.UnlockBits(bd);
            return(zwracana);
        }
Exemple #5
0
        /// <summary>
        /// Convert a 32-bit ARGB image to YUY2
        /// </summary>
        /// <param name="dwWidthInPixels">Width to copy in pixels</param>
        /// <param name="dwHeightInPixels">Height to copy in pixels</param>
        /// <param name="dwDestStride">Stride of target in bytes</param>
        /// <param name="ipSrc">Pointer to source data</param>
        /// <param name="ipDest">Pointer to destination data</param>
        unsafe static public int ARGB32_To_YUY2(
            int dwWidthInPixels,
            int dwHeightInPixels,
            int dwDestStride,
            IntPtr ipSrc,
            IntPtr ipDest
            )
        {
            Debug.Assert(dwDestStride >= (dwWidthInPixels * 2));

            RGB * pSrcPixel  = (RGB *)ipSrc;
            YUYV *pDestPixel = (YUYV *)ipDest;
            int   dwUseWidth = dwWidthInPixels / 2;

            for (int y = 0; y < dwHeightInPixels; y++)
            {
                for (int x = 0; x < dwUseWidth; x++)
                {
                    pDestPixel[x].Y = (byte)(((66 * pSrcPixel->R + 129 * pSrcPixel->G + 25 * pSrcPixel->B + 128) >> 8) + 16);
                    pDestPixel[x].U = (byte)(((-38 * pSrcPixel->R - 74 * pSrcPixel->G + 112 * pSrcPixel->B + 128) >> 8) + 128);

                    pSrcPixel++;

                    pDestPixel[x].Y2 = (byte)(((66 * pSrcPixel->R + 129 * pSrcPixel->G + 25 * pSrcPixel->B + 128) >> 8) + 16);
                    pDestPixel[x].V  = (byte)(((112 * pSrcPixel->R - 94 * pSrcPixel->G - 18 * pSrcPixel->B + 128) >> 8) + 128);

                    pSrcPixel++;
                }
                pDestPixel = &pDestPixel[dwDestStride / sizeof(YUYV)];
            }

            return((int)((byte *)pDestPixel - (byte *)ipDest));
        }
Exemple #6
0
        private static unsafe byte[] ToRGBFrom32Bpp(BitmapData imageData)
        {
            int width      = imageData.Width;
            int height     = imageData.Height;
            int destStride = width * sizeof(RGB);

            var dist = new byte[imageData.Width * imageData.Height * 3];

            ARGB *srcImgPtr = (ARGB *)imageData.Scan0;

            fixed(byte *destImgDataPinnedPtr = dist)
            {
                byte *destImageDataPtr = destImgDataPinnedPtr;

                Parallel.For(0, imageData.Height, y =>
                {
                    RGB *yDestRgbPtr = (RGB *)(destImageDataPtr + (destStride * y));
                    ARGB *ySrcRgbPtr = srcImgPtr + (width * y);

                    for (int x = 0; x < width; ++x)
                    {
                        ySrcRgbPtr++->ReverseTo(yDestRgbPtr++);
                    }
                });
            }

            return(dist);
        }
        private unsafe byte *PobierzObrazByte(int x, int y, Bitmap b)
        {
            //416 784
            Wielkość = new Size(x, y);
            byte *     zw                = (byte *)System.Runtime.InteropServices.Marshal.AllocHGlobal(x * y);
            byte *     zwk               = zw;
            BitmapData bd                = b.LockBits(new Rectangle(Point.Empty, b.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            byte *     początek          = (byte *)bd.Scan0;
            int        str               = bd.Stride;
            PointF     MinimalnyXF       = XNYN.NaPointF();
            PointF     MaksymalnyYF      = XPYN.NaPointF();
            PointF     PrzesuniecieLewe  = XNYP.Odejmij(XNYN).NaPointF().Dziel(y);
            PointF     PrzesunieciePrawe = XPYP.Odejmij(XPYN).NaPointF().Dziel(y);

            for (int i = 0; i < y; i++)
            {
                PointF PunktPoStronieLewej  = MinimalnyXF.Dodaj(PrzesuniecieLewe.Razy(i));
                PointF PunktPoStroniePrawej = MaksymalnyYF.Dodaj(PrzesunieciePrawe.Razy(i));

                PointF PrzesuniecieMiedzyStronami = PunktPoStroniePrawej.Odejmij(PunktPoStronieLewej).Dziel(x);

                float Px = PunktPoStronieLewej.X, Py = PunktPoStronieLewej.Y;
                float PrzesuniecieX = PrzesuniecieMiedzyStronami.X, PrzesuniecieY = PrzesuniecieMiedzyStronami.Y;
                for (int j = 0; j < x; j++, zw++)
                {
                    int X = (int)(PrzesuniecieX * j + Px);
                    int Y = (int)(Py + j * PrzesuniecieY);
                    if (X < 0 || Y < 0 || X >= b.Width || Y >= b.Height)
                    {
                        *zw = 0;
                        continue;
                    }
                    byte *tmp = początek + str * Y + X * 3;
                    RGB * r   = (RGB *)tmp;
                    int   s   = r->B + r->G + r->R;
                    s /= 3;
                    *zw = (byte)s;
                }
            }
            b.UnlockBits(bd);
            return(zwk);
        }
Exemple #8
0
        public static void WriteToPbm(string input, string output)
        {
            Gdi::Bitmap image = new Gdi::Bitmap(input);
            int         w     = image.Width;
            int         h     = image.Height;

            Gdi::Imaging.BitmapData data = image.LockBits(
                new Gdi::Rectangle(Gdi::Point.Empty, image.Size),
                Gdi::Imaging.ImageLockMode.ReadOnly,
                Gdi::Imaging.PixelFormat.Format24bppRgb);

            System.IO.Stream       str = System.IO.File.OpenWrite(output);
            System.IO.StreamWriter sw  = new System.IO.StreamWriter(str, System.Text.Encoding.ASCII);
            sw.WriteLine("P1");
            sw.WriteLine("{0} {1}", w, h);
            unsafe {
                int i = 0;
                for (int y = 0; y < h; y++)
                {
                    RGB *ppx  = (RGB *)((byte *)data.Scan0 + data.Stride * y);
                    RGB *ppxM = ppx + w;
                    while (ppx < ppxM)
                    {
                        sw.Write((ppx++)->Intensity() > 0x80?"0":"1");
                        if (++i % 64 == 0)
                        {
                            sw.WriteLine();
                        }
                        else
                        {
                            sw.Write(" ");
                        }
                    }
                }
            }
            sw.Close();
            str.Close();

            image.UnlockBits(data);
            image.Dispose();
        }
        public static unsafe byte *PrzeklorujMapkę(Bitmap b, int L2DoSklaliUjemnej, out Size Wielkość, int IlośćKolorów, int[] filtr)
        {
            int SzerokośćNowego = b.Width >> L2DoSklaliUjemnej;
            int WysokośćNowego  = b.Height >> L2DoSklaliUjemnej;
            int Skaler          = 1 << L2DoSklaliUjemnej;

            Wielkość = new Size(SzerokośćNowego, WysokośćNowego);
            IntPtr     ip        = Marshal.AllocHGlobal(SzerokośćNowego * WysokośćNowego);
            float      Rdzielnik = 0.299f * (IlośćKolorów - 1); Rdzielnik /= 255;
            float      GDzielnik = 0.587f * (IlośćKolorów - 1); GDzielnik /= 255;
            float      BDzielnik = 0.114f * (IlośćKolorów - 1); BDzielnik /= 255;
            float      OdwrotnośćIlościKolorów = 255f / (IlośćKolorów - 1);
            BitmapData bd = b.LockBits(new Rectangle(0, 0, b.Width & ~3, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            for (int j = 0; j < WysokośćNowego; j++)
            {
                for (int i = 0; i < SzerokośćNowego; i++)
                {
                    byte *  Mapa      = (byte *)ip + j * SzerokośćNowego + i;
                    short[] Histogram = new short[IlośćKolorów];//Uważaj na obszary wieksze od 16x16
                    for (int jx = 0; jx < Skaler; jx++)
                    {
                        RGB *KolorNaWejsciu = (RGB *)(((byte *)bd.Scan0) + bd.Stride * (Skaler * j));
                        KolorNaWejsciu += i * Skaler;
                        for (int ix = 0; ix < Skaler; ix++, KolorNaWejsciu++)
                        {
                            float f   = KolorNaWejsciu->R * Rdzielnik + KolorNaWejsciu->G * GDzielnik + BDzielnik * KolorNaWejsciu->B;
                            int   ind = Convert.ToInt32(f);
                            Histogram[ind]++;
                        }
                    }
                    int Wartość = Filtry.FiltrZnajdźNajwyszyPunkt(Histogram, filtr);
                    *   Mapa    = Convert.ToByte(Wartość * OdwrotnośćIlościKolorów);
                }
            }
            b.UnlockBits(bd);
            return((byte *)ip);
        }
        public static unsafe byte *ZmieńPoCzestotliwosciach(Bitmap b)
        {
            BitmapData br                  = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            byte *     Początek            = (byte *)System.Runtime.InteropServices.Marshal.AllocHGlobal(b.Width * b.Height);
            byte *     MIejscePrzeglądanie = Początek;
            int        strige              = br.Stride;

            for (int i = 0; i < b.Height; i++)
            {
                RGB *color = (RGB *)(br.Scan0 + strige * i);
                for (int j = 0; j < b.Width; j++, color++, MIejscePrzeglądanie++)
                {
                    double f = color->G * 0.5;
                    f += color->R * 1;
                    int c = color->B + color->R + color->G;
                    f /= c;
                    f *= 255;
                    byte K = (byte)f;
                    *    MIejscePrzeglądanie = K;
                }
            }
            b.UnlockBits(br);
            return(Początek);
        }
        public static byte *SkalujMax(this Bitmap b, Size sz)
        {
            Stopwatch s     = Stopwatch.StartNew();
            byte *    obraz = (byte *)Marshal.AllocHGlobal(sz.Width * sz.Height);

            OperacjeNaStrumieniu.Czyść((bool *)obraz, sz.Width * sz.Height);
            float XS = (float)(sz.Width - 1); XS /= b.Width - 1;
            float YS = (float)(sz.Height - 1); YS /= b.Height - 1;

            int[] PodmianyX;
            PodmianyX = TablicaSkalująca(b.Width, XS);
            BitmapData bc = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            for (int i = 0; i < b.Height; i++)
            {
                int  y  = Convert.ToInt32(i * YS);
                RGB *rl = (RGB *)((byte *)bc.Scan0 + (i * bc.Stride));

                byte *WskaźnikWyjściaLiniki = obraz + sz.Width * y;
                for (int j = 0; j < b.Width; j++, rl++)
                {
                    RGB r       = *rl;
                    int jasność = r.R + r.G + r.B;
                    jasność /= 3;
                    byte *wsWyjścia  = WskaźnikWyjściaLiniki + PodmianyX[j];
                    byte  jasnośćpix = *wsWyjścia;
                    if (jasność > (*wsWyjścia))
                    {
                        *wsWyjścia = (byte)jasność;
                    }
                }
            }
            Debug.WriteLine(s.ElapsedMilliseconds);
            b.UnlockBits(bc);
            return(obraz);
        }
 public unsafe void ReverseTo(RGB *value)
 {
     value->R = this.B;
     value->G = this.G;
     value->B = this.R;
 }
Exemple #13
0
        /// <summary>
        /// Convert a 32-bit ARGB image to NV12
        /// </summary>
        /// <param name="dwWidthInPixels">Width to copy in pixels</param>
        /// <param name="dwHeightInPixels">Height to copy in pixels</param>
        /// <param name="dwDestStride">Stride of target in bytes</param>
        /// <param name="ipSrc">Pointer to source data</param>
        /// <param name="ipDest">Pointer to destination data</param>
        unsafe static public int ARGB32_To_NV12(
            int dwWidthInPixels,
            int dwHeightInPixels,
            int dwDestStride,
            IntPtr ipSrc,
            IntPtr ipDest
            )
        {
            Debug.Assert(dwWidthInPixels % 2 == 0);
            Debug.Assert(dwHeightInPixels % 2 == 0);
            Debug.Assert(dwDestStride >= dwWidthInPixels);

            RGB * pSrcRow = (RGB *)ipSrc;
            byte *pDestY  = (byte *)ipDest;

            // Convert the Y plane.
            for (int y = 0; y < dwHeightInPixels; y++)
            {
                RGB *pSrcPixel = (RGB *)pSrcRow;

                for (int x = 0; x < dwWidthInPixels; x++)
                {
                    // Y0
                    pDestY[x] = (byte)(((66 * pSrcPixel[x].R + 129 * pSrcPixel[x].G + 25 * pSrcPixel[x].B + 128) >> 8) + 16);
                }
                pDestY  += dwDestStride;
                pSrcRow += dwWidthInPixels;
            }

            // Calculate the offsets for the V and U planes.
            // In NV12, each chroma plane has equal stride and half the height as the Y plane.
            byte *pDestUV = (byte *)ipDest;

            pDestUV += (dwDestStride * dwHeightInPixels);

            // Convert the V and U planes.
            // NV12 is a 4:2:0 format, so each chroma sample is derived from four RGB pixels.
            // The chroma samples are packed in one plane (U,V)
            pSrcRow = (RGB *)ipSrc;
            for (int y = 0; y < dwHeightInPixels; y += 2)
            {
                RGB *pSrcPixel   = (RGB *)pSrcRow;
                RGB *pNextSrcRow = (RGB *)(pSrcRow + dwWidthInPixels);

                byte *pbUV = pDestUV;

                for (int x = 0; x < dwWidthInPixels; x += 2)
                {
                    // Use a simple average to downsample the chroma.

                    // U
                    *pbUV++ = (byte)((
                                         (byte)(((-38 * pSrcPixel[x].R - 74 * pSrcPixel[x].G + 112 * pSrcPixel[x].B + 128) >> 8) + 128) +
                                         (byte)(((-38 * pSrcPixel[x + 1].R - 74 * pSrcPixel[x + 1].G + 112 * pSrcPixel[x + 1].B + 128) >> 8) + 128) +
                                         (byte)(((-38 * pNextSrcRow[x].R - 74 * pNextSrcRow[x].G + 112 * pNextSrcRow[x].B + 128) >> 8) + 128) +
                                         (byte)(((-38 * pNextSrcRow[x + 1].R - 74 * pNextSrcRow[x + 1].G + 112 * pNextSrcRow[x + 1].B + 128) >> 8) + 128)
                                         ) / 4);

                    // V
                    *pbUV++ = (byte)((
                                         (byte)(((112 * pSrcPixel[x].R - 94 * pSrcPixel[x].G - 18 * pSrcPixel[x].B + 128) >> 8) + 128) +
                                         (byte)(((112 * pSrcPixel[x + 1].R - 94 * pSrcPixel[x + 1].G - 18 * pSrcPixel[x + 1].B + 128) >> 8) + 128) +
                                         (byte)(((112 * pNextSrcRow[x].R - 94 * pNextSrcRow[x].G - 18 * pNextSrcRow[x].B + 128) >> 8) + 128) +
                                         (byte)(((112 * pNextSrcRow[x + 1].R - 94 * pNextSrcRow[x + 1].G - 18 * pNextSrcRow[x + 1].B + 128) >> 8) + 128)
                                         ) / 4);
                }
                pDestUV += dwDestStride;

                // Skip two lines on the source image.
                pSrcRow += (dwWidthInPixels * 2);
            }

            return((int)(pDestUV - (byte *)ipDest));
        }
Exemple #14
0
        public static int CountPixelbyPointer(Bitmap image) //Pointer를 이용한 코드
        {
            int ImageIndex = 0;
            int PixelCount = 0;
            RGB ImageRGB;

            HongLine.Clear();
            HongPoint1.Clear();
            HongPoint2.Clear();
            HongPoint3.Clear();
            HongPoint4.Clear();
            HongPoint5.Clear();
            HongPoint6.Clear();
            HongPoint7.Clear();
            HongPoint8.Clear();
            unsafe
            {
                // unsafe한 Pointer를 이용하여 각각의 값을 가져옴
                RGB *RGBImage = (RGB *)image.LockMemory(ImageLockMode.ReadOnly).ToPointer();
                for (int X = 250; X <= 950; X += 100)
                {
                    for (int Y = 100; Y < image.Height - 100; Y++)
                    {
                        ImageIndex = Y * image.Width + X;
                        ImageRGB   = RGBImage[ImageIndex];

                        // if (IsSameColor(selectedColor,ImageRGB) == true)
                        if (ImageRGB.B >= Xacel && X == 250)
                        {
                            PixelCount++;
                            HongPoint1.Add(Y);
                        }
                        if (ImageRGB.B >= Xacel && X == 350)
                        {
                            PixelCount++;
                            HongPoint2.Add(Y);
                        }
                        if (ImageRGB.B >= Xacel && X == 450)
                        {
                            PixelCount++;
                            HongPoint3.Add(Y);
                        }
                        if (ImageRGB.B >= Xacel && X == 550)
                        {
                            PixelCount++;
                            HongPoint4.Add(Y);
                        }
                        if (ImageRGB.B >= Xacel && X == 650)
                        {
                            PixelCount++;
                            HongPoint5.Add(Y);
                        }
                        if (ImageRGB.B >= Xacel && X == 750)
                        {
                            PixelCount++;
                            HongPoint6.Add(Y);
                        }
                        if (ImageRGB.B >= Xacel && X == 850)
                        {
                            PixelCount++;
                            HongPoint7.Add(Y);
                        }
                        if (ImageRGB.B >= Xacel && X == 950)
                        {
                            PixelCount++;
                            HongPoint8.Add(Y);
                        }
                    }
                }
            }
            image.UnLockMemory();
            image.Dispose();
            return(PixelCount);
        }