Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rletotiff.c
Go to the documentation of this file.
1 /* rletotiff.c */
2 
3 /*************************************************************************
4  * rletotiff - Main program for RLE to TIFF graphics format conversion.
5  *
6  * Written by Bailey Brown, Jr. June 4, 1990.
7  *************************************************************************
8  */
9 
10 #define NO_DECLARE_MALLOC /* tiffcompat.h declares it. */
11 #include "rle.h"
12 #undef TIFF /* Defined in rle_config.h and tiffio.h. */
13 #ifndef USE_STDARG
14 #define USE_VARARGS 1 /* Needed by tiffcompat.h. */
15 #endif
16 #ifdef USE_PROTOTYPES
17 #undef USE_PROTOTYPES
18 #define USE_PROTOTYPES 1 /* Needs to have a value for tiffcompat.h. */
19 #endif
20 
21 #include <stdio.h>
22 #include "tiffio.h"
23 
24 #define SETFIELD(tif, fieldname, arg) TIFFSetField(tif,fieldname,arg)
25 typedef unsigned char tiff_sample;
26 
27 #ifdef USE_PROTOTYPES
28 void error(CONST_DECL char *s);
29 void get_tiff_scanline( tiff_sample* buf, int row);
30 void usage(void);
31 #else
32 void error();
33 void get_tiff_scanline();
34 void usage();
35 #endif
36 void get_scanlines();
37 
38 static rle_hdr in_hdr;
39 /* needed to read all scanlines before converting
40  to tiff (rle is upside down) */
45 
46 TIFF *tif;
47 
48 static unsigned short compression = COMPRESSION_LZW;
49 static int flip = 0;
50 
51 void
53 int argc;
54 char *argv[];
55 {
56  int i;
57  long int rows_per_strip;
58  int cflag = 0;
59  char *outfname = NULL, *infname = NULL;
60 
61  in_hdr = *rle_hdr_init( (rle_hdr *)NULL );
62 
63  if (sizeof(rle_pixel) != sizeof(tiff_sample))
64  error("tiff_sample and rle_pixel not same size");
65 
66  if ( scanargs( argc, argv, "% Cc%- f%- o!-outfile.tiff!s infile.rle%s\n(\
67 \tConvert URT image to 24-bit TIFF image.\n\
68 \t-c\tUse LZW compression (default)\n\
69 \t-C\tUse no compression\n\
70 \t-f\tDon't flip image top to bottom\n\
71 \t-o outfile.tiff is required)",
72  &cflag, &flip, &i, &outfname, &infname ) == 0 )
73  exit( 1 );
74 
75  if ( cflag == 1 )
76  compression = COMPRESSION_LZW;
77  else if ( cflag == 2 )
78  compression = COMPRESSION_NONE;
79 
80  rle_names( &in_hdr, cmd_name( argv ), infname, 0 );
83 
85  in_hdr.xmin = 0;
86  if (in_hdr.ncolors != 3) error("infile must be 24-bit RGB");
87  /* No alpha channel, either. */
89  get_scanlines();
90 /* rletotiff specific code comes after here */
91  RGBscanline = (tiff_sample*)malloc(
92  sizeof(tiff_sample)*3*(in_hdr.xmax+1));
93  if (RGBscanline == NULL) error("can't allocate RGB scanline");
94  tif = TIFFOpen(outfname, "w");
95  if (!tif) error("tiffopen ret null");
96 #ifndef TIFF2p4
97  SETFIELD(tif,TIFFTAG_IMAGEWIDTH, in_hdr.xmax + 1);
98  SETFIELD(tif,TIFFTAG_IMAGELENGTH, in_hdr.ymax - in_hdr.ymin + 1);
99 #else /* TIFF2p4 */
100  SETFIELD(tif,TIFFTAG_IMAGEWIDTH, (unsigned long)(in_hdr.xmax + 1));
101  SETFIELD(tif,TIFFTAG_IMAGELENGTH,
102  (unsigned long)(in_hdr.ymax - in_hdr.ymin + 1));
103 #endif /* TIFF2p4 */
104  SETFIELD(tif, TIFFTAG_COMPRESSION, compression);
105  rows_per_strip = (long)8*1024/(in_hdr.xmax+1);
106  if (rows_per_strip < 1) rows_per_strip = 1L;
107  SETFIELD(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip);
108  SETFIELD(tif, TIFFTAG_XRESOLUTION, 1.0);
109  SETFIELD(tif, TIFFTAG_YRESOLUTION, 1.0);
110  SETFIELD(tif, TIFFTAG_RESOLUTIONUNIT, 1);
111  SETFIELD(tif, TIFFTAG_BITSPERSAMPLE, 8);
112  SETFIELD(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
113  SETFIELD(tif, TIFFTAG_PLANARCONFIG, 1);
114  SETFIELD(tif, TIFFTAG_PHOTOMETRIC, 2);
115  for (i = 0; i < in_hdr.ymax - in_hdr.ymin + 1; i++) {
116  get_tiff_scanline(RGBscanline, i);
117  TIFFWriteScanline(tif, RGBscanline, i, 1);
118  }
119  TIFFClose(tif);
120 /* rletotiff specific code comes before here */
121  for (i = 0; i < in_hdr.ymax - in_hdr.ymin + 1; i++ ) {
122  free(scan_red[i]);
123  free(scan_green[i]);
124  free(scan_blue[i]);
125  }
126  free(scan_red);
127  free(scan_green);
128  free(scan_blue);
129 }
130 
131 /*
132  * get_scanlines() reads in all RLE raster data at once. This is
133  * necessary because of RLE stores images bottom to top and TIFF stores
134  * them top to bottom.
135  */
136 void
137 get_scanlines()
138 {
139  int i,j;
140  rle_pixel **scan;
141 
142  scan = (rle_pixel **)malloc( in_hdr.ncolors * sizeof(rle_pixel *) );
143  if ( scan == 0 ) error("can't allocate scan");
144  scan_red = (rle_pixel**)malloc(sizeof(rle_pixel*)*
146  scan_green = (rle_pixel**)malloc(sizeof(rle_pixel*)*
148  scan_blue = (rle_pixel**)malloc(sizeof(rle_pixel*)*
150  if (!(scan_red && scan_green && scan_blue))
151  error("can't allocate a scan buffer");
152  if (flip) {
153  for (i = 0; i < in_hdr.ymax-in_hdr.ymin+1; i++) {
154  for (j=0; j < 3; j++) {
155  scan[j] = (rle_pixel*)malloc(sizeof(rle_pixel)*(in_hdr.xmax+1));
156  if (scan[j] == NULL) error("can't allocate current scanline");
157  }
158  rle_getrow(&in_hdr, scan);
159  scan_red[i] = scan[0];
160  scan_green[i] = scan[1];
161  scan_blue[i] = scan[2];
162  for (j=0; j < 3; j++) {
163  scan[j] = (rle_pixel*)malloc(sizeof(rle_pixel)*(in_hdr.xmax+1));
164  if (scan[j] == NULL) error("can't allocate current scanline");
165  }
166  }
167  } else {
168  for (i = 0; i < in_hdr.ymax-in_hdr.ymin+1; i++) {
169  for (j=0; j < 3; j++) {
170  scan[j] = (rle_pixel*)malloc(sizeof(rle_pixel)*(in_hdr.xmax+1));
171  if (scan[j] == NULL) error("can't allocate current scanline");
172  }
173  rle_getrow(&in_hdr, scan);
174  scan_red[in_hdr.ymax - in_hdr.ymin - i] = scan[0];
175  scan_green[in_hdr.ymax - in_hdr.ymin - i] = scan[1];
176  scan_blue[in_hdr.ymax - in_hdr.ymin - i] = scan[2];
177  }
178  }
179  free( scan );
180 }
181 
182 void
183 get_tiff_scanline( buf, row )
184 tiff_sample *buf;
185 int row;
186 {
187  register int i;
188  register int k;
189 
190  for (i=k=0; i < in_hdr.xmax+1; i++, k +=3) {
191  buf[k] = scan_red[row][i];
192  buf[k+1] = scan_green[row][i];
193  buf[k+2] = scan_blue[row][i];
194  }
195 }
196 
197 void
198 error(s)
199 CONST_DECL char *s;
200 {
201  fprintf(stderr,"%s: %s\n", in_hdr.cmd, s);
202  exit(2);
203 }
204 
205 void
206 usage()
207 {
208  fprintf(stderr,"Usage:\n");
209  fprintf(stderr,"\trletotiff [options] infile outfile\n");
210  fprintf(stderr,"options:\n");
211  fprintf(stderr,"\t-c lzw or -c none (compression, default lzw)\n");
212  fprintf(stderr,"\t-v (vertical flip, default not)\n");
213  exit(2);
214 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
static rle_pixel ** scan_green
Definition: rletotiff.c:42
static tiff_sample * RGBscanline
Definition: rletotiff.c:44
#define USE_STDARG
Definition: rle_config.h:24
int xmin
Definition: rle.h:100
TIFF * tif
Definition: tifftorle.c:33
#define USE_PROTOTYPES
Definition: rle_config.h:22
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
static rle_hdr in_hdr
Definition: rletotiff.c:38
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
static unsigned short compression
Definition: rletotiff.c:48
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
int ymin
Definition: rle.h:100
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
const char * cmd
Definition: rle.h:133
static rle_pixel ** scan_blue
Definition: rletotiff.c:43
int xmax
Definition: rle.h:100
#define SETFIELD(tif, fieldname, arg)
Definition: rletotiff.c:24
static rle_pixel ** scan_red
Definition: rletotiff.c:41
#define CONST_DECL
Definition: rle_config.h:42
unsigned char tiff_sample
Definition: rletotiff.c:25
static int flip
Definition: rletotiff.c:49
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
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
#define RLE_ALPHA
Definition: rle.h:65
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