Guest User

Untitled

a guest
Apr 3rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.30 KB | None | 0 0
  1. /*
  2. Christopher Ginac
  3. image.cpp
  4. */
  5.  
  6. #include "cuda_runtime.h"
  7. #include "device_launch_parameters.h"
  8. #include <stdlib.h>
  9. #include <iostream>
  10. #include "Image.cuh"
  11. #include <cmath>
  12. using namespace std;
  13. const int ntpb = 1024;
  14.  
  15. __global__ void enlarge(int* a, int* b, int sz, int scale, int cols, int scols) {
  16.     int idx = blockIdx.x * blockDim.x + threadIdx.x;
  17.     int x = idx / scols;
  18.     int y = idx % scols;
  19.     if (idx < sz) {
  20.         a[idx] = b[(x / scale) * cols + (y / scale)];
  21.     }
  22. }
  23.  
  24. __global__ void negate(int* a, int* b, int n) {
  25.     int idx = blockIdx.x * blockDim.x + threadIdx.x;
  26.     if (idx < n) {
  27.         a[idx] = -(b[idx]) + 255;
  28.     }
  29. }
  30.  
  31. __global__ void verticalReflect(int* a, int* b, int sz, int n, int m) {
  32.     int idx = blockIdx.x * blockDim.x + threadIdx.x;
  33.     int x = idx / m;
  34.     int y = idx % m;
  35.     if (idx < sz) {
  36.         a[x * m + (m - y)] = b[idx];
  37.     }
  38. }
  39.  
  40. __global__ void horizontalReflect(int* a, int* b, int sz, int n, int m) {
  41.     int idx = blockIdx.x * blockDim.x + threadIdx.x;
  42.     int x = idx / m;
  43.     int y = idx % m;
  44.     if (idx < sz) {
  45.         a[(n - x) * m + y] = b[idx];
  46.     }
  47. }
  48.  
  49. Image::Image()
  50. /* Creates an Image 0x0 */
  51. {
  52.     N = 0;
  53.     M = 0;
  54.     Q = 0;
  55.  
  56.     pixelVal = NULL;
  57. }
  58.  
  59. Image::Image(int numRows, int numCols, int grayLevels)
  60. /* Creates an Image of numRows x numCols and creates the arrays for it*/
  61. {
  62.  
  63.     N = numRows;
  64.     M = numCols;
  65.     Q = grayLevels;
  66.  
  67.     pixelVal = new int[N * M];
  68.     for (int i = 0; i < N; i++)
  69.     {
  70.         for (int j = 0; j < M; j++)
  71.             pixelVal[i * M + j] = 0;
  72.     }
  73. }
  74.  
  75. Image::~Image()
  76. /*destroy image*/
  77. {
  78.     N = 0;
  79.     M = 0;
  80.     Q = 0;
  81.     delete pixelVal;
  82. }
  83.  
  84. Image::Image(const Image& oldImage)
  85. /*copies oldImage into new Image object*/
  86. {
  87.     N = oldImage.N;
  88.     M = oldImage.M;
  89.     Q = oldImage.Q;
  90.  
  91.     pixelVal = new int[N * M];
  92.     for (int i = 0; i < N; i++)
  93.     {
  94.         for (int j = 0; j < M; j++)
  95.             pixelVal[i * M + j] = oldImage.pixelVal[i * M + j];
  96.     }
  97. }
  98.  
  99. void Image::operator=(const Image& oldImage)
  100. /*copies oldImage into whatever you = it to*/
  101. {
  102.     N = oldImage.N;
  103.     M = oldImage.M;
  104.     Q = oldImage.Q;
  105.  
  106.     pixelVal = new int[N * M];
  107.     for (int i = 0; i < N; i++)
  108.     {
  109.         for (int j = 0; j < M; j++)
  110.             pixelVal[i * M + j] = oldImage.pixelVal[i * M + j];
  111.     }
  112. }
  113.  
  114. void Image::setImageInfo(int numRows, int numCols, int maxVal)
  115. /*sets the number of rows, columns and graylevels*/
  116. {
  117.     N = numRows;
  118.     M = numCols;
  119.     Q = maxVal;
  120. }
  121.  
  122. void Image::getImageInfo(int &numRows, int &numCols, int &maxVal)
  123. /*returns the number of rows, columns and gray levels*/
  124. {
  125.     numRows = N;
  126.     numCols = M;
  127.     maxVal = Q;
  128. }
  129.  
  130. int Image::getPixelVal(int row, int col)
  131. /*returns the gray value of a specific pixel*/
  132. {
  133.     return pixelVal[row * M + col];
  134. }
  135.  
  136.  
  137. void Image::setPixelVal(int row, int col, int value)
  138. /*sets the gray value of a specific pixel*/
  139. {
  140.     pixelVal[row * M + col] = value;
  141. }
  142.  
  143. bool Image::inBounds(int row, int col)
  144. /*checks to see if a pixel is within the image, returns true or false*/
  145. {
  146.     if (row >= N || row < 0 || col >= M || col < 0)
  147.         return false;
  148.     //else
  149.     return true;
  150. }
  151.  
  152. void Image::getSubImage(int upperLeftRow, int upperLeftCol, int lowerRightRow,
  153.     int lowerRightCol, Image& oldImage)
  154.     /*Pulls a sub image out of oldImage based on users values, and then stores it
  155.     in oldImage*/
  156. {
  157.     int width, height;
  158.  
  159.     width = lowerRightCol - upperLeftCol;
  160.     height = lowerRightRow - upperLeftRow;
  161.  
  162.     Image tempImage(height, width, Q);
  163.  
  164.     for (int i = upperLeftRow; i < lowerRightRow; i++)
  165.     {
  166.         for (int j = upperLeftCol; j < lowerRightCol; j++)
  167.             tempImage.pixelVal[(i - upperLeftRow) * height + j - upperLeftCol] = oldImage.pixelVal[i * oldImage.M + j];
  168.     }
  169.  
  170.     oldImage = tempImage;
  171. }
  172.  
  173. int Image::meanGray()
  174. /*returns the mean gray levels of the Image*/
  175. {
  176.     int totalGray = 0;
  177.  
  178.     for (int i = 0; i < N; i++)
  179.     {
  180.         for (int j = 0; j < M; j++)
  181.             totalGray += pixelVal[i * M + j];
  182.     }
  183.  
  184.     int cells = M * N;
  185.  
  186.     return (totalGray / cells);
  187. }
  188.  
  189. void Image::enlargeImage(int value, Image& oldImage)
  190. /*enlarges Image and stores it in tempImage, resizes oldImage and stores the
  191. larger image in oldImage*/
  192. {
  193.     int rows, cols, gray;
  194.     int pixel;
  195.     int enlargeRow, enlargeCol;
  196.  
  197.     rows = oldImage.N * value;
  198.     cols = oldImage.M * value;
  199.     gray = oldImage.Q;
  200.  
  201.     Image tempImage(rows, cols, gray);
  202.  
  203.    
  204.     int r = oldImage.N;
  205.     int c = oldImage.M;
  206.  
  207.     int* d_temp = nullptr;
  208.     int* d_img = nullptr;
  209.     int size = rows * cols;
  210.     int nblocks = size / ntpb;
  211.  
  212.     cudaMalloc((void**)&d_temp, size * sizeof(int));
  213.     cudaMalloc((void**)&d_img, size * sizeof(int));
  214.    
  215.     cudaMemcpy(d_temp, tempImage.pixelVal, size * sizeof(int), cudaMemcpyHostToDevice);
  216.     cudaMemcpy(d_img, oldImage.pixelVal, (r * c) * sizeof(int), cudaMemcpyHostToDevice);
  217.  
  218.     enlarge << <nblocks, ntpb >> >(d_temp, d_img, size, value, c, cols);
  219.  
  220.     cudaDeviceSynchronize();
  221.  
  222.     //set the image's data
  223.     cudaMemcpy(tempImage.pixelVal, d_temp, size * sizeof(int), cudaMemcpyDeviceToHost);
  224.  
  225.     //free device mem
  226.     cudaFree(d_temp);
  227.     cudaFree(d_img);
  228.  
  229.     oldImage = tempImage;
  230. }
  231.  
  232. void Image::shrinkImage(int value, Image& oldImage)
  233. /*Shrinks image as storing it in tempImage, resizes oldImage, and stores it in
  234. oldImage*/
  235. {
  236.     int rows, cols, gray;
  237.  
  238.     rows = oldImage.N / value;
  239.     cols = oldImage.M / value;
  240.     gray = oldImage.Q;
  241.  
  242.     Image tempImage(rows, cols, gray);
  243.  
  244.     for (int i = 0; i < rows; i++)
  245.     {
  246.         for (int j = 0; j < cols; j++)
  247.             tempImage.pixelVal[i * cols + j] = oldImage.pixelVal[(i * value) * cols + j * value];
  248.     }
  249.     oldImage = tempImage;
  250. }
  251.  
  252. void Image::reflectImage(bool flag, Image& oldImage)
  253. /*Reflects the Image based on users input*/
  254. {
  255.     int rows = oldImage.N;
  256.     int cols = oldImage.M;
  257.     Image tempImage(oldImage);
  258.  
  259.     int* d_temp = nullptr;
  260.     int* d_img = nullptr;
  261.     int size = rows * cols;
  262.     int nblocks = size / ntpb;
  263.     cudaMalloc((void**)&d_temp, size * sizeof(int));
  264.     cudaMalloc((void**)&d_img, size * sizeof(int));
  265.     cudaMemcpy(d_temp, tempImage.pixelVal, size * sizeof(int), cudaMemcpyHostToDevice);
  266.     cudaMemcpy(d_img, oldImage.pixelVal, size * sizeof(int), cudaMemcpyHostToDevice);
  267.     if (flag) {
  268.         horizontalReflect << <nblocks, ntpb >> >(d_temp, d_img, size, rows, cols);
  269.     }
  270.     else {
  271.         verticalReflect << <nblocks, ntpb >> >(d_temp, d_img, size, rows, cols);
  272.     }
  273.     cudaDeviceSynchronize();
  274.     cudaMemcpy(tempImage.pixelVal, d_temp, size * sizeof(int), cudaMemcpyDeviceToHost);
  275.     cudaFree(d_temp);
  276.     cudaFree(d_img);
  277.  
  278.     oldImage = tempImage;
  279. }
  280.  
  281. void Image::translateImage(int value, Image& oldImage)
  282. /*translates image down and right based on user value*/
  283. {
  284.     int rows = oldImage.N;
  285.     int cols = oldImage.M;
  286.     int gray = oldImage.Q;
  287.     Image tempImage(N, M, Q);
  288.  
  289.     for (int i = 0; i < (rows - value); i++)
  290.     {
  291.         for (int j = 0; j < (cols - value); j++)
  292.             tempImage.pixelVal[(i + value) * cols + j + value] = oldImage.pixelVal[i * cols + j];
  293.     }
  294.  
  295.     oldImage = tempImage;
  296. }
  297.  
  298. void Image::rotateImage(int theta, Image& oldImage)
  299. /*based on users input and rotates it around the center of the image.*/
  300. {
  301.     int r0, c0;
  302.     int r1, c1;
  303.     int rows, cols;
  304.     rows = oldImage.N;
  305.     cols = oldImage.M;
  306.     Image tempImage(rows, cols, oldImage.Q);
  307.  
  308.     float rads = (theta * 3.14159265) / 180.0;
  309.  
  310.     r0 = rows / 2;
  311.     c0 = cols / 2;
  312.  
  313.     for (int r = 0; r < rows; r++)
  314.     {
  315.         for (int c = 0; c < cols; c++)
  316.         {
  317.             r1 = (int)(r0 + ((r - r0) * cos(rads)) - ((c - c0) * sin(rads)));
  318.             c1 = (int)(c0 + ((r - r0) * sin(rads)) + ((c - c0) * cos(rads)));
  319.  
  320.             if (inBounds(r1, c1))
  321.             {
  322.                 tempImage.pixelVal[r1 * cols + c1] = oldImage.pixelVal[r * cols + c];
  323.             }
  324.         }
  325.     }
  326.  
  327.     for (int i = 0; i < rows; i++)
  328.     {
  329.         for (int j = 0; j < cols; j++)
  330.         {
  331.             if (tempImage.pixelVal[i * cols + j] == 0)
  332.                 tempImage.pixelVal[i * cols + j] = tempImage.pixelVal[i * cols + j + 1];
  333.         }
  334.     }
  335.     oldImage = tempImage;
  336. }
  337.  
  338. Image Image::operator+(const Image &oldImage)
  339. /*adds images together, half one image, half the other*/
  340. {
  341.     Image tempImage(oldImage);
  342.  
  343.     int rows, cols;
  344.     rows = oldImage.N;
  345.     cols = oldImage.M;
  346.  
  347.     for (int i = 0; i < rows; i++)
  348.     {
  349.         for (int j = 0; j < cols; j++)
  350.             tempImage.pixelVal[i * cols + j] = (pixelVal[i * cols + j] + oldImage.pixelVal[i * cols + j]) / 2;
  351.     }
  352.  
  353.     return tempImage;
  354. }
  355.  
  356. Image Image::operator-(const Image& oldImage)
  357. /*subtracts images from each other*/
  358. {
  359.     Image tempImage(oldImage);
  360.  
  361.     int rows, cols;
  362.     rows = oldImage.N;
  363.     cols = oldImage.M;
  364.     int tempGray = 0;
  365.  
  366.     for (int i = 0; i < rows; i++)
  367.     {
  368.         for (int j = 0; j < cols; j++)
  369.         {
  370.  
  371.             tempGray = abs(pixelVal[i * cols + j] - oldImage.pixelVal[i * cols + j]);
  372.             if (tempGray < 35)// accounts for sensor flux
  373.                 tempGray = 0;
  374.             tempImage.pixelVal[i * cols + j] = tempGray;
  375.         }
  376.  
  377.     }
  378.  
  379.     return tempImage;
  380. }
  381.  
  382. void Image::negateImage(Image& oldImage)
  383. /*negates image*/
  384. {
  385.     Image tempImage(N, M, Q);
  386.  
  387.     int* d_temp = nullptr;
  388.     int* d_img = nullptr;
  389.     int size = N * M;
  390.     int nblocks = size / ntpb;
  391.     cudaMalloc((void**)&d_temp, size * sizeof(int));
  392.     cudaMalloc((void**)&d_img, size * sizeof(int));
  393.     cudaMemcpy(d_temp, tempImage.pixelVal, size * sizeof(int), cudaMemcpyHostToDevice);
  394.     cudaMemcpy(d_img, pixelVal, size * sizeof(int), cudaMemcpyHostToDevice);
  395.    
  396.     negate << <nblocks, ntpb >> >(d_temp, d_img, size);
  397.  
  398.     cudaError_t err = cudaGetLastError();
  399.     if (err != cudaSuccess)
  400.         printf("Error: %s\n", cudaGetErrorString(err));
  401.    
  402.     cudaDeviceSynchronize();
  403.     cudaMemcpy(tempImage.pixelVal, d_temp, size * sizeof(int), cudaMemcpyDeviceToHost);
  404.     cudaFree(d_temp);
  405.     cudaFree(d_img);
  406.  
  407.     oldImage = tempImage;
  408. }
Add Comment
Please, Sign In to add comment