w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

yuvsplittoppm.c
Go to the documentation of this file.
1 /* yuvsplittoppm.c - construct a portable pixmap from 3 raw files:
2 ** - basename.Y : The Luminance chunk at the size of the Image
3 ** - basename.U : The Chrominance chunk U at 1/4
4 ** - basename.V : The Chrominance chunk V at 1/4
5 ** The subsampled U and V values are made by arithmetic mean.
6 **
7 ** If ccir601 is defined, the produced YUV triples have been scaled again
8 ** to fit into the smaller range of values for this standard.
9 **
10 ** by Marcel Wijkstra <wijkstra@fwi.uva.nl>
11 **
12 ** Based on ppmtoyuvsplit.c
13 **
14 ** Permission to use, copy, modify, and distribute this software and its
15 ** documentation for any purpose and without fee is hereby granted, provided
16 ** that the above copyright notice appear in all copies and that both that
17 ** copyright notice and this permission notice appear in supporting
18 ** documentation. This software is provided "as is" without express or
19 ** implied warranty.
20 */
21 
22 #include <string.h>
23 #include "ppm.h"
24 
25 /* x must be signed for the following to work correctly */
26 #define limit(x) (((x>0xffffff)?0xff0000:((x<=0xffff)?0:x&0xff0000))>>16)
27 
28 int
30 char **argv;
31 {
32  FILE *vf,*uf,*yf;
33  pixel *pixelrow1,*pixelrow2;
34  register pixel *pP1,*pP2;
35  int rows, cols, row;
36  register int col;
37  char *usage="<basename> <width> <height> [-ccir601]";
38  long int u,v,y0,y1,y2,y3,u0,u1,u2,u3,v0,v1,v2,v3;
39  unsigned char *y1buf,*y2buf,*ubuf,*vbuf;
40  char ufname[256],vfname[256],yfname[256];
41  int ccir601=0;
42  /* Whether to create YUV in JFIF(JPEG) or CCIR.601(MPEG) scale */
43 
44 
45 
46  ppm_init(&argc, argv);
47 
48  if ((argc>5) || (argc<4)) pm_usage(usage);
49 
50  if (argc==5) {
51  if (strncmp(argv[4],"-c",2) != 0) ccir601 = 1;
52  } else pm_usage(usage);
53 
54  strcpy(ufname,argv[1]);
55  strcpy(vfname,argv[1]);
56  strcpy(yfname,argv[1]);
57 
58  strcat(ufname,".U");
59  strcat(vfname,".V");
60  strcat(yfname,".Y");
61 
62  uf = fopen(ufname,"rb");
63  vf = fopen(vfname,"rb");
64  yf = fopen(yfname,"rb");
65 
66  if(!(uf && vf && yf)) {
67  perror("error opening input files");
68  exit(0);
69  }
70 
71  cols = atoi(argv[2]);
72  rows = atoi(argv[3]);
73  if (cols <= 0 || rows <= 0)
74  pm_usage(usage);
75 
76  ppm_writeppminit(stdout, cols, rows, (pixval) 255, 0);
77 
78  if(cols & 1) fprintf(stderr,
79  "%s: Warning: odd columns count, exceed ignored\n",
80  argv[0]);
81  if(rows & 1) fprintf(stderr,
82  "%s: Warning: odd rows count, exceed ignored\n",
83  argv[0]);
84 
85  pixelrow1 = ((pixel*) pm_allocrow( cols, sizeof(pixel) ));
86  pixelrow2 = ((pixel*) pm_allocrow( cols, sizeof(pixel) ));
87 
88  y1buf = (unsigned char *) pm_allocrow( cols, 1 );
89  y2buf = (unsigned char *) pm_allocrow( cols, 1 );
90  ubuf = (unsigned char *) pm_allocrow( cols, 1 );
91  vbuf = (unsigned char *) pm_allocrow( cols, 1 );
92 
93  for (row = 0; row < (rows & ~1); row += 2) {
94  unsigned char *y1ptr,*y2ptr,*uptr,*vptr;
95 
96  fread(y1buf, (cols & ~1), 1, yf);
97  fread(y2buf, (cols & ~1), 1, yf);
98  fread(ubuf, cols/2, 1, uf);
99  fread(vbuf, cols/2, 1, vf);
100 
101  y1ptr = y1buf; y2ptr = y2buf; vptr = vbuf; uptr = ubuf;
102 
103  pP1 = pixelrow1; pP2 = pixelrow2;
104 
105  for (col = 0 ; col < (cols & ~1); col += 2) {
106  long int r0,g0,b0,r1,g1,b1,r2,g2,b2,r3,g3,b3;
107 
108  y0 = (long int) *y1ptr++;
109  y1 = (long int) *y1ptr++;
110  y2 = (long int) *y2ptr++;
111  y3 = (long int) *y2ptr++;
112 
113  u = (long int) ((*uptr++) - 128);
114  v = (long int) ((*vptr++) - 128);
115 
116  if (ccir601) {
117  y0 = ((y0-16)*255)/219;
118  y1 = ((y1-16)*255)/219;
119  y2 = ((y2-16)*255)/219;
120  y3 = ((y3-16)*255)/219;
121 
122  u = (u*255)/224 ;
123  v = (v*255)/224 ;
124  }
125  /* mean the chroma for subsampling */
126 
127  u0=u1=u2=u3=u;
128  v0=v1=v2=v3=v;
129 
130 
131 /* The inverse of the JFIF RGB to YUV Matrix for $00010000 = 1.0
132 
133 [Y] [65496 0 91880][R]
134 [U] = [65533 -22580 -46799[G]
135 [V] [65537 116128 -8][B]
136 
137 */
138 
139  r0 = 65536 * y0 + 91880 * v0;
140  g0 = 65536 * y0 - 22580 * u0 - 46799 * v0;
141  b0 = 65536 * y0 + 116128 * u0 ;
142 
143  r1 = 65536 * y1 + 91880 * v1;
144  g1 = 65536 * y1 - 22580 * u1 - 46799 * v1;
145  b1 = 65536 * y1 + 116128 * u1 ;
146 
147  r2 = 65536 * y2 + 91880 * v2;
148  g2 = 65536 * y2 - 22580 * u2 - 46799 * v2;
149  b2 = 65536 * y2 + 116128 * u2 ;
150 
151  r3 = 65536 * y3 + 91880 * v3;
152  g3 = 65536 * y3 - 22580 * u3 - 46799 * v3;
153  b3 = 65536 * y3 + 116128 * u3 ;
154 
155  r0 = limit(r0);
156  r1 = limit(r1);
157  r2 = limit(r2);
158  r3 = limit(r3);
159  g0 = limit(g0);
160  g1 = limit(g1);
161  g2 = limit(g2);
162  g3 = limit(g3);
163  b0 = limit(b0);
164  b1 = limit(b1);
165  b2 = limit(b2);
166  b3 = limit(b3);
167 
168  /* first pixel */
169  PPM_ASSIGN(*pP1, (pixval)r0, (pixval)g0, (pixval)b0);
170  pP1++;
171  /* 2nd pixval */
172  PPM_ASSIGN(*pP1, (pixval)r1, (pixval)g1, (pixval)b1);
173  pP1++;
174  /* 3rd pixval */
175  PPM_ASSIGN(*pP2, (pixval)r2, (pixval)g2, (pixval)b2);
176  pP2++;
177  /* 4th pixval */
178  PPM_ASSIGN(*pP2, (pixval)r3, (pixval)g3, (pixval)b3);
179  pP2++;
180  }
181  ppm_writeppmrow(stdout, pixelrow1, cols, (pixval) 255, 0);
182  ppm_writeppmrow(stdout, pixelrow2, cols, (pixval) 255, 0);
183  }
184  pm_close(stdout);
185 
186  fclose(yf);
187  fclose(uf);
188  fclose(vf);
189  exit(0);
190 }
void __cdecl perror(char const *_ErrMsg)
#define y0
#define y3
int * g2
Definition: bmpfont.h:57
int v
Definition: dviconv.c:10
#define fopen
Definition: xxstdio.h:21
#define fread
Definition: xxstdio.h:25
char * strcpy()
#define v0
#define v1
#define v2
#define r1
#define v3
#define r3
#define r2
#define r0
static char usage[]
Definition: giftopnm.c:59
int col
Definition: gsftopk.c:443
small capitals from c petite p scientific f u
Definition: afcover.h:88
void exit()
int atoi(const char *)
#define fclose
Definition: debug.h:100
#define fprintf
Definition: mendex.h:64
#define b0
Definition: texmfmem.h:168
#define b1
Definition: texmfmem.h:169
#define b3
Definition: texmfmem.h:171
#define b2
Definition: texmfmem.h:170
int strncmp()
char * vfname
Definition: main-euc.c:13
void pm_usage(char *usage)
Definition: libpbm1.c:343
char * pm_allocrow(int cols, int size)
Definition: libpbm1.c:30
static int rows
Definition: pbmclean.c:15
static int cols
Definition: pbmmask.c:21
#define pm_close(file)
Definition: png22pnm.c:120
void ppm_init(int *argcP, argv)
Definition: libppm1.c:21
void ppm_writeppmrow(FILE *file, pixel *pixelrow, int cols, pixval maxval, int forceplain)
Definition: libppm2.c:129
void ppm_writeppminit(FILE *file, int cols, int rows, pixval maxval, int forceplain)
Definition: libppm2.c:23
#define PPM_ASSIGN(p, red, grn, blu)
Definition: ppm.h:46
gray pixval
Definition: ppm.h:9
static int row
Definition: ps2pk.c:587
#define y1
#define y2
Definition: ppm.h:33
Definition: vf.c:59
#define FILE
Definition: t1stdio.h:34
return() int(((double) *(font_tbl[cur_fnt].wtbl+(int)(*(font_tbl[cur_fnt].char_wi+(int)(ch - font_tbl[cur_fnt].char_f)% 256)))/(double)(1L<< 20)) *(double) font_tbl[cur_fnt].scale)
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269
#define limit(x)
Definition: yuvsplittoppm.c:26
int main(argc, char **argv)
Definition: yuvsplittoppm.c:29