Guest User

Untitled

a guest
Dec 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #define CLAMP(t) ((t>255)?255:((t<0)?0:t))
  2. // RGB to YUV
  3. #define GET_Y(R,G,B) CLAMP((( 66 * (R) + 129 * (G) + 25 * (B) + 128) >> 8) + 16)
  4. #define GET_U(R,G,B) CLAMP(((-38 * (R) - 74 * (G) + 112 * (B) + 128) >> 8) + 128)
  5. #define GET_V(R,G,B) CLAMP(((112 * (R) - 94 * (G) - 18 * (B) + 128) >> 8) + 128)
  6.  
  7. unsigned int Capture::GrabPlanar(unsigned char **Y, unsigned char **U, unsigned char **V){
  8. if(*Y != nullptr || *U != nullptr || *V != nullptr){return 0;}
  9. unsigned char *frame = nullptr;
  10. unsigned int width, height, plane_size = 0;
  11. unsigned int size = GrabBitmap(&frame, width, height, 1);
  12. if( size){ // I420 algorithm...
  13. plane_size = width * height;
  14. *Y = (unsigned char *)malloc(plane_size );
  15. *U = (unsigned char *)malloc(plane_size >> 2);
  16. *V = (unsigned char *)malloc(plane_size >> 2);
  17. unsigned char R, G, B;
  18. unsigned int iY, iU, iV;
  19. iY = iU = iV = 0;
  20. for(unsigned int i; i < size; i+=3){
  21. R = frame[i ];
  22. G = frame[i+1];
  23. B = frame[i+2];
  24. (*Y)[iY] = GET_Y(R,G,B);
  25. // Не знаю что делать с индексами iU и iV
  26. // там ведь буфер в 4 раза меньше, можно на ошибку сегментации нарваться...
  27. iY++;
  28. }
  29. }
  30. if( frame != nullptr ){free(frame);}
  31. return plane_size;
  32. }
Add Comment
Please, Sign In to add comment