Adjust manual pages to recent lock related changes.
[dragonfly.git] / share / examples / meteor / yuvpk.c
1 /* capture a PPM image using YUV packed 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 typedef unsigned char uint8;
39 typedef signed char int8;
40
41 static int8 *yuv_data;
42 extern int errno;
43 #define ROWS 480
44 #define COLS 640
45 #define SIZE (ROWS * COLS * 2)
46 main()
47 {
48         struct meteor_geomet geo;
49         char b[4],header[16],*p;
50         int i,o,c,r;
51         int y1,y2,u,v;
52
53         if ((i = open("/dev/meteor", O_RDONLY)) < 0) {
54                 printf("open failed: %d\n", errno);
55                 exit(1);
56         }
57                                 /* set up the capture type and size */
58         geo.rows = ROWS;
59         geo.columns = COLS;
60         geo.frames = 1;
61
62
63         geo.oformat = METEOR_GEO_YUV_PACKED;
64
65         if (ioctl(i, METEORSETGEO, &geo) < 0) {
66                 printf("ioctl failed: %d\n", errno);
67                 exit(1);
68         }
69
70         c = METEOR_FMT_NTSC;
71
72         if (ioctl(i, METEORSFMT, &c) < 0) {
73                 printf("ioctl failed: %d\n", errno);
74                 exit(1);
75         }
76
77         c = METEOR_INPUT_DEV0;
78
79         if (ioctl(i, METEORSINPUT, &c) < 0) {
80                 printf("ioctl failed: %d\n", errno);
81                 exit(1);
82         }
83
84         yuv_data = (uint8 *)mmap((caddr_t)0,SIZE,PROT_READ,MAP_SHARED, i, (off_t)0);
85
86         if (yuv_data == (int8 *) MAP_FAILED) return (0);
87
88         c = METEOR_CAP_SINGLE ;
89         ioctl(i, METEORCAPTUR, &c);
90   
91         if ((o = open("yuvpk.ppm", O_WRONLY | O_CREAT, 0644)) < 0) {
92                 printf("ppm open failed: %d\n", errno);
93                 exit(1);
94         }
95
96                 /* make PPM header and save to file */
97         strcpy(&header[0], "P6 640 480 255 ");
98         header[2] = header[6]  = header[10] = header[14] = '\n';
99         write (o, &header[0], 15);
100
101         for (c = 0 ; c < ROWS*COLS; c+=2) {
102                 u = *yuv_data++; 
103                 if ((y1 = *yuv_data++) < 0) y1 += 256;
104                 v = *yuv_data++;
105                 if ((y2 = *yuv_data++) < 0) y2 += 256;
106
107                 b[0]= (double)y1  + 1.375 * (double)v ; /*r*/
108                 b[1]= (double)y1 - 0.703125 * (double)v -0.34375 * (double)u ;  /* g */
109                 b[2]= (double)y1 + 1.734375 * (double)u ;               /* b */
110                 b[3]= (double)y2  + 1.375 * (double)v ; /*r*/
111                 b[4]= (double)y2 - 0.703125 * (double)v -0.34375 * (double)u ;  /* g */
112                 b[5]= (double)y2 + 1.734375 * (double)u ;               /* b */
113                 write(o,&b[0], 6);
114         }
115         close(o);
116         close(i);
117         exit(0);
118 }