Advertisement
regzarr

convo (needs fixing)

Jun 4th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 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 5 // 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.   uchar3 *bufArray = (uchar3 *)malloc(H * W * sizeof(uchar3));
  32.   fread(bufArray, sizeof(uchar3), W*H, input);
  33.   fwrite(bufArray, sizeof(uchar3), W*H, output);
  34.   return;
  35.    
  36.   int i, j;
  37.   for (i = 0; i < H; ++i){
  38.     for (j = 0; j < W; ++j){
  39.       //unsigned sumYUV[3] = { 0 };
  40.       unsigned3 sumYUV = { 0 };
  41.       //sumYUV[0] = 0; sumYUV[1] = 0; sumYUV[2] = 0;
  42.       for (int k = i - BLUR_VAL; (k <= i + BLUR_VAL) && k < H; ++k){
  43.         for (int l = j - BLUR_VAL; (l <= j + BLUR_VAL) && l < W; ++l){
  44.           if (k < 0){
  45.         k = -1;
  46.         break;
  47.           }
  48.           if (l < 0){
  49.         l = -1;
  50.         continue;
  51.           }
  52.           // sumYUV += (__builtin_convertvector(bufArray[k * W + l], unsigned3));
  53.           /*sumYUV[0] += bufArray[k * W + l].y;
  54.           sumYUV[1] += bufArray[k * W + l].u;
  55.           sumYUV[2] += bufArray[k * W + l].v;*/
  56.           sumYUV[0] += bufArray[k * W + l][0];
  57.           sumYUV[1] += bufArray[k * W + l][1];
  58.           sumYUV[2] += bufArray[k * W + l][2];
  59.         }
  60.       }
  61.       uchar3 Pixel = {
  62.         //unsigned char Pixel[3] = {
  63.         sumYUV[0] / KERNEL_SIZE, sumYUV[1] / KERNEL_SIZE, sumYUV[2] / KERNEL_SIZE
  64.       };
  65.       fwrite(&Pixel, 1, 3, output);
  66.     }
  67.   }
  68.    
  69.     free(bufArray);
  70.     fclose(input);
  71.     fclose(output);
  72. }
  73.  
  74. int main(){
  75.   //printf("kernel length is %d\n",KERNEL_LENGTH);
  76.   foo();
  77.   return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement