# hakanardo / video / xvview.c

 ```Hakan Ardo edbd398 2011-02-11 ``` ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224``` ```#include #include #include #include #include #include #include #include #include #include /* ok the following constant should be by right included thru in Xvlib.h */ #ifndef XV_YV12 #define XV_YV12 0x32315659 #endif #ifndef XV_YUY2 #define XV_YUY2 0x32595559 #endif #ifndef XV_UYVY #define XV_UYVY 0x59565955 #endif #define RGB2YUV(r, g, b, y, u, v)\ y = (9798*r + 19235*g + 3736*b) / 32768;\ u = (-4784*r - 9437*g + 14221*b) / 32768 + 128;\ v = (20218*r - 16941*g - 3277*b) / 32768 + 128;\ y = y < 0 ? 0 : y;\ u = u < 0 ? 0 : u;\ v = v < 0 ? 0 : v;\ y = y > 255 ? 255 : y;\ u = u > 255 ? 255 : u;\ v = v > 255 ? 255 : v static inline void rgb2yuy2 (unsigned char *RGB, unsigned char *YUV, int NumPixels) { int i, j; register int y0, y1, u0, u1, v0, v1 ; register int r, g, b; for (i = 0, j = 0; i < 3 * NumPixels; i += 6, j += 4) { r = RGB[i + 0]; g = RGB[i + 1]; b = RGB[i + 2]; RGB2YUV (r, g, b, y0, u0 , v0); r = RGB[i + 3]; g = RGB[i + 4]; b = RGB[i + 5]; RGB2YUV (r, g, b, y1, u1 , v1); YUV[j + 0] = y0; YUV[j + 1] = (u0+u1)/2; YUV[j + 2] = y1; YUV[j + 3] = (v0+v1)/2; } } static inline void bgr2yuy2 (unsigned char *RGB, unsigned char *YUV, int NumPixels) { int i, j; register int y0, y1, u0, u1, v0, v1 ; register int r, g, b; for (i = 0, j = 0; i < 3 * NumPixels; i += 6, j += 4) { r = RGB[i + 2]; g = RGB[i + 1]; b = RGB[i + 0]; RGB2YUV (r, g, b, y0, u0 , v0); r = RGB[i + 5]; g = RGB[i + 4]; b = RGB[i + 3]; RGB2YUV (r, g, b, y1, u1 , v1); YUV[j + 0] = y0; YUV[j + 1] = (u0+u1)/2; YUV[j + 2] = y1; YUV[j + 3] = (v0+v1)/2; } } static inline void yuv2yuy2 (unsigned char *YUV, unsigned char *YUV2, int NumPixels) { int i, j; //register int y0, y1, u0, u1, v0, v1 ; //register int r, g, b; for (i = 0, j = 0; i < 3 * NumPixels; i += 6, j += 4) { YUV2[j + 0] = YUV[i+2]; YUV2[j + 1] = (YUV[i+1]+YUV[i+4])/2; YUV2[j + 2] = YUV[i+5]; YUV2[j + 3] = (YUV[i+0]+YUV[i+3])/2; } } static inline void y2yuy2 (unsigned char *Y, unsigned char *YUV2, int NumPixels) { int i, j; //register int y0, y1, u0, u1, v0, v1 ; //register int r, g, b; for (i = 0, j = 0; i < NumPixels; i += 2, j += 4) { YUV2[j + 0] = Y[i+0]; YUV2[j + 1] = 127; YUV2[j + 2] = Y[i+1]; YUV2[j + 3] = 127; } } int xvview(int w, int h, char *data, int color, char typecode) { static Display *display=NULL; static int adaptor=-1; static XvAdaptorInfo *info; static const long format=XV_YUY2; static unsigned char *frame_buffer; static Window window; static XvImage *xv_image; static GC gc; static XShmSegmentInfo Shminfo; static XGCValues xgcv; static int paused = 0; if (!display) { int num_adaptors; int num_formats; XvImageFormatValues *formats=NULL; int i,j; char xv_name[5]; display=XOpenDisplay(getenv("DISPLAY")); if(display==NULL) { fprintf(stderr, "Could not open display \"%s\"\n",getenv("DISPLAY")); return 0; } XvQueryAdaptors(display,DefaultRootWindow(display),&num_adaptors,&info); for(i=0;idata_size, IPC_CREAT | 0777); Shminfo.shmaddr = (char *) shmat(Shminfo.shmid, 0, 0); Shminfo.readOnly = False; xv_image->data = Shminfo.shmaddr; XShmAttach(display, &Shminfo); XSync(display, False); shmctl(Shminfo.shmid, IPC_RMID, 0); } if (color && typecode == 'B') { rgb2yuy2(data, xv_image->data, w*h); } else if (!color && typecode == 'B') { y2yuy2(data, xv_image->data, w*h); } else { fprintf(stderr, "Unknown image format: %c color=%d\n", typecode, color); return 0; } XvShmPutImage(display,info[adaptor].base_id,window,gc,xv_image, 0,0,w,h, 0,0,w,h, False); XFlush(display); XEvent xev; do { while(XPending(display)>0) { XNextEvent(display,&xev); if (xev.type == KeyPress) { char key[1]; XLookupString(&xev.xkey, key, 1, NULL, NULL); if (key[0]==' ') paused=~paused; else if (key[0] == 27) exit(0); } else if (xev.type == ButtonPress) { if (xev.xbutton.button==1) printf("Click: (%d,%d)\n",xev.xbutton.x,xev.xbutton.y); } } } while (paused); return 1; } ```