Advertisement
Andrei_M

YUVtoRGB

Apr 26th, 2020
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define W 384
  4. #define H 288
  5. #define Bytes 3
  6. #define SIZE (W*H*Bytes)
  7.  
  8. // YUV444 -> RGB888
  9. // write new img to a file
  10. // get green value for X = 60, Y = 257
  11. // both have 3 Bpp
  12. //|R0|R1|R2|R3|G0|G1|G2|G3|B0|B1|B2|B3|
  13.  
  14. FILE* openRead(char *path)
  15. {
  16.  
  17.     FILE *f;
  18.  
  19.     if( (f = fopen(path, "rb")) == NULL)
  20.     {
  21.         perror("can't open read file");
  22.         exit(-2);
  23.     }
  24.     return f;
  25. }
  26. FILE* openWrite(char *path)
  27. {
  28.  
  29.     FILE *g;
  30.  
  31.     if((g = fopen(path, "wb")) == NULL)
  32.     {
  33.         perror("can't open write file");
  34.         exit(-2);
  35.     }
  36.  
  37.     return g;
  38. }
  39.  
  40. int clamp(double x, int min, int max)
  41. {
  42.     if(x < min)
  43.         x = min;
  44.     else
  45.         if(x > max)
  46.             x = max;
  47.     return x;
  48. }
  49.  
  50.  
  51. void scan_image(int x, int y, char *path, char *newPath)
  52. {
  53.     FILE *f = openRead(path);
  54.     FILE *g = openWrite(newPath);
  55.  
  56.     int c, d, e, R, G, B;
  57.     int Y, U, V;
  58.  
  59.     char newCharBuffer[W*H*3] = {0};
  60.     fread(newCharBuffer, (W*H*3), 1, f);
  61.  
  62.     for(int i = 0; i < (W*H*3) ; i = i + 3) //cate 3 bytes Y, U, V
  63.     {
  64.  
  65.        Y = newCharBuffer[i];
  66.        U = newCharBuffer[i+1];
  67.        V = newCharBuffer[i+2];
  68.        if(i >= 0 && i <= 15)
  69.        {
  70.            printf("\nY = %d\n", Y);
  71.            printf("U = %d\n", U);
  72.            printf("V = %d\n", V);
  73.        }
  74.  
  75.        c = Y - 16;
  76.        d = U - 128;
  77.        e = V - 128;
  78.  
  79.        R = clamp(((298*c + 409*e + 128) >> 8), 0, 255);
  80.        G = clamp(((298*c - 100*d - 208*e + 128) >> 8), 0, 255);
  81.        B = clamp(((298*c + 516*d + 128) >> 8), 0, 255);
  82.         if(i >= 0 && i <= 15)
  83.        {
  84.            printf("\nR = %d\n", R);
  85.            printf("G = %d\n", G);
  86.            printf("B = %d\n", B);
  87.        }
  88.  
  89.         newCharBuffer[i] = R;
  90.         newCharBuffer[i+1] = G;
  91.         newCharBuffer[i+2] = B;
  92.  
  93.     }
  94.  
  95.     fwrite(newCharBuffer, (W*H*3), 1, g); // bun
  96.  
  97.     fclose(f); fclose(g);
  98. }
  99.  
  100.  
  101. int main()
  102. {
  103.     int x = 60;
  104.     int y = 257;
  105.     char path[] = "img2.raw";
  106.     char newPath[] = "newImg.raw";
  107.     scan_image(x, y, path, newPath);
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement