Tachyon (current)  Current Main Branch
tgatoyuv.c
Go to the documentation of this file.
1 /*
2  * tgatoyuv.c - converts .tga files to Abekas YUV format .yuv files..
3  *
4  * (C) Copyright 1994-2022 John E. Stone
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * $Id: tgatoyuv.c,v 1.5 2022/02/18 18:18:36 johns Exp $
8  *
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #ifdef MPI
16 #include <mpi.h>
17 #endif
18 
19 /* ONLY HANDLES 352x240 images!!! */
20 
21 #define TGASZ 253440
22 #define YUVSZ 168960
23 
24 void readtga(char * fname, unsigned char * mem) {
25  unsigned char hdr[40];
26  FILE * ifp;
27 
28  ifp = fopen(fname, "r");
29  if (ifp == NULL) {
30  printf("Error opening %s for input!\n", fname);
31  return;
32  }
33 
34  fread(hdr, 18, 1, ifp); /* read in tga file header */
35  fread(mem, TGASZ, 1, ifp);
36 
37  fclose(ifp);
38 }
39 
40 void writeyuv(char *fname, unsigned char * data) {
41  FILE * ofp;
42 
43  ofp = fopen(fname, "w+b");
44 
45  fwrite(data, YUVSZ, 1, ofp);
46 
47  fclose(ofp);
48 }
49 
50 void bgrtoyuv(unsigned char * bgr, unsigned char *yuv) {
51  int z,t;
52  unsigned char r,g,b;
53  unsigned long y1,y2,u,v,u0,u1,u2,v0,v1,v2;
54  unsigned char * yuvptr;
55 
56  y1=y2=u=v=u0=u1=u2=v0=v1=v2=0;
57 
58  yuvptr = yuv;
59  z=0;
60  for (t=0; t<(TGASZ / 3); t+=2) {
61 
62  /* first pel (gives Y and half of chroma) */
63  b = bgr[z*3 ]; /* blue */
64  g = bgr[z*3 + 1]; /* green */
65  r = bgr[z*3 + 2]; /* red */
66 
67  y1 = 16829 * r + 33039 * g + 6416 * b + (0xffff & y2);
68  u1 = -4853 * r - 9530 * g + 14383 * b;
69  v1 = 14386 * r - 12046 * g - 2340 * b;
70 
71  /* second pel (gives Y and quarter U and quarter V) */
72  z++;
73  b = bgr[z*3 ]; /* blue */
74  g = bgr[z*3 + 1]; /* green */
75  r = bgr[z*3 + 2]; /* red */
76  y2 = 16829 * r + 33039 * g + 6416 * b + (0xffff & y1);
77  u2 = -2426 * r - 4765 * g + 7191 * b;
78  v2 = 7193 * r - 6023 * g - 1170 * b;
79 
80  /* filter the chroma */
81  u = u0 + u1 + u2 + (0xffff & u);
82  v = v0 + v1 + v2 + (0xffff & v);
83 
84  u0 = u2;
85  v0 = v2;
86 
87  *yuvptr++ = (unsigned char) ((unsigned long) (u >> 16) + 128);
88  *yuvptr++ = (unsigned char) ((unsigned long) (y1 >> 16) + 16);
89  *yuvptr++ = (unsigned char) ((unsigned long) (v >> 16) + 128);
90  *yuvptr++ = (unsigned char) ((unsigned long) (y2 >> 16) + 16);
91 
92  /* next pel...*/
93  z++;
94  }
95 }
96 
97 int main (int argc, char **argv) {
98  int fs, fe;
99  int i;
100  unsigned char *tgadata, *yuvdata;
101  char tganame[800];
102  char yuvname[800];
103  int numnodes;
104  int mynode;
105 
106  mynode=0;
107  numnodes=1;
108 
109 #ifdef MPI
110  MPI_Init(&argc, &argv); /* initialize MPI */
111 
112  MPI_Comm_rank(MPI_COMM_WORLD, &mynode);
113  MPI_Comm_size(MPI_COMM_WORLD, &numnodes);
114 
115  if (mynode == 0)
116  printf("Parallel tga to yuv converter\n");
117 
118  MPI_Barrier(MPI_COMM_WORLD);
119 #endif
120 
121  tgadata = malloc(TGASZ);
122  yuvdata = malloc(YUVSZ);
123 
124  if (mynode == 0)
125  printf("Converting 352x240 .tga files to abekas YUV files...\n");
126 
127  if (argc != 4) {
128  if (mynode == 0)
129  printf("Usage is: tgatoyuv basename startframe# endframe# \n");
130  exit(0);
131  }
132 
133  if (mynode == 0)
134  printf("Using %s as base filename for frames %s -> %s\n",
135  argv[1], argv[2], argv[3]);
136 
137  fs = atoi(argv[2]);
138  fe = atoi(argv[3]);
139 
140  for (i=(fs + mynode); i<=fe; i+=numnodes) {
141  sprintf(tganame, "%s.%04d.tga", argv[1], i);
142  sprintf(yuvname, "%s.%04d.yuv", argv[1], i);
143  printf("converting %s -> %s . . .\n", tganame, yuvname);
144 
145  readtga(tganame, tgadata);
146  bgrtoyuv(tgadata, yuvdata);
147  writeyuv(yuvname, yuvdata);
148  }
149 
150 #ifdef MPI
151  MPI_Barrier(MPI_COMM_WORLD);
152 
153  if (mynode == 0)
154  printf("All nodes complete, all images converted.\n");
155 
156  MPI_Finalize(); /* close MPI */
157 #endif
158 
159  return 0;
160 }
161 
int main(int argc, char **argv)
Definition: tgatoyuv.c:97
#define TGASZ
Definition: tgatoyuv.c:21
void bgrtoyuv(unsigned char *bgr, unsigned char *yuv)
Definition: tgatoyuv.c:50
void readtga(char *fname, unsigned char *mem)
Definition: tgatoyuv.c:24
#define YUVSZ
Definition: tgatoyuv.c:22
void writeyuv(char *fname, unsigned char *data)
Definition: tgatoyuv.c:40