Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Data Structures | Macros | Functions | Variables
targatorle.c File Reference
#include <stdio.h>
#include <sys/types.h>
#include "rle.h"
Include dependency graph for targatorle.c:

Go to the source code of this file.

Data Structures

struct  targafile
 

Macros

#define HEADSIZE   18
 
#define GETSHORT(N)   (((unsigned short)(*(N+1))<<8) + (unsigned short)(*(N)))
 

Functions

void targa_getrow ()
 
void init_comp ()
 
unsigned char getbyte ()
 
void main (int argc, argv)
 

Variables

unsigned char * outrows [4]
 
unsigned char * redline
 
unsigned char * grnline
 
unsigned char * bluline
 
unsigned char * alfline
 
FILE * infile
 
int bwimage =0
 
int truecolor =0
 
int cmapped =0
 
int rlcomp =0
 
int nchannels
 
int cmapbytes
 
int pixwidth
 
struct targafile tga_head
 
int havedata =0
 
int rlcount
 
int bufinx
 
unsigned char rlbuf [4 *128]
 

Macro Definition Documentation

#define GETSHORT (   N)    (((unsigned short)(*(N+1))<<8) + (unsigned short)(*(N)))

Definition at line 78 of file targatorle.c.

#define HEADSIZE   18

Definition at line 77 of file targatorle.c.

Function Documentation

unsigned char getbyte ( )

Definition at line 461 of file targatorle.c.

References bufinx, havedata, infile, pixwidth, rlbuf, rlcomp, and rlcount.

462 {
463  int j,k;
464  int pkhdr;
465  unsigned char pixdata[4];
466  unsigned char *pp;
467  unsigned char next;
468 
469  if (!rlcomp)
470  {
471  next = (unsigned)getc(infile)&0xff;
472  return next;
473  }
474 
475  if (!havedata)
476  {
477  pkhdr = getc(infile);
478  rlcount = (pkhdr & 0x7f) + 1;
479 
480  if (pkhdr & 0x80) /* Run-length packet */
481  {
482  fread(pixdata,pixwidth,1,infile);
483  pp = rlbuf;
484  for(k=0; k<rlcount; k++)
485  for(j=0; j<pixwidth; j++)
486  *(pp++) = pixdata[j];
487  }
488  else /* Raw packet */
489  {
490  fread(rlbuf,pixwidth,rlcount,infile);
491  }
492  rlcount *= pixwidth;
493  bufinx = 0;
494  havedata = 1;
495  }
496 
497  next = rlbuf[bufinx];
498  if (++bufinx == rlcount) havedata = 0;
499 
500  return next;
501 }
int rlcomp
Definition: targatorle.c:107
unsigned char rlbuf[4 *128]
Definition: targatorle.c:116
int bufinx
Definition: targatorle.c:115
int pixwidth
Definition: targatorle.c:110
int havedata
Definition: targatorle.c:113
FILE * infile
Definition: targatorle.c:102
int rlcount
Definition: targatorle.c:114
void init_comp ( )

Definition at line 456 of file targatorle.c.

References havedata.

457 {
458  havedata = 0;
459 }
int havedata
Definition: targatorle.c:113
void main ( int  argc,
argv   
)

Definition at line 123 of file targatorle.c.

