Advertisement
regzarr

first convolution v1.1 (notepad++ indentation)

Apr 15th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define W 800
  5. #define H 1200
  6. #define SIZE_IN_BYTES W*H*3
  7. #define BLUR_VAL 10 // distance in pixels to neighbours taken for blurring process
  8. #define KERNEL_LENGTH (BLUR_VAL * 2 + 1)
  9. #define KERNEL_SIZE (KERNEL_LENGTH * KERNEL_LENGTH)
  10.  
  11. typedef unsigned unsigned3 __attribute__((ext_vector_type(3)));
  12. typedef unsigned char uchar3 __attribute__((ext_vector_type(3)));
  13.  
  14. typedef struct __yuv {
  15.   unsigned char y, u, v;
  16. }_yuv;
  17.  
  18. void foo(){
  19.   FILE *input =
  20.     fopen("david.rgb", "rb");
  21.   if (input == NULL){
  22.     printf("Error opening reading file\n");
  23.     return;
  24.   }
  25.   FILE *output = fopen("rgbee.rgb", "wb");
  26.   if(output == NULL){
  27.     printf("Error opening/creating output file\n");
  28.     return;
  29.   }
  30.  
  31.   _yuv *bufArray = (_yuv *)malloc(H * W * sizeof(_yuv));
  32.   fread(bufArray, sizeof(_yuv), W*H, input);
  33.    
  34.   int i, j;
  35.   for (i = 0; i < H; ++i){
  36.     for (j = 0; j < W; ++j){
  37.       //unsigned sumYUV[3] = { 0 };
  38.             unsigned3 sumYUV = { 0 };
  39.             //sumYUV[0] = 0; sumYUV[1] = 0; sumYUV[2] = 0;
  40.       for (int k = i - BLUR_VAL; (k <= i + BLUR_VAL) && k < H; ++k){
  41.                 for (int l = j - BLUR_VAL; (l <= j + BLUR_VAL) && l < W; ++l){
  42.                     if (k < 0){
  43.                         k = -1;
  44.                         break;
  45.                     }
  46.                     if (l < 0){
  47.                         l = -1;
  48.                         continue;
  49.                     }
  50.            
  51.                     sumYUV[0] += bufArray[k * W + l].y;
  52.                     sumYUV[1] += bufArray[k * W + l].u;
  53.                     sumYUV[2] += bufArray[k * W + l].v;
  54.                 }
  55.       }
  56.             //uchar3 Pixel = {
  57.             unsigned char Pixel[3] = {
  58.                 sumYUV[0] / KERNEL_SIZE, sumYUV[1] / KERNEL_SIZE, sumYUV[2] / KERNEL_SIZE
  59.             };
  60.       fwrite(Pixel, 1, 3, output);
  61.     }
  62.   }
  63.  
  64.     free(bufArray);
  65.   fclose(input);
  66.   fclose(output);
  67. }
  68.  
  69. int main(){
  70.     //printf("kernel length is %d\n",KERNEL_LENGTH);
  71.   foo();
  72.   return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement