Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rletotarga.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  * Copyright (c) 1986, John W. Peterson
19  */
20 
21 /*
22  * rletotarga.c - Convert RLE files into Truevision TARGA format.
23  * Modified from targatorle.c, with help from rletogray.c
24  *
25  * By: Andrew Hadenfeldt
26  * Department of Electrical Engineering
27  * University of Nebraska-Lincoln
28  * 16-Mar-1991
29  *
30  * Usage:
31  *
32  * rletotarga [infile] outfile
33  *
34  * If no input filename is given, input is take from stdin.
35  *
36  * Since RLE and TARGA formats can agree on which end of the image is up, we
37  * do not need to flip the image.
38  */
39 
40 #include <stdio.h>
41 #include <sys/types.h>
42 #include "rle.h"
43 
44 #define PUTBYTE(N) putc((N)&0xff,outfile)
45 #define PUTSHORT(N) (putc((N)&0xff,outfile),putc(((N)>>8)&0xff,outfile))
46 
47 /*
48  * Description of header for files containing TARGA images
49  */
50 struct targafile {
51  unsigned char num_char_id, /* Number of Characters in ID Field */
52  cmap_type, /* Color Map Type */
53  image_type; /* Image Type Code */
54  unsigned short cmap_origin, /* Color Map Origin */
55  cmap_length; /* Color Map Length */
56  unsigned char cmap_size; /* Color Map Entry Size */
57  unsigned short image_x_origin, /* X-Origin of Image */
58  image_y_origin, /* Y-Origin of Image */
59  image_width, /* Width of Image */
60  image_height; /* Height of Image */
61  unsigned char image_pix_size, /* Image Pixel Size */
62  image_descriptor; /* Image Descriptor Byte */
63  };
64 
65 void
67 int argc;
68 char *argv[];
69 {
70  FILE *outfile;
71  char *infname=NULL,
72  *outfname=NULL;
73  int aflag=0;
74  unsigned i, j;
75  rle_hdr hdr;
76  rle_pixel **inprows;
77  struct targafile tga_head;
78 
79  if ( scanargs( argc, argv, "% o!-outfile!s infile%s\n(\
80 \tConvert RLE file to TARGA format.\n\
81 \tNote that output file name is required.)",&i,&outfname,&infname)==0)
82  exit( 1 );
83 
84  hdr = *rle_hdr_init( (rle_hdr *)NULL );
85  rle_names( &hdr, cmd_name( argv ), infname, 0 );
86 
87  /* Open specified input file (or stdin if none) */
88  hdr.rle_file = rle_open_f(hdr.cmd, infname, "r");
89  outfile = rle_open_f(hdr.cmd, outfname, "w");
90 
91  /* Read the image header */
92 
93  rle_get_setup_ok(&hdr, NULL, NULL);
94 
95  /* Fill in the TARGA image header */
96 
97  tga_head.num_char_id = 0;
98  tga_head.cmap_type = 0;
99 
100  tga_head.cmap_origin = 0;
101  tga_head.cmap_length = 0;
102  tga_head.cmap_size = 0;
103 
104  tga_head.image_x_origin = 0;
105  tga_head.image_y_origin = 0;
106  tga_head.image_width = hdr.xmax - hdr.xmin + 1;
107  tga_head.image_height = hdr.ymax - hdr.ymin + 1;
108 
109  tga_head.image_descriptor = 0;
110 
111  switch (hdr.ncolors)
112  {
113  case 1: /* Black and White image type */
114  tga_head.image_type = 3;
115  tga_head.image_pix_size = 8;
116  break;
117 
118  case 3: /* Real-Color image */
119  tga_head.image_type = 2;
120  tga_head.image_pix_size = 24;
121  if (hdr.alpha)
122  {
123  aflag = 1;
124  tga_head.image_pix_size = 32;
125  tga_head.image_descriptor = 0x08; /* Indicate 8 alpha bits */
126  }
127  break;
128 
129  default:
130  fprintf(stderr,"%s: Invalid number of color channels (%d).\n",
131  hdr.cmd, hdr.ncolors);
132  exit(1);
133  }
134 
135  /* Allocate pixel buffer */
136 
137  if (rle_row_alloc(&hdr,&inprows))
138  {
139  fprintf(stderr,"%s: Not enough memory.\n", hdr.cmd);
140  exit(1);
141  }
142 
143  /* Write the TARGA image header */
144 
145  PUTBYTE(tga_head.num_char_id);
146  PUTBYTE(tga_head.cmap_type);
147  PUTBYTE(tga_head.image_type);
148 
149  PUTSHORT(tga_head.cmap_origin);
150  PUTSHORT(tga_head.cmap_length);
151  PUTBYTE(tga_head.cmap_size);
152 
153  PUTSHORT(tga_head.image_x_origin);
154  PUTSHORT(tga_head.image_y_origin);
155  PUTSHORT(tga_head.image_width);
156  PUTSHORT(tga_head.image_height);
157  PUTBYTE(tga_head.image_pix_size);
158 
159  PUTBYTE(tga_head.image_descriptor);
160 
161  /* Process the RLE image data */
162 
163  for (i=0;i<tga_head.image_height;i++)
164  {
165  rle_getrow(&hdr,inprows);
166 
167  if (tga_head.image_pix_size==8)
168  fwrite(inprows[0],1,tga_head.image_width,outfile);
169  else
170  for (j=0;j<tga_head.image_width;j++)
171  {
172  PUTBYTE(inprows[2][j]); /* Blue plane */
173  PUTBYTE(inprows[1][j]); /* Green plane */
174  PUTBYTE(inprows[0][j]); /* Red plane */
175  if (aflag) PUTBYTE(inprows[-1][j]); /* Alpha plane */
176  }
177  }
178 
179  fclose(hdr.rle_file);
180  fclose(outfile);
181 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
int xmin
Definition: rle.h:100
unsigned char image_pix_size
Definition: rletotarga.c:61
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
unsigned short image_width
Definition: rletotarga.c:57
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
unsigned char image_descriptor
Definition: rletotarga.c:61
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
int ymin
Definition: rle.h:100
unsigned char cmap_type
Definition: rletotarga.c:51
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
#define PUTSHORT(N)
Definition: rletotarga.c:45
#define PUTBYTE(N)
Definition: rletotarga.c:44
const char * cmd
Definition: rle.h:133
int xmax
Definition: rle.h:100
unsigned char image_type
Definition: rletotarga.c:51
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
Definition: rle_getrow.c:254
unsigned short cmap_origin
Definition: rletotarga.c:54
int ymax
Definition: rle.h:100
unsigned short image_x_origin
Definition: rletotarga.c:57
unsigned char rle_pixel
Definition: rle.h:56
int alpha
Definition: rle.h:100
unsigned short cmap_length
Definition: rletotarga.c:54
unsigned char num_char_id
Definition: rletotarga.c:51
unsigned short image_height
Definition: rletotarga.c:57
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
unsigned char cmap_size
Definition: rletotarga.c:56
unsigned short image_y_origin
Definition: rletotarga.c:57
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100