126 {
127  FILE *outfile;
128  FILE *hdrfile=NULL;
129  char *infname=NULL,
130  *outfname=NULL,
131  *hdrfname=NULL;
132  int oflag=0,
133  nflag=0,
134  hflag=0;
135  int i;
136  unsigned char headbuf[HEADSIZE];
138 
139  if ( scanargs( argc,argv,
140  "% h%-hdrfile!s n%-nchannels!d o%-outfile!s infile%s\n(\
141 \tConvert TARGA file to URT format.\n\
142 \t-h\tSpecify separate header file.\n\
143 \t-n\tNumber of channels to write, defaults to same as input.)",
144  &hflag, &hdrfname, &nflag, &nchannels,
145  &oflag, &outfname, &infname ) == 0 )
146  exit(1);
147 
148  out_hdr = *rle_hdr_init( (rle_hdr *)NULL );
149  rle_names( &out_hdr, cmd_name( argv ), outfname, 0 );
150 
151  infile = rle_open_f(out_hdr.cmd, infname, "r");
152  if ( hflag )
153  hdrfile = rle_open_f(out_hdr.cmd, hdrfname, "w");
154  outfile = rle_open_f(out_hdr.cmd, outfname, "w");
155 
156  /* Check that hdrfile and outfile aren't the same. */
157 
158  if ( hdrfile == outfile )
159  {
160  fprintf( stderr,
161  "%s: Can't write header and RLE data to same file.\n",
162  out_hdr.cmd );
163  exit( 1 );
164  }
165 
166  /* Read the image header */
167 
168  if (fread (headbuf, 1, HEADSIZE, infile)!=HEADSIZE)
169  {
170  fprintf(stderr,"targatorle: Read Error.\n");
171  exit(1);
172  }
173 
174  tga_head.num_char_id = headbuf[0];
175  tga_head.cmap_type = headbuf[1];
176  tga_head.image_type = headbuf[2];
177 
178  tga_head.cmap_origin = GETSHORT(headbuf+3);
179  tga_head.cmap_length = GETSHORT(headbuf+5);
180  tga_head.cmap_size = headbuf[7];
181 
182  tga_head.image_x_origin = GETSHORT(headbuf+8);
183  tga_head.image_y_origin = GETSHORT(headbuf+10);
184  tga_head.image_width = GETSHORT(headbuf+12);
185  tga_head.image_height = GETSHORT(headbuf+14);
186  tga_head.image_pix_size = headbuf[16];
187 
188  tga_head.image_descriptor = headbuf[17];
189 
190  if (hflag) {
191  fprintf (hdrfile, "num_char_id = %u \n", (unsigned)tga_head.num_char_id);
192  fprintf (hdrfile, "cmap_type = %u \n", (unsigned)tga_head.cmap_type);
193  fprintf (hdrfile, "image_type = %u \n", (unsigned)tga_head.image_type);
194  }
195 
196  /* Check Color Map Type. Currently, Truevision defines only 0 and 1 */
197 
198  if (tga_head.cmap_type==0) {
199  if (hflag) {
200  fprintf (hdrfile, "Color Map Type = 0 \n");
201  }
202  }
203  else if (tga_head.cmap_type==1) {
204  cmapped = 1;
205  if (hflag) {
206  fprintf (hdrfile, "cmap_origin = %u \n",
207  (unsigned)tga_head.cmap_origin);
208  fprintf (hdrfile, "cmap_length = %u \n",
209  (unsigned)tga_head.cmap_length);
210  fprintf (hdrfile, "cmap_size = %u \n",
211  (unsigned)tga_head.cmap_size);
212  }
213  }
214  else {
215  fprintf (stderr,
216  "%s: Invalid Color Map Type Code (%d)\n", out_hdr.cmd,
218  exit (1);
219  }
220 
221  if (hflag) {
222  fprintf (hdrfile, "image_x_origin = %u \n",
223  (unsigned)tga_head.image_x_origin);
224  fprintf (hdrfile, "image_y_origin = %u \n",
225  (unsigned)tga_head.image_y_origin);
226  fprintf (hdrfile, "image_width = %u \n",
227  (unsigned)tga_head.image_width);
228  fprintf (hdrfile, "image_height = %u \n",
229  (unsigned)tga_head.image_height);
230  fprintf (hdrfile, "image_pix_size = %u \n",
231  (unsigned)tga_head.image_pix_size);
232  }
233 
234  /* Is this a supported image type? */
235 
236  switch (tga_head.image_type)
237  {
238  /* Image type codes from imagefmt.h, */
239  /* by Truevision, Inc. */
240 
241  case 0: /* No Image Data */
242  fprintf(stderr,"%s: Image file is header only, no data.\n",
243  out_hdr.cmd);
244  exit(1);
245  break;
246 
247  case 2: /* Uncompressed Real-Color Image */
248 
249  /* Check for special case of a B/W */
250  /* pretending to be a color image */
251  if ((tga_head.image_pix_size==8) &&
252  (tga_head.cmap_length==0))
253  {
254  bwimage = 1;
255  nchannels = 1;
256  }
257  else
258  {
259  truecolor = 1;
260  }
261  break;
262 
263  case 3: /* Uncompressed B/W Image */
264  bwimage = 1;
265  nchannels = 1;
266  break;
267 
268  case 10: /* Run-length Encoded, Real-Color Image */
269  truecolor = 1;
270  rlcomp = 1;
271  break;
272 
273  case 11: /* Run-length Encoded, B/W Image */
274  bwimage = 1;
275  nchannels = 1;
276  rlcomp = 1;
277  break;
278 
279  case 1: /* Uncompressed, Color-Mapped Image */
280  case 9: /* Run-length Encoded, Color-Mapped Image */
281  default:
282  fprintf(stderr,"%s: Image type (%d) not supported by this program.\n",
283  out_hdr.cmd, tga_head.image_type);
284  exit(1);
285  }
286 
287  /* Check limits on what this program can handle */
288 
289  if (bwimage)
290  {
291  if (tga_head.image_pix_size!=8)
292  {
293  fprintf(stderr,"%s: B/W image pixel size must be 8.\n", out_hdr.cmd);
294  exit(1);
295  }
296  }
297  else if (truecolor)
298  {
299  if ( !((tga_head.image_pix_size==16) ||
300  (tga_head.image_pix_size==24) ||
301  (tga_head.image_pix_size==32) ))
302  {
303  fprintf(stderr,
304  "%s: Real-color image pixel size must be 16, 24 or 32.\n",
305  out_hdr.cmd);
306  exit(1);
307  }
308  if (tga_head.image_pix_size==24) nchannels = 3;
309  }
310 
311  /* Read image ID field */
312 
313  if ((i=(unsigned)tga_head.num_char_id)>=1) {
314  tga_head.image_id = (unsigned char *)malloc(i);
315  fread (tga_head.image_id, i, 1, infile);
316  }
317 
318  /* Read Color Map, if any. Do this even if the image is "true-color", since */
319  /* Truevision and other developers use the colormap area for their own needs. */
320 
321  if ((unsigned)tga_head.cmap_type==1) {
322  switch((unsigned)tga_head.cmap_size) {
323 
324  case 15: /* Typical values for colormap width */
325  case 16:
326  cmapbytes = 2;
327  break;
328  case 24:
329  cmapbytes = 3;
330  break;
331  case 32:
332  cmapbytes = 4;
333  break;
334 
335  default:
336  fprintf (stderr,"%s: Invalid color map width (%u bits)\n",
337  out_hdr.cmd, (unsigned)tga_head.cmap_size);
338  exit(1);
339  }
341  (unsigned char *)calloc((unsigned)tga_head.cmap_length,cmapbytes);
342  fread (tga_head.cmap_data, cmapbytes, (unsigned)tga_head.cmap_length, infile);
343 
344  }
345 
346  /* Build RLE image header */
347 
348  RLE_SET_BIT(out_hdr, RLE_RED);
349 
350  if (nchannels==1)
351  {
352  out_hdr.ncolors = 1;
353  }
354  else
355  {
356  RLE_SET_BIT(out_hdr, RLE_GREEN);
357  RLE_SET_BIT(out_hdr, RLE_BLUE);
358  out_hdr.ncolors = 3;
359 
360  if (nchannels==4) /* Save the alpha channel? */
361  {
362  RLE_SET_BIT(out_hdr, RLE_ALPHA);
363  out_hdr.alpha = 1;
364  }
365  }
366 
367  out_hdr.rle_file = outfile;
368  out_hdr.xmax = (unsigned)tga_head.image_width-1;
369  out_hdr.ymax = (unsigned)tga_head.image_height-1;
370 
371  /* Allocate pixel buffers */
372 
373  alfline = (unsigned char *)malloc(tga_head.image_width);
374  redline = (unsigned char *)malloc(tga_head.image_width);
375  grnline = (unsigned char *)malloc(tga_head.image_width);
376  bluline = (unsigned char *)malloc(tga_head.image_width);
377 
378  if ((!alfline) || (!redline) || (!grnline) || (!bluline))
379  {
380  fprintf(stderr,"%s: Image is too large for this program.\n",
381  out_hdr.cmd);
382  exit(1);
383  }
384 
385  outrows[0] = alfline;
386  outrows[1] = redline;
387  outrows[2] = grnline;
388  outrows[3] = bluline;
389 
390  rle_addhist( argv, (rle_hdr *)NULL, &out_hdr );
391  rle_put_setup( &out_hdr );
392 
393  /* Read and process each of the TARGA scan lines */
394 
395  pixwidth = ((unsigned)tga_head.image_pix_size+7)/8; /* Pixel size in bytes */
396  init_comp();
397  for (i=0;i<(unsigned)tga_head.image_height;i++)
398  {
399  targa_getrow();
400  rle_putrow (&outrows[1], (unsigned)tga_head.image_width, &out_hdr);
401  }
402  rle_puteof( &out_hdr );
403 
404  free(alfline);
405  free(redline);
406  free(grnline);
407  free(bluline);
408 }
int bwimage
Definition: targatorle.c:104
#define GETSHORT(N)
Definition: targatorle.c:78
#define RLE_SET_BIT(glob, bit)
Definition: rle.h:122
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
unsigned char * image_id
Definition: targatorle.c:96
FILE * outfile
Definition: giftorle.c:61
unsigned char image_pix_size
Definition: rletotarga.c:61
unsigned short image_width
Definition: rletotarga.c:57
unsigned char image_descriptor
Definition: rletotarga.c:61
unsigned char * bluline
Definition: targatorle.c:101
void rle_putrow(rle_pixel *rows[], int rowlen, rle_hdr *the_hdr)
int rlcomp
Definition: targatorle.c:107
#define RLE_GREEN
Definition: rle.h:63
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
int truecolor
Definition: targatorle.c:105
#define RLE_BLUE
Definition: rle.h:64
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
unsigned char cmap_type
Definition: rletotarga.c:51
unsigned char * alfline
Definition: targatorle.c:101
string infname
Definition: getbob.c:68
int cmapbytes
Definition: targatorle.c:109
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
void targa_getrow()
Definition: targatorle.c:410
const char * cmd
Definition: rle.h:133
Definition: rle.h:96
#define RLE_RED
Definition: rle.h:62
int xmax
Definition: rle.h:100
int pixwidth
Definition: targatorle.c:110
unsigned char * grnline
Definition: targatorle.c:101
int nchannels
Definition: targatorle.c:108
unsigned char image_type
Definition: rletotarga.c:51
unsigned short cmap_origin
Definition: rletotarga.c:54
#define HEADSIZE
Definition: targatorle.c:77
FILE * infile
Definition: targatorle.c:102
int ymax
Definition: rle.h:100
unsigned short image_x_origin
Definition: rletotarga.c:57
unsigned char * redline
Definition: targatorle.c:101
void * malloc()
int i
Definition: rletorla.c:82
int cmapped
Definition: targatorle.c:106
int alpha
Definition: rle.h:100
#define RLE_ALPHA
Definition: rle.h:65
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
unsigned short cmap_length
Definition: rletotarga.c:54
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
char * cmd_name(char **argv)
Definition: cmd_name.c:31
struct targafile tga_head
Definition: targatorle.c:111
unsigned char num_char_id
Definition: rletotarga.c:51
unsigned short image_height
Definition: rletotarga.c:57
void init_comp()
Definition: targatorle.c:456
int oflag
Definition: painttorle.c:45
unsigned char cmap_size
Definition: rletotarga.c:56
unsigned short image_y_origin
Definition: rletotarga.c:57
FILE * rle_file
Definition: rle.h:114
unsigned char * outrows[4]
Definition: targatorle.c:100
rle_hdr out_hdr
Definition: dvirle2.c:89
int ncolors
Definition: rle.h:100
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
unsigned char * cmap_data
Definition: targatorle.c:97
void targa_getrow ( )

Definition at line 410 of file targatorle.c.

References alfline, bluline, grnline, targafile::image_width, pixwidth, redline, and tga_head.

411 {
412  int j;
413  int numpix;
414  unsigned pxltmp;
415 
416  numpix = (unsigned)tga_head.image_width;
417 
418  switch (pixwidth) {
419 
420  case 1: /* 8-bit pixels */
421  for (j=0;j<numpix;j++) redline[j] = getbyte();
422  return;
423  break;
424 
425  case 2: /* 16-bit pixels */
426  for (j=0;j<numpix;j++)
427  {
428  pxltmp = (unsigned)getbyte();
429  pxltmp |= ((unsigned)getbyte())<<8;
430 
431  bluline[j] = (pxltmp & 0x1f) << 3;
432  grnline[j] = ((pxltmp >> 5) & 0x1f) << 3;
433  redline[j] = ((pxltmp >> 10) & 0x1f) << 3;
434  alfline[j] = ((pxltmp & 0x8000) ? 0xff : 0);
435  }
436  break;
437 
438  case 3: /* 24/32 bit pixels */
439  case 4:
440  for (j=0;j<numpix;j++)
441  {
442  bluline[j]=getbyte();
443  grnline[j]=getbyte();
444  redline[j]=getbyte();
445  if (pixwidth==4)
446  alfline[j]=getbyte();
447  }
448  break;
449 
450  default:
451  fprintf(stderr,"targatorle: Invalid pixel size (%d bytes) for this program.\n",
452  pixwidth);
453  }
454 }
unsigned short image_width
Definition: rletotarga.c:57
unsigned char * bluline
Definition: targatorle.c:101
unsigned char getbyte()
Definition: targatorle.c:461
unsigned char * alfline
Definition: targatorle.c:101
int pixwidth
Definition: targatorle.c:110
unsigned char * grnline
Definition: targatorle.c:101
unsigned char * redline
Definition: targatorle.c:101
struct targafile tga_head
Definition: targatorle.c:111

Variable Documentation

unsigned char * alfline

Definition at line 101 of file targatorle.c.

Referenced by targa_getrow().

unsigned char * bluline

Definition at line 101 of file targatorle.c.

Referenced by targa_getrow().

int bufinx

Definition at line 115 of file targatorle.c.

Referenced by getbyte().

int bwimage =0

Definition at line 104 of file targatorle.c.

int cmapbytes

Definition at line 109 of file targatorle.c.

int cmapped =0

Definition at line 106 of file targatorle.c.

unsigned char * grnline

Definition at line 101 of file targatorle.c.

Referenced by targa_getrow().

int havedata =0

Definition at line 113 of file targatorle.c.

Referenced by getbyte(), and init_comp().

FILE* infile

Definition at line 102 of file targatorle.c.

Referenced by getbyte().

int nchannels

Definition at line 108 of file targatorle.c.

unsigned char* outrows[4]

Definition at line 100 of file targatorle.c.

int pixwidth

Definition at line 110 of file targatorle.c.

Referenced by getbyte(), and targa_getrow().

unsigned char* redline

Definition at line 101 of file targatorle.c.

Referenced by targa_getrow().

unsigned char rlbuf[4 *128]

Definition at line 116 of file targatorle.c.

Referenced by getbyte().

int rlcomp =0

Definition at line 107 of file targatorle.c.

Referenced by getbyte().

int rlcount

Definition at line 114 of file targatorle.c.

Referenced by getbyte().

struct targafile tga_head

Definition at line 111 of file targatorle.c.

Referenced by targa_getrow().

int truecolor =0

Definition at line 105 of file targatorle.c.