Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rletoabA60.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * rletoabA60.c - Convert rle images into 4:2:2 yuv format for Abekas A60
20  *
21  * Author: T. Todd Elvins
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Fri June 3 1988
25  * Copyright (c) 1988, University of Utah
26  *
27  */
28 #if 0
29 rletoabA60() /* For tags. */
30 #endif
31 
32 #include <stdio.h>
33 #include <math.h>
34 #include "rle.h"
35 
36 #ifndef ABEKAS_PAL
37 #define LINE_LENGTH 720
38 #define FRAME_LENGTH 486
39 #else
40 #define LINE_LENGTH 720
41 #define FRAME_LENGTH 576
42 #endif
43 
44 #define shifty(val) y4=y3;y3=y2;y2=y1;y1=y0;y0=(val)
45 #define shiftu(val) u4=u3;u3=u2;u2=u1;u1=u0;u0=(val)
46 #define shiftv(val) v4=v3;v3=v2;v2=v1;v1=v0;v0=(val)
47 
48 void read_image(), send_image();
49 
50 rle_hdr hdr;
54 
55 void
57 int argc;
58 char **argv;
59 {
60  char *infname = NULL,
61  *out_fname = NULL;
62  int cflag = 0,
63  Pflag = 0,
64  pflag = 0,
65  oflag = 0;
66  register int i, xrun, maxy;
67  int minx, maxx, miny, yrun, px, py;
68  int ix, iy;
69  rle_pixel *scanbuf[3];
70  FILE *outfile;
71 
72  if ( scanargs( argc, argv,
73  "% c%- P%-incx!dincy!d p%-posx!dposy!d o%-outfile!s infile%s\n(\
74 \t-c\tCenter image in Abekas frame.\n\
75 \t-p\tPosition image at given position.\n\
76 \t-P\tIncrementally position image by given offset.)",
77  &cflag,
78  &Pflag, &ix, &iy,
79  &pflag, &px, &py, &oflag, &out_fname, &infname ) == 0 )
80  exit(3);
81 
82  /* Initialize header. */
83  hdr = *rle_hdr_init( (rle_hdr *)NULL );
84  rle_names( &hdr, cmd_name( argv ), infname, 0 );
85 
86  if ( cflag + pflag + Pflag > 1 ) {
87  fprintf( stderr, "%s: specify exactly one of -c -p -P\n", hdr.cmd );
88  exit(3);
89  }
90  hdr.rle_file = rle_open_f(hdr.cmd, infname, "r");
91  outfile = rle_open_f(hdr.cmd, out_fname, "w");
92  /*
93  * Do the rle initialization stuff
94  */
95  rle_get_setup_ok( &hdr, NULL, NULL );
96 
98  fprintf( stderr,"%s: rle image too big for Abekas\n", hdr.cmd);
99  exit(3);
100  }
102  for (i = 3 ; i < hdr.ncolors ; i++)
103  RLE_CLR_BIT( hdr, i );
104 
105  /*
106  * Initialize some variables.
107  */
108  bzero( scanred, LINE_LENGTH * FRAME_LENGTH );
109  bzero( scangrn, LINE_LENGTH * FRAME_LENGTH );
110  bzero( scanblu, LINE_LENGTH * FRAME_LENGTH );
111 
112  for (i=0; i<3; i++)
113  scanbuf[i] = (rle_pixel *) malloc ( LINE_LENGTH * sizeof(rle_pixel));
114 
115  /*
116  * Put the entire rle image into a buffer upside down and centered.
117  */
118  xrun = hdr.xmax - hdr.xmin + 1;
119  yrun = hdr.ymax - hdr.ymin + 1;
120  if ( cflag ) {
121  minx = (LINE_LENGTH - xrun) / 2;
122  miny = (FRAME_LENGTH - yrun) / 2;
123  }
124  else if ( pflag ) {
125  minx = px;
126  miny = FRAME_LENGTH - (yrun + py);
127  }
128  else if ( Pflag ) {
129  minx = hdr.xmin + ix;
130  miny = FRAME_LENGTH - (yrun + hdr.ymin + iy);
131  }
132  else {
133  minx = hdr.xmin;
134  miny = FRAME_LENGTH - (yrun + hdr.ymin);
135  }
136 
137  maxx = minx + xrun - 1;
138  maxy = miny + yrun - 1;
139 
140  if ( maxx > LINE_LENGTH-1 )
141  xrun = LINE_LENGTH - minx;
142 
143  read_image( yrun, scanbuf, minx, maxy, xrun );
144  send_image( outfile );
145 
146  exit(0);
147 }
148 
149 void
150 read_image ( yrun, scanbuf, minx, maxy, xrun )
151 int yrun;
152 rle_pixel *scanbuf[3];
153 int minx, maxy, xrun;
154 {
155  register int line, upsidedowny;
156 
157  for( line = 0; line < yrun; line++ ) {
158  rle_getrow( &hdr, scanbuf );
159  upsidedowny = maxy - line;
160 
161  if ( ( upsidedowny > FRAME_LENGTH-1 ) || ( upsidedowny < 0 ) )
162  continue;
163 
164  bcopy( scanbuf[RLE_RED], &scanred[upsidedowny][minx], xrun );
165  bcopy( scanbuf[RLE_GREEN], &scangrn[upsidedowny][minx], xrun );
166  bcopy( scanbuf[RLE_BLUE], &scanblu[upsidedowny][minx], xrun );
167  }
168 
169 }
170 
171 void
172 send_image ( outfile )
173 FILE *outfile;
174 {
175  unsigned char buf[LINE_LENGTH*5];
176  register unsigned char *bp;
177  int line, col;
178  register rle_pixel *sr, *sg, *sb;
179  register int r, g, b;
180  int y, u, v;
181  int y0=0, y1=0, y2=0, y3=0, y4=0;
182  int u0=0, u1=0, u2=0, u3=0, u4=0;
183  int v0=0, v1=0, v2=0, v3=0, v4=0;
184 
185  /*
186  * Convert the 720x486 rle image to yuv one byte at a time.
187  *
188  * All arithmetic is performed in "fixed point" integer for speed.
189  * The original equations are shown in comments.
190  */
191  for( line=0; line < FRAME_LENGTH; line++ ) {
192  sr = scanred[line];
193  sg = scangrn[line];
194  sb = scanblu[line];
195  bp = buf;
196 
197  for( col=0; col < LINE_LENGTH; col+=2 ) {
198  if ( *sr || *sg || *sb )
199  {
200  /*
201  * r = *sr++ / 255.0;
202  * g = *sg++ / 255.0;
203  * b = *sb++ / 255.0;
204  */
205  r = (*sr++ * 8000000) / 255;
206  g = (*sg++ * 8000000) / 255;
207  b = (*sb++ * 8000000) / 255;
208 
209  /*
210  * shifty( ( 0.2990 * r + 0.5870 * g + 0.1140 * b));
211  * shiftu( (-0.1686 * r - 0.3311 * g + 0.4997 * b));
212  * shiftv( ( 0.4998 * r - 0.4185 * g - 0.0813 * b));
213  */
214  shifty( ( r / 33445 + g / 17036 + b / 87719) * 1000000 );
215  shiftu( (-r / 59312 - g / 30202 + b / 20012) * 1000000 );
216  shiftv( ( r / 20012 - g / 23895 - b / 123001) * 1000000 );
217  }
218  else
219  {
220  sr++; sg++; sb++;
221  shifty( 0 ); shiftu( 0 ); shiftv( 0 );
222  }
223 
224  /*
225  * u = ( (0.14963 * u0) + (0.22010 * u1)
226  * +(0.26054 * u2) + (0.22010 * u3)
227  * +(0.14963 * u4)) * 224;
228  */
229  u = ( (u0 / 66832) + (u1 / 45434)
230  +(u2 / 38382) + (u3 / 45434)
231  +(u4 / 66832)) * 224 / 80000;
232  *bp++ = u + 128;
233 
234  /*
235  * y = (-(0.05674 * y0) + (0.01883 * y1)
236  * +(1.07582 * y2) + (0.01883 * y3)
237  * -(0.05674 * y4)) * 219.0;
238  */
239  y = (-(y0 / 176243) + (y1 / 531067)
240  +(y1 / 9295) + (y3 / 531067)
241  -(y4 / 176243)) * 219 / 80000;
242  *bp++ = y + 16;
243 
244  /*
245  * v = ((0.14963 * v0) + (0.22010 * v1)
246  * +(0.26054 * v2) + (0.22010 * v3)
247  * +(0.14963 * v4)) * 224.0;
248  */
249  v = ( (v0 / 66832) + (v1 / 45434)
250  +(v2 / 38382) + (v3 / 45434)
251  +(v4 / 66832)) * 224 / 80000;
252  *bp++ = v + 128;
253 
254  if ( *sr || *sg || *sb )
255  {
256  r = (*sr++ * 8000000) / 255;
257  g = (*sg++ * 8000000) / 255;
258  b = (*sb++ * 8000000) / 255;
259 
260  shifty( ( r / 33445 + g / 17036 + b / 87719) * 1000000 );
261  shiftu( (-r / 59312 - g / 30202 + b / 20012) * 1000000 );
262  shiftv( ( r / 20012 - g / 23895 - b / 123001) * 1000000 );
263  }
264  else
265  {
266  sr++; sg++; sb++;
267  shifty( 0 ); shiftu( 0 ); shiftv( 0 );
268  }
269 
270  y = (-(y0 / 176243) + (y1 / 531067)
271  +(y1 / 9295) + (y3 / 531067)
272  -(y4 / 176243)) * 219 / 80000;
273  *bp++ = y + 16;
274  }
275 
276  fwrite( buf, 1, bp - buf, outfile );
277  }
278 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
rle_hdr hdr
Definition: getx10.c:84
int xmin
Definition: rle.h:100
#define shiftv(val)
Definition: rletoabA60.c:46
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
#define RLE_GREEN
Definition: rle.h:63
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
rle_pixel scanred[486][720]
Definition: rletoabA60.c:51
int ymin
Definition: rle.h:100
#define RLE_BLUE
Definition: rle.h:64
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
const char * cmd
Definition: rle.h:133
#define RLE_RED
Definition: rle.h:62
int xmax
Definition: rle.h:100
#define shiftu(val)
Definition: rletoabA60.c:45
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
Definition: rle_getrow.c:254
#define RLE_CLR_BIT(glob, bit)
Definition: rle.h:124
rle_pixel scangrn[486][720]
Definition: rletoabA60.c:53
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
#define shifty(val)
Definition: rletoabA60.c:44
#define RLE_ALPHA
Definition: rle.h:65
rle_pixel scanblu[486][720]
Definition: rletoabA60.c:52
#define FRAME_LENGTH
Definition: rletoabA60.c:38
#define LINE_LENGTH
Definition: rletoabA60.c:37
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100