Archive for the ‘CUDA’ Category

#define PADBYTES 4
int Interpolation(BYTE* pbySrc, BYTE* pbyDest, int iSrcWidth, int iSrcHeight, int iSrcBPP, int iDestWidth, int iDestHeight)
{  
 //process each line
 for (int iRow = 0; iRow < iDestHeight; iRow++)
 {
 double ay = (double) iRow * iSrcHeight / iDestHeight;    //scaled source iRow
 int     y = (int)ay;                                    //truncates source iRow
 ay = ay - y;                                        //distance as fractional part of the scaled pixel, used for weighting

 //process each pixel within the line
 for (int iCol = 0; iCol < iDestWidth; iCol++)
 {
 double R, G, B;

 double ax = (double)iCol * iSrcWidth / iDestWidth;    //scaled source column
 int     x = (int)ax;                                //truncates source column
 ax = ax - x;                                    //distance as fractional part of the scaled pixel, used for weighting

 if ((x < iSrcWidth - 1) && (y < iSrcHeight - 1))
 {
 //interpolate each channel
 R = (1.0 - ax) * (1.0 - ay) * GetValueR(x+0, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 ax  * (1.0 - ay) * GetValueR(x+1, y+0, iSrcWidth, iSrcHeight, pbySrc) +
 (1.0 - ax) *        ay  * GetValueR(x+0, y+1, iSrcWidth, iSrcHeight, pbySrc) +
 ax  *        ay  * GetValueR(x+1, y+1, iSrcWidth, iSrcHeight, pbySrc);
 
 G = (1.0 - ax) * (1.0 - ay) * GetValueG(x+0, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 ax  * (1.0 - ay) * GetValueG(x+1, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 (1.0 - ax) *        ay  * GetValueG(x+0, y+1, iSrcWidth, iSrcHeight, pbySrc) +
 ax  *        ay  * GetValueG(x+1, y+1, iSrcWidth, iSrcHeight, pbySrc);

 B = (1.0 - ax) * (1.0 - ay) * GetValueB(x+0, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 ax  * (1.0 - ay) * GetValueB(x+1, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 (1.0 - ax) *        ay  * GetValueB(x+0, y+1, iSrcWidth, iSrcHeight, pbySrc) +
 ax  *        ay  * GetValueB(x+1, y+1, iSrcWidth, iSrcHeight, pbySrc);
 }
 else
 {
 //set to zero to blank these lines instead of copying source
 R = pbySrc[y * iSrcWidth * iSrcBPP + x * iSrcBPP + 0];
 G = pbySrc[y * iSrcWidth * iSrcBPP + x * iSrcBPP + 1];
 B = pbySrc[y * iSrcWidth * iSrcBPP + x * iSrcBPP + 2];
 }

 //write to destination buffer, change order for BGR
 *pbyDest++ = (BYTE)R;
 *pbyDest++ = (BYTE)G;
 *pbyDest++ = (BYTE)B;
 }

 int iPad = (iDestWidth * 3) % PADBYTES ? PADBYTES - (iDestWidth * 3) % PADBYTES : 0;
 for (int idx = 0; idx < iPad; idx++)
 {
 *pbyDest++ = 0;
 }
 }
 return 0;
}

pbySrc      -> Source Buffer
pbyDest     -> Destination Buffer
iSrcWidth   -> Source Image Width
iSrcHeight  -> Source Image Height
iSrcBPP     -> Bytes per pixel od the source Image
iDestWidth  -> New width of the image
iDestHeight -> New Height of the Image

NB: The memory allocation for the new image should be allocated. the Bytes per pixel of the new image is 3. For the other function used here please refer the previous posts.

int SetSharpnessR(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dbSharpnessR)
{
 int MaskMatrix[3][3] ={{1,  1,  1}, {1, -8,  1}, {1,  1,  1} };
 double dbPixelValueR = 0.0;
 int i, j, iRed;

 for (j = -1; j <2 ; j++)
 {
 for (i = -1; i <2 ; i++)
 {
 dbPixelValueR += MaskMatrix[j+1][i+1] * GetValueR(iX+j, iY+i, iWidth, iHeight, iBPP, pbySrc);
 }
 }
 iRed = (int)(GetValueR(iX, iY, iWidth, iHeight, iBPP, pbySrc) - (dbSharpnessR * dbPixelValueR));
 return Clip(iRed);
}

int SetSharpnessG(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dbSharpnessG)
{
 int MaskMatrix[3][3] ={{1,  1,  1}, {1, -8,  1}, {1,  1,  1} };
 double dbPixelValueG = 0.0;
 int i, j, iGreen;

 for (j = -1; j <2 ; j++)
 {
 for (i = -1; i <2 ; i++)
 {
 dbPixelValueG += MaskMatrix[j+1][i+1] * GetValueG(iX+j, iY+i, iWidth, iHeight, iBPP, pbySrc);
 }
 }
 iGreen = (int)(GetValueG(iX, iY, iWidth, iHeight, iBPP, pbySrc) - (dbSharpnessG * dbPixelValueG));
 return Clip(iGreen);
}

int SetSharpnessB(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dbSharpnessB)
{
 int MaskMatrix[3][3] ={{1,  1,  1}, {1, -8,  1}, {1,  1,  1} };
 double dbPixelValueB = 0.0;
 int i, j, iBlue;

 dbPixelValueB = 0.0;

 for (j = -1; j <2 ; j++)
 {
 for (i = -1; i <2 ; i++)
 {
 dbPixelValueB += MaskMatrix[j+1][i+1] * GetValueB(iX+j, iY+i, iWidth, iHeight, iBPP, pbySrc);
 }
 }
 iBlue = (int)(GetValueB(iX, iY, iWidth, iHeight, iBPP, pbySrc) - (dbSharpnessB * dbPixelValueB));
 return Clip(iBlue);
}

iX                       -> x cordinate of the Pixel
iY                       -> y cordinate of the Pixel
iWidth               -> Width of the Image
iHeight              -> Height of the Image
pbySrc              -> Pointer to the Buffer where image is readed
iBpp                   -> Bytes per pixel
dbSharpnessR  -> New Sharpness value of Red Component of the Pixel
dbSharpnessG  -> New Sharpness value of Green Component of the Pixel
dbSharpnessB  -> New Sharpness value of Blue Component of the Pixel

The Sharpness values should be in the range ie,  iBrightValR, iBrightValG, iBrightValB should be in 0.0 to 0.5
The default value is 0.0.

You can call these functions to the entire pixels of the image for the Sharpness change of the image

NB: For the other functions given here, please refer the previous posts

int ContrastR(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dContrastValR)
{
 int iRed = GetValueR(iX, iY, iWidth, iHeight, iBPP, pbySrc);
 iRed = (int)floor((iRed - GREY) * dContrastValR) + GREY;
 return Clip(iRed);
}

int ContrastG(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dContrastValG)
{
 int iGreen = GetValueG(iX, iY, iWidth, iHeight, iBPP, pbySrc);
 iGreen = (int)floor((iGreen - GREY) * dContrastValG) + GREY;
 return Clip(iGreen);
}

int ContrastB(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dContrastValB)
{
 int iBlue = GetValueB(iX, iY, iWidth, iHeight, iBPP, pbySrc);
 iBlue = (int)floor((iBlue - GREY) * dContrastValB) + GREY;
 return Clip(iBlue);
}

iX                        -> x cordinate of the Pixel
iY                        -> y cordinate of the Pixel
iWidth                -> Width of the Image
iHeight               -> Height of the Image
pbySrc               -> Pointer to the Buffer where image is readed
iBpp                    -> Bytes per pixel
dContrastValR  -> New Contrast value of Red Component of the Pixel
dContrastValG  -> New Contrast value of Green Component of the Pixel
dContrastValB  -> New Contrast value of Blue Component of the Pixel

The Contrast values should be in the range ie,  iBrightValR, iBrightValG, iBrightValB should be in 0.0 to 4.0
The default value is 1.0.

You can call these functions to the entire pixels of the image for the Contrast change of the image

NB: For the other functions given here, please refer the previous posts

int BrightnessR(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE* pbySrc, int iBrightValR)
{
 return Clip(GetValueR(iX, iY, iWidth, iHeight, iBPP, pbySrc) + iBrightValR);
}

int BrightnessG(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, int iBrightValG)
{
 return Clip(GetValueG(iX, iY, iWidth, iHeight, iBPP, pbySrc) + iBrightValG);
}

int BrightnessB(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, int iBrightValB)
{
 return Clip(GetValueB(iX, iY, iWidth, iHeight, iBPP, pbySrc) + iBrightValB);
}

iX                   -> x cordinate of the Pixel
iY                   -> y cordinate of the Pixel
iWidth           -> Width of the Image
iHeight          -> Height of the Image
pbySrc          -> Pointer to the Buffer where image is readed
iBpp               -> Bytes per pixel
iBrightValR   -> New Brightness value of Red Component of the Pixel
iBrightValG   -> New Brightness value of Green Component of the Pixel
iBrightValB   -> New Brightness value of Blue Component of the Pixel

The Brightness values should be in the range ie,  iBrightValR, iBrightValG, iBrightValB should be in -128 to +128.
The default value is 0.

You can call these functions to the entire pixels of the image for the Brightness change of the image

NB: For the other functions given here, please refer the previous posts

#define MAXVAL          255.0
#define GAMMACONSTANT   1
int SetGammaR(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dbGammaR)
{
 double dbR = (double)(GetValueR(iX, iY, iWidth, iHeight, iBPP, pbySrc) / MAXVAL);
 dbR = pow (dbR, dbGammaR);
 dbR = dbR > GAMMACONSTANT ? dbR - GAMMACONSTANT : dbR;
 return((int) (dbR * MAXVAL));
}

int SetGammaG(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dbGammaG)
{
 double dbG = (double)(GetValueG(iX, iY, iWidth, iHeight, iBPP, pbySrc) / MAXVAL);
 dbG = pow (dbG, dbGammaG);
 dbG = dbG > GAMMACONSTANT ? dbG - GAMMACONSTANT : dbG;
 return((int) (dbG * MAXVAL));
}

int SetGammaB(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dbGammaB)
{
 double dbB = (double)(GetValueB(iX, iY, iWidth, iHeight, iBPP, pbySrc) / MAXVAL);
 dbB = pow (dbB, dbGammaB);
 dbB = dbB > GAMMACONSTANT ? dbB - GAMMACONSTANT : dbB;
 return((int) (dbB * MAXVAL));
}

iX                    -> x cordinate of the Pixel
iY                    -> y cordinate of the Pixel
iWidth           -> Width of the Image
iHeight          -> Height of the Image
pbySrc           -> Pointer to the Buffer where image is readed
iBpp               -> Bytes per pixel
dbGammaR  -> New Gamma value of Red Component of the Pixel
dbGammaG  -> New Gamma value of Green Component of the Pixel
dbGammaB  -> New Gamma value of Blue Component of the Pixel

The Gamma values should be in the range ie,  dbGammaR,  dbGammaG,  dbGammaB should be in 0.0 to 4.00
The default value is 1.0.

You can call these functions to the entire pixels of the image for the Gamma change of the image

NB: For the other functions given here, please refer the previous posts


									
#define REDCONSTANT     0.299
#define GREENCONSTANT   0.587
#define BLUECONSTANT    0.114
#define LUMALIMIT       219.0/255.0
#define LUMAVAL         16
int SetSaturationR(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dbSaturationR)
{
 double dbLum = 0.0;
 dbLum = (( (REDCONSTANT * GetValueR(iX, iY, iWidth, iHeight, iBPP, pbySrc)) + (GREENCONSTANT * GetValueG(iX, iY, iWidth, iHeight, iBPP, pbySrc))  + (BLUECONSTANT * GetValueB(iX, iY, iWidth, iHeight, iBPP, pbySrc))) * LUMALIMIT ) + LUMAVAL;
 return(Clip((int)(dbLum + (( GetValueR(iX, iY, iWidth, iHeight, iBPP, pbySrc)  - dbLum ) * dbSaturationR ))));
}

int SetSaturationG(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dbSaturationG)
{
 double dbLum = 0.0;
 dbLum = (( (REDCONSTANT * GetValueR(iX, iY, iWidth, iHeight, iBPP, pbySrc)) + (GREENCONSTANT * GetValueG(iX, iY, iWidth, iHeight, iBPP, pbySrc))  + (BLUECONSTANT * GetValueB(iX, iY, iWidth, iHeight, iBPP, pbySrc))) * LUMALIMIT ) + LUMAVAL;
 return (Clip((int)(dbLum + (( GetValueG(iX, iY, iWidth, iHeight, iBPP, pbySrc)- dbLum ) * dbSaturationG ))));
}

int SetSaturationB(int iX, int iY, int iWidth, int iHeight, int iBPP, BYTE *pbySrc, double dbSaturationB)
{
 double dbLum = 0.0;
 dbLum = (( (REDCONSTANT * GetValueR(iX, iY, iWidth, iHeight, iBPP, pbySrc)) + (GREENCONSTANT * GetValueG(iX, iY, iWidth, iHeight, iBPP, pbySrc))  + (BLUECONSTANT * GetValueB(iX, iY, iWidth, iHeight, iBPP, pbySrc))) * LUMALIMIT ) + LUMAVAL;
 return (Clip((int)(dbLum + (( GetValueB(iX, iY, iWidth, iHeight, iBPP, pbySrc) - dbLum ) * dbSaturationB ))));
}

iX                        -> x cordinate of the Pixel
iY                        -> y cordinate of the Pixel
iWidth                -> Width of the Image
iHeight               -> Height of the Image
pbySrc               -> Pointer to the Buffer where image is readed
iBpp                    -> Bytes per pixel
dbSaturationR  -> New Saturation value of Red Component of the Pixel
dbSaturationG  -> New Saturation value of Green Component of the Pixel
dbSaturationB  -> New Saturation value of Blue Component of the Pixel

The Saturation values should be in the range ie,  iBrightValR, iBrightValG, iBrightValB should be in 0.0 to 2.00
The default value is 1.00.

You can call these functions to the entire pixels of the image for the Saturation change of the image

NB: For the other functions given here, please refer the previous posts

#define BCHANNEL 0
#define GCHANNEL 1
#define RCHANNEL 2
unsigned char Clip(int iVal)
{
 return ( iVal > 255 ? 255 : ( iVal < 0 ) ? 0 : iVal) ;
}

//Pixel Functions
int GetValue(int iX, int iY, int iImageWidth, int iImageHeight, int iChannel, int iBPP, BYTE *pbySrc)
{
 Clip(iX);
 Clip(iY);
 return pbySrc[iY * iImageWidth * iBPP + iX * iBPP + iChannel];
}

int GetValueR(int iX, int iY, int iImageWidth, int iImageHeight, int iBPP, BYTE *pbySrc)
{
 return GetValue(iX, iY, iImageWidth, iImageHeight, RCHANNEL, iBPP,pbySrc);
}

int GetValueG(int iX, int iY, int iImageWidth, int iImageHeight, int iBPP, BYTE *pbySrc)
{
 return GetValue(iX, iY, iImageWidth, iImageHeight, GCHANNEL, iBPP, pbySrc);
}

int GetValueB(int iX, int iY, int iImageWidth, int iImageHeight, int iBPP, BYTE *pbySrc)
{
 return GetValue(iX, iY, iImageWidth, iImageHeight, BCHANNEL, iBPP, pbySrc);
}

Here the iX and iY values are the pixel positions
iImageWidth  -> Width of the Image
iImageHeight -> Height of the Image
iBPP                -> Bytes per pixel ie bitsperPixel/8. For eg. RGB 24 it is 3.
pbySrc            -> Address of the Source buffer

Call the GetValueR() for Red value of a pixel, Call the GetValueG() for Green value of a pixel and Call the GetValueB() for Blue value of a pixel from your application.

NB: These functions are applicatoion for interlaced fromat or packed RGB formats and not for the planar formats

Install CUDA Tool kit and SDK in you PC.
open your project containing the CUDA files
Set CUDA build Rules to your project.
Right Click on the .cu file
Select properties.
Under Configuration properties, select CUDA BUILD Rule v *.*.*
Under CUDA BUILD Rule v *.*.*, Select
General
Generate Debug Information – Yes (-D_DEBUG)
Emulation Mode – Yes (-deviceemu -D_DEVICEEMU)
Optimization – Disabled (/Od)
Hybrid CUDA/C++ options
RunTime Library – Multi-Threaded Debug (/MTd)

After debugging completed then change the options as
General
Generate Debug Information – No
Emulation Mode – No
Optimization – Maximize Speed (/O2)
Hybrid CUDA/C++ options
RunTime Library – Multi-Threaded (/MT)

Right Click on the .cu file in the project in Solution explorer
Select properties
In Configuration properties, Click General. In the Tool column it displays as Cuda build Rule v*.*.*
Below General you can see Cuda Build Rule v*.*.* and its options
Also it can be checked by selecting the project properties.
ie,Right click on the project.
Selct properties
Under Configuration properties (in the last), CUDA Build Rule v*.*.* can be seen

Open Visual Studio project
Add .cu file to the project
Right Click on the project.
Select custom build rules.
Select CUDA Build Rule v*.*.*
Click OK.
Now build the project.

NB: Do not forget to add the required header files and lib files to project. Also add the path of the included header files and lib files to the project as normal Visual Studio project.