Merge from vendor branch NTPD:
[dragonfly.git] / share / examples / meteor / yuvpl.c
1 /* capture a PPM image using YUV planer format and single capture mode */
2 /* Copyright (c) 1995 Mark Tinguely and Jim Lowe 
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Mark Tinguely and Jim Lowe
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <sys/fcntl.h>
35 /*#include <machine/ioctl_meteor.h>*/                             
36 #include "/sys/i386/include/ioctl_meteor.h"                   
37
38
39 typedef unsigned char uint8;
40 typedef signed char int8;
41
42 static uint8 *yuv_data;
43 static int8 *ue, *uo, *ve, *vo;
44 extern int errno;
45 #define ROWS 480
46 #define COLS 640
47 #define SIZE (ROWS * COLS * 2)
48 main()
49 {
50         struct meteor_geomet geo;
51         char b[4],header[16],*p;
52         int i,o,c,r;
53         int y1,y2,u,v;
54         int temp;
55
56         if ((i = open("/dev/meteor", O_RDONLY)) < 0) {
57                 printf("open failed: %d\n", errno);
58                 exit(1);
59         }
60                                 /* set up the capture type and size */
61         geo.rows = ROWS;
62         geo.columns = COLS;
63         geo.frames = 1;
64
65
66         geo.oformat = METEOR_GEO_YUV_PLANER;
67
68         if (ioctl(i, METEORSETGEO, &geo) < 0) {
69                 printf("ioctl failed: %d\n", errno);
70                 exit(1);
71         }
72
73         c = METEOR_FMT_NTSC;
74
75         if (ioctl(i, METEORSFMT, &c) < 0) {
76                 printf("ioctl failed: %d\n", errno);
77                 exit(1);
78         }
79
80         c = METEOR_INPUT_DEV0;
81
82         if (ioctl(i, METEORSINPUT, &c) < 0) {
83                 printf("ioctl failed: %d\n", errno);
84                 exit(1);
85         }
86
87         yuv_data = (uint8 *)mmap((caddr_t)0,SIZE,PROT_READ,MAP_SHARED, i, (off_t)0);
88
89         if (yuv_data == (uint8 *) MAP_FAILED) return (0);
90
91         temp = ROWS * COLS;
92         ue = yuv_data + temp;
93         temp = temp / 4;
94         ve = ue + temp;
95         uo = ve + temp;
96         vo = uo + temp;
97
98 printf("data locations = %08x %08x %08x %08x %08x \n", yuv_data,ue,ve,uo,vo);
99
100         c = METEOR_CAP_SINGLE ;
101         ioctl(i, METEORCAPTUR, &c);
102   
103         printf("read done %d %d\n", errno, i);
104         if ((o = open("yuvpl.ppm", O_WRONLY | O_CREAT, 0644)) < 0) {
105                 printf("ppm open failed: %d\n", errno);
106                 exit(1);
107         }
108
109                 /* make PPM header and save to file */
110         strcpy(&header[0], "P6 640 480 255 ");
111         header[2] = header[6]  = header[10] = header[14] = '\n';
112         write (o, &header[0], 15);
113
114         for (r = 0 ; r < ROWS ; r++) {
115            for (c = 0 ; c < COLS / 2; c++) {
116                 if ((y1 = *yuv_data++) < 0) y1 += 256;
117                 if ((y2 = *yuv_data++) < 0) y2 += 256;
118                 if (r & 1) { /* odd */
119                         u =   *uo++ ;
120                         v =   *vo++;
121                 }
122                 else { /* even */
123                         u =   *ue++;
124                         v =   *ve++;
125                 }
126                 b[0]= (double)y1  + 1.375 * (double)v ; /*r*/
127                 b[1]= (double)y1 - 0.703125 * (double)v -0.34375 * (double)u ;  /* g */
128                 b[2]= (double)y1 + 1.734375 * (double)u ;               /* b */
129                 b[3]= (double)y2  + 1.375 * (double)v ; /*r*/
130                 b[4]= (double)y2 - 0.703125 * (double)v -0.34375 * (double)u ;  /* g */
131                 b[5]= (double)y2 + 1.734375 * (double)u ;               /* b */
132                 write(o,&b[0], 6);
133          }
134         }
135         close(o);
136         close(i);
137         exit(0);
138 }