Advertisement
Evyatar12

cpy

Jan 1st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.93 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4.    unsigned char red;
  5.    unsigned char green;
  6.    unsigned char blue;
  7. } pixel;
  8.  
  9. typedef struct {
  10.     unsigned short red;
  11.     unsigned short green;
  12.     unsigned short blue;
  13. } summed_pixel;
  14.  
  15. typedef union {
  16.     summed_pixel sum;
  17.     unsigned long wrapper;
  18.     unsigned char bytes[8];
  19. } long_summed_pixel;
  20.  
  21.  
  22. // adds a pixel with a given scale to a long_summed_pixel
  23. #define ADD_TO_SUM(summed, pix, scale) \
  24.     summed.sum.red += pix.red * scale; \
  25.     summed.sum.green += pix.green * scale; \
  26.     summed.sum.blue += pix.blue * scale;
  27.  
  28. void applyKernelBlur(int dim, pixel* curr, pixel* out) {
  29.     long_summed_pixel val = {0};
  30.  
  31.     // line before
  32.     ADD_TO_SUM(val, curr[-dim - 1], 1);
  33.     ADD_TO_SUM(val, curr[-dim], 1);
  34.     ADD_TO_SUM(val, curr[-dim + 1], 1);
  35.  
  36.     // current line
  37.     ADD_TO_SUM(val, curr[-1], 1);
  38.     ADD_TO_SUM(val, curr[0], 1);
  39.     ADD_TO_SUM(val, curr[1], 1);
  40.  
  41.     // line after
  42.     ADD_TO_SUM(val, curr[dim - 1], 1);
  43.     ADD_TO_SUM(val, curr[dim], 1);
  44.     ADD_TO_SUM(val, curr[dim + 1], 1);
  45.  
  46.     val.sum.red /= 9;
  47.     val.sum.green /= 9;
  48.     val.sum.blue /= 9;
  49.  
  50.     out->red = val.bytes[0];
  51.     out->green = val.bytes[2];
  52.     out->blue = val.bytes[4];
  53.  
  54. }
  55.  
  56. #define KERNEL_SIZE 3
  57. #define HALF_KERNEL (KERNEL_SIZE / 2)
  58. /*
  59. * Apply the kernel over each pixel.
  60. */
  61. void smooth(int dim, pixel *src, pixel *dst, char isBlur) {
  62.     int rowoffset, coloffset;
  63.  
  64.     size_t numPixels = n * m;
  65.  
  66.     const int rowMin = HALF_KERNEL * dim;
  67.     const int rowMax = dim * (dim - HALF_KERNEL);
  68.     const int rowJump = dim;
  69.  
  70.     const int colMax = dim - HALF_KERNEL;
  71.  
  72.     if (isBlur) {
  73.         // in case it's blur, do this
  74.         // dst += rowMin;
  75.         // src += rowMin;
  76.  
  77.         for (rowoffset = rowMin; rowoffset < rowMax; rowoffset += rowJump) {
  78.             // dst += HALF_KERNEL;
  79.             // src += HALF_KERNEL;
  80.             for (coloffset = HALF_KERNEL; coloffset < colMax; coloffset++) {
  81.                 applyKernelBlur(dim, &src[rowoffset + coloffset], &dst[rowoffset + coloffset]);
  82.                 // src++;
  83.                 // dst++;
  84.             }
  85.  
  86.             // src += rowJump;
  87.             // dst += rowJump;
  88.         }
  89.     }
  90.     else {
  91.         // in case it's sharpen
  92.     }
  93. }
  94.  
  95. void charsToPixels(Image *charsImg, pixel* pixels) {
  96.  
  97.     int row, col;
  98.     for (row = 0 ; row < m ; row++) {
  99.         for (col = 0 ; col < n ; col++) {
  100.  
  101.             pixels[row*n + col].red = image->data[3*row*n + 3*col];
  102.             pixels[row*n + col].green = image->data[3*row*n + 3*col + 1];
  103.             pixels[row*n + col].blue = image->data[3*row*n + 3*col + 2];
  104.         }
  105.     }
  106. }
  107.  
  108. void pixelsToChars(pixel* pixels, Image *charsImg) {
  109.  
  110.     int row, col;
  111.     for (row = 0 ; row < m ; row++) {
  112.         for (col = 0 ; col < n ; col++) {
  113.  
  114.             image->data[3*row*n + 3*col] = pixels[row*n + col].red;
  115.             image->data[3*row*n + 3*col + 1] = pixels[row*n + col].green;
  116.             image->data[3*row*n + 3*col + 2] = pixels[row*n + col].blue;
  117.         }
  118.     }
  119. }
  120.  
  121. void doConvolution(Image *image, int isBlur) {
  122.  
  123.     pixel* pixelsImg = malloc(m*n*sizeof(pixel));
  124.     pixel* dest = malloc(m*n*sizeof(pixel));
  125.  
  126.     charsToPixels(image, pixelsImg);
  127.  
  128.     printf("m: %d, n: %d\n", m ,n);
  129.     smooth(m, pixelsImg, dest, 1);
  130.  
  131.     pixelsToChars(dest, image);
  132.  
  133.     free(pixelsImg);
  134.     free(dest);
  135. }
  136.  
  137. void myfunction(Image *image, char* srcImgpName, char* blurRsltImgName, char* sharpRsltImgName) {
  138.  
  139.     /*
  140.     * [1, 1, 1]
  141.     * [1, 1, 1]
  142.     * [1, 1, 1]
  143.     */
  144.     int blurKernel[3][3] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
  145.  
  146.     /*
  147.     * [-1, -1, -1]
  148.     * [-1, 9, -1]
  149.     * [-1, -1, -1]
  150.     */
  151.     int sharpKernel[3][3] = {{-1,-1,-1},{-1,9,-1},{-1,-1,-1}};
  152.  
  153.     // blur image
  154.     doConvolution(image, 1);
  155.  
  156.     // write result image to file
  157.     writeBMP(image, srcImgpName, blurRsltImgName);
  158.  
  159.     //smooth(m, backupOrg, pixelsImg, kernelSize, sharpKernel, 1);
  160.  
  161.     // sharpen the resulting image
  162.     doConvolution(image, 1);
  163.  
  164.     // write result image to file
  165.     writeBMP(image, srcImgpName, sharpRsltImgName);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement