6 #include "IndiceTools.h"
10 #include "ConvolutionDevice.h"
12 // Le kernel de la convolution en constant memory.
13 __constant__ float TAB_KERNEL[KERNEL_SIZE][KERNEL_SIZE];
15 // L'image source accédée comme une texture.
16 texture<uchar4, 2, cudaReadModeElementType> textureImageSource;
18 ConstantMemoryLink constantMemoryKernelLink()
21 size_t sizeAll = KERNEL_SIZE * KERNEL_SIZE * sizeof(float);
22 HANDLE_ERROR(cudaGetSymbolAddress((void**)&ptrDevTabData, TAB_KERNEL));
23 ConstantMemoryLink cmLink =
25 (void**)ptrDevTabData, KERNEL_SIZE * KERNEL_SIZE, sizeAll
30 void bindSouceAsTexture(uchar4* source, int w, int h)
32 // Propriétés de la texture (image en entrée).
33 textureImageSource.addressMode[0] = cudaAddressModeClamp;
34 textureImageSource.addressMode[1] = cudaAddressModeClamp;
35 textureImageSource.filterMode = cudaFilterModePoint;
36 textureImageSource.normalized = false;
38 const size_t pitch = w * sizeof(uchar4); // Taille d'une ligne.
39 cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<uchar4>();
40 HANDLE_ERROR(cudaBindTexture2D(NULL, textureImageSource, source, channelDesc, w, h, pitch));
43 void unbindSouceAsTexture()
45 HANDLE_ERROR(cudaUnbindTexture(textureImageSource));
49 void toGrayscale(uchar4* ptrDevPixels, int w, int h)
51 const int TID = Indice2D::tid();
52 const int NB_THREAD = Indice2D::nbThread();
60 IndiceTools::toIJ(s, w, &pixelI, &pixelJ);
62 const uchar average = (uchar)(((int)ptrDevPixels[s].x + (int)ptrDevPixels[s].y + (int)ptrDevPixels[s].z) / 3);
63 ptrDevPixels[s].x = average;
64 ptrDevPixels[s].y = average;
65 ptrDevPixels[s].z = average;
72 void convolution(uchar4* ptrDevOutput, int w, int h)
74 const int TID = Indice2D::tid();
75 const int NB_THREAD = Indice2D::nbThread();
83 IndiceTools::toIJ(s, w, &pixelI, &pixelJ); // update (pixelI, pixelJ)
85 // La somme des produits du kernel avec l'image source.
87 for (int i = -4; i <= 4; i++)
88 for (int j = -4; j <= 4; j++)
90 uchar4 valueSource = tex2D(textureImageSource, pixelJ + j, pixelI + i);
91 sum += (float)valueSource.x * TAB_KERNEL[i + 4][j + 4]; // Comme c'est une image en niveau de gris on ne prend qu'une composante.
94 sum /= 100; // Comme défini dans la donnée.
96 const float finalValue = (uchar)(sum * 50);
98 ptrDevOutput[s].x = finalValue;
99 ptrDevOutput[s].y = finalValue;
100 ptrDevOutput[s].z = finalValue;