示例#1
0
        static void Main(string[] args)
        {
            HybRunner runner = HybRunner.Cuda().SetDistrib(32, 32, 16, 16, 1, 0);

            GrayBitmap image = GrayBitmap.Load("../../images/lena512.bmp");
            uint       height = image.Height, width = image.Width;

            ushort[] inputPixels = image.PixelsUShort;

            float[] imageFloat   = new float[width * height];
            float[] imageCompute = new float[width * height];
            for (int i = 0; i < width * height; ++i)
            {
                imageFloat[i] = (float)inputPixels[i];
            }

            dynamic wrapper = runner.Wrap(new Program());

            wrapper.Sobel(imageFloat, imageCompute, (int)width, (int)height);

            ushort[] outputPixel = new ushort[width * height];
            for (int i = 0; i < width * height; ++i)
            {
                outputPixel[i] = (ushort)imageCompute[i];
            }

            GrayBitmap imageSobel = new GrayBitmap(width, height);

            imageSobel.PixelsUShort = outputPixel;
            imageSobel.Save("../../output-01-gpu/sobel.bmp");
        }
示例#2
0
        public static GrayBitmap Load(string filename)
        {
            using (BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open)))
            {
                byte[]   header = br.ReadBytes(54);
                GCHandle gch    = GCHandle.Alloc(header, GCHandleType.Pinned);

                // read headers
                FileHeader       fh  = (FileHeader)Marshal.PtrToStructure(Marshal.UnsafeAddrOfPinnedArrayElement(header, 0), typeof(FileHeader));
                BitmapInfoHeader bih = (BitmapInfoHeader)Marshal.PtrToStructure(Marshal.UnsafeAddrOfPinnedArrayElement(header, 14), typeof(BitmapInfoHeader));

                // read the palette
                br.ReadBytes((int)(bih.numberOfColorsInPalette * 4));

                //Console.Out.WriteLine("Loading bitmap file width = {0} - height = {1}", bih.width, bih.height) ;

                GrayBitmap res = new GrayBitmap(bih.width, bih.height);

                // read data - note that bmp starts with bottom-left corner
                for (int y = 0; y < res.height; ++y)
                {
                    br.Read(res.data, (int)res.width * ((int)res.height - 1 - y), (int)res.width);
                }
                return(res);
            }
        }
        static void Main(string[] args)
        {
            HybRunner runner = HybRunner.Cuda().SetDistrib(32, 32, 16, 16, 1, 0);

            GrayBitmap image = GrayBitmap.Load("../../images/lena512.bmp");
            uint       height = image.Height, width = image.Width;

            ushort[] inputPixels = image.PixelsUShort;

            float[] imageFloat   = new float[width * height];
            float[] imageCompute = new float[width * height];
            for (int i = 0; i < width * height; ++i)
            {
                imageFloat[i] = (float)inputPixels[i];
            }

            IntPtr src = runner.Marshaller.MarshalManagedToNative(imageFloat);

            //bind texture
            cudaChannelFormatDesc channelDescTex = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat);
            cudaArray_t           cuArrayTex     = TextureHelpers.CreateCudaArray(channelDescTex, src, (int)width, (int)height);
            cudaResourceDesc      resDescTex     = TextureHelpers.CreateCudaResourceDesc(cuArrayTex);

            //create Texture descriptor
            cudaTextureDesc texDesc = TextureHelpers.CreateCudaTextureDesc();

            //create Texture object
            cudaTextureObject_t texObj;

            cuda.CreateTextureObject(out texObj, ref resDescTex, ref texDesc);

            //create and bind surface

            dynamic wrapper = runner.Wrap(new Program());

            wrapper.Sobel(texObj, imageCompute, (int)width, (int)height);

            ushort[] outputPixel = new ushort[width * height];
            for (int i = 0; i < width * height; ++i)
            {
                outputPixel[i] = (ushort)imageCompute[i];
            }

            GrayBitmap imageSobel = new GrayBitmap(width, height);

            imageSobel.PixelsUShort = outputPixel;
            imageSobel.Save("../../output-03-surface/sobel.bmp");
        }
        static void Main(string[] args)
        {
            HybRunner runner = HybRunner.Cuda().SetDistrib(32, 32, 16, 16, 1, 0);

            GrayBitmap image = GrayBitmap.Load("../../images/lena512.bmp");
            uint       height = image.Height, width = image.Width;

            ushort[] inputPixels = image.PixelsUShort;

            float[] imageFloat = new float[width * height];

            for (int i = 0; i < width * height; ++i)
            {
                imageFloat[i] = (float)inputPixels[i];
            }

            IntPtr src = runner.Marshaller.MarshalManagedToNative(imageFloat);

            //bind texture
            cudaChannelFormatDesc channelDescTex = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat);
            cudaArray_t           cuArrayTex     = TextureHelpers.CreateCudaArray(channelDescTex, src, (int)width, (int)height);
            cudaResourceDesc      resDescTex     = TextureHelpers.CreateCudaResourceDesc(cuArrayTex);

            //create Texture descriptor
            cudaTextureDesc texDesc = TextureHelpers.CreateCudaTextureDesc();

            //create Texture object
            cudaTextureObject_t texObj;

            cuda.CreateTextureObject(out texObj, ref resDescTex, ref texDesc);

            //bind surface
            cudaChannelFormatDesc channelDescSurf = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat);
            cudaArray_t           cuArraySurf;

            cuda.MallocArray(out cuArraySurf, ref channelDescSurf, width, height, cudaMallocArrayFlags.cudaArraySurfaceLoadStore);

            //create cudaResourceDesc for surface
            cudaResourceDesc resDescSurf = TextureHelpers.CreateCudaResourceDesc(cuArraySurf);

            //create surface object
            cudaSurfaceObject_t surfObj;

            cuda.CreateSurfaceObject(out surfObj, ref resDescSurf);

            dynamic wrapper = runner.Wrap(new Program());

            wrapper.Sobel(texObj, surfObj, (int)width, (int)height);

            //pinned float array to allow the copy of the surface object on the host
            float[]  imageCompute = new float[width * height];
            GCHandle handle       = GCHandle.Alloc(imageCompute, GCHandleType.Pinned);
            IntPtr   dest         = handle.AddrOfPinnedObject();

            cuda.MemcpyFromArray(dest, cuArraySurf, 0, 0, width * height * sizeof(float), cudaMemcpyKind.cudaMemcpyDeviceToHost);

            ushort[] outputPixel = new ushort[width * height];
            for (int i = 0; i < width * height; ++i)
            {
                outputPixel[i] = (ushort)imageCompute[i];
            }

            GrayBitmap imageSobel = new GrayBitmap(width, height);

            imageSobel.PixelsUShort = outputPixel;
            imageSobel.Save("../../output-3-surface/sobel.bmp");
        }