Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rle.h
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  * rle.h - Global declarations for Utah Raster Toolkit RLE programs.
20  *
21  * Author: Todd W. Fuqua
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Sun Jul 29 1984
25  * Copyright (c) 1984 Todd W. Fuqua
26  *
27  * $Id: rle.h,v 3.0.1.5 1992/04/30 14:05:56 spencer Exp $
28  */
29 
30 #ifndef RLE_H
31 #define RLE_H
32 
33 #include "rle_config.h" /* Configuration parameters. */
34 
35 #include <stdio.h> /* Declare FILE. */
36 
37 #ifdef c_plusplus
38 #define USE_PROTOTYPES
39 #endif
40 #ifndef CONST_DECL
41 #define CONST_DECL
42 #endif
43 
47 };
48 
49 /* ****************************************************************
50  * TAG( rle_pixel rle_map )
51  *
52  * Typedef for 8-bit (or less) pixel data.
53  *
54  * Typedef for 16-bit color map data.
55  */
56 typedef unsigned char rle_pixel;
57 typedef unsigned short rle_map;
58 
59 /*
60  * Defines for traditional channel numbers.
61  */
62 #define RLE_RED 0 /* Red channel traditionally here. */
63 #define RLE_GREEN 1 /* Green channel traditionally here. */
64 #define RLE_BLUE 2 /* Blue channel traditionally here. */
65 #define RLE_ALPHA -1 /* Alpha channel here. */
66 
67 /*
68  * Return values from rle_get_setup.
69  */
70 #define RLE_SUCCESS 0
71 #define RLE_NOT_RLE -1
72 #define RLE_NO_SPACE -2
73 #define RLE_EMPTY -3
74 #define RLE_EOF -4
75 
76 /*
77  * "Magic" value for is_init field. Pi * 2^29.
78  */
79 #define RLE_INIT_MAGIC 0x6487ED51L
80 
81 /*****************************************************************
82  * TAG( RLE_CHECK_ALLOC )
83  *
84  * Test for allocation failure, scream and die if so.
85  */
86 #define RLE_CHECK_ALLOC( pgm, ptr, name )
87  ( !(ptr) ? rle_alloc_error( pgm, name ) : 0 )
88 
89 /*
90  * TAG( rle_hdr )
91  *
92  * Definition of header structure used by RLE routines.
93  */
94 
95 #ifndef c_plusplus
96 typedef
97 #endif
98 struct rle_hdr {
99  enum rle_dispatch dispatch; /* Type of file to create. */
100  int ncolors, /* Number of color channels. */
101  *bg_color, /* Pointer to bg color vector. */
102  alpha, /* If !0, save alpha channel. */
103  background, /* 0->just save all pixels, */
104  /* 1->overlay, 2->clear to bg first. */
105  xmin, /* Lower X bound (left.) */
106  xmax, /* Upper X bound (right.) */
107  ymin, /* Lower Y bound (bottom.) */
108  ymax, /* Upper Y bound (top.) */
109  ncmap, /* Number of color channels in color map. */
110  /* Map only saved if != 0. */
111  cmaplen; /* Log2 of color map length. */
112  rle_map *cmap; /* Pointer to color map array. */
113  CONST_DECL char **comments; /* Pointer to array of pointers to comments. */
114  FILE *rle_file; /* Input or output file. */
115  /*
116  * Bit map of channels to read/save. Indexed by (channel mod 256).
117  * Alpha channel sets bit 255.
118  *
119  * Indexing (0 <= c <= 255):
120  * bits[c/8] & (1 << (c%8))
121  */
122 #define RLE_SET_BIT(glob,bit)
123  ((glob).bits[((bit)&0xff)/8] |= (1<<((bit)&0x7)))
124 #define RLE_CLR_BIT(glob,bit)
125  ((glob).bits[((bit)&0xff)/8] &= ~(1<<((bit)&0x7)))
126 #define RLE_BIT(glob,bit)
127  ((glob).bits[((bit)&0xff)/8] & (1<<((bit)&0x7)))
128  char bits[256/8];
129  /* Set to magic pattern if following fields are initialized. */
130  /* This gives a 2^(-32) chance of missing. */
131  long int is_init;
132  /* Save the command, file name and image number for error messages. */
135  int img_num;
136  /*
137  * Local storage for rle_getrow & rle_putrow.
138  * rle_getrow has
139  * scan_y int current Y scanline.
140  * vert_skip int number of lines to skip.
141  * rle_putrow has
142  * nblank int number of blank lines.
143  * brun short(*)[2] Array of background runs.
144  * fileptr long Position in output file.
145  */
146  union {
147  struct {
148  int scan_y,
149  vert_skip;
150  char is_eof, /* Set when EOF or EofOp encountered. */
151  is_seek; /* If true, can seek input file. */
152  } get;
153  struct {
154  int nblank;
155  short (*brun)[2];
156  long fileptr;
157  } put;
158  } priv;
159 }
160 #ifndef c_plusplus
161 rle_hdr /* End of typedef. */
162 #endif
163 ;
164 
165 /*
166  * TAG( rle_dflt_hdr )
167  *
168  * Global variable with possibly useful default values.
169  */
170 extern rle_hdr rle_dflt_hdr;
171 
172 
173 /* Declare RLE library routines. */
174 
175 #ifdef USE_PROTOTYPES
176  /* From rle_error.c. */
177  /*****************************************************************
178  * TAG( rle_alloc_error )
179  *
180  * Print memory allocation error message and exit.
181  */
182  extern int rle_alloc_error( CONST_DECL char *pgm,
183  CONST_DECL char *name );
184 
185  /*****************************************************************
186  * TAG( rle_get_error )
187  *
188  * Print an error message based on the error code returned by
189  * rle_get_setup.
190  */
191  extern int rle_get_error( int code,
192  CONST_DECL char *pgmname,
193  CONST_DECL char *fname );
194 
195  /* From rle_getrow.c */
196 
197  /*****************************************************************
198  * TAG( rle_debug )
199  *
200  * Turn RLE debugging on or off.
201  */
202  extern void rle_debug( int on_off );
203 
204  /*****************************************************************
205  * TAG( rle_get_setup )
206  */
207  extern int rle_get_setup( rle_hdr *the_hdr );
208 
209  /*****************************************************************
210  * TAG( rle_get_setup_ok )
211  *
212  * Call rle_get_setup. If it returns an error code, call
213  * rle_get_error to print the error message, then exit with the error
214  * code.
215  */
216  extern void rle_get_setup_ok( rle_hdr *the_hdr,
217  CONST_DECL char *prog_name,
218  CONST_DECL char *file_name);
219 
220  /*****************************************************************
221  * TAG( rle_getrow )
222  *
223  * Read a scanline worth of data from an RLE file.
224  */
225  extern int rle_getrow( rle_hdr * the_hdr,
226  rle_pixel * scanline[] );
227 
228  /* From rle_getskip.c */
229 
230  /*****************************************************************
231  * TAG( rle_getskip )
232  * Skip a scanline, return the number of the next one.
233  */
234  extern unsigned int rle_getskip( rle_hdr *the_hdr );
235 
236  /* From rle_hdr.c. */
237 
238  /*****************************************************************
239  * TAG( rle_names )
240  *
241  * Load the command and file names into the rle_hdr.
242  */
243  extern void rle_names( rle_hdr *the_hdr,
244  CONST_DECL char *pgmname,
245  CONST_DECL char *fname,
246  int img_num );
247 
248  /*****************************************************************
249  * TAG( rle_hdr_cp )
250  *
251  * Make a "safe" copy of a rle_hdr structure.
252  */
253  extern rle_hdr * rle_hdr_cp( rle_hdr *from_hdr,
254  rle_hdr *to_hdr );
255 
256  /*****************************************************************
257  * TAG( rle_hdr_init )
258  *
259  * Initialize a rle_hdr structure.
260  */
261  extern rle_hdr * rle_hdr_init( rle_hdr *the_hdr );
262 
263  /*****************************************************************
264  * TAG( rle_hdr_clear )
265  *
266  */
267  extern void rle_hdr_clear( rle_hdr *the_hdr );
268 
269  /* From rle_putrow.c. */
270 
271  /*****************************************************************
272  * TAG( rgb_to_bw )
273  *
274  * Converts RGB data to gray data via the NTSC Y transform.
275  */
276  extern void rgb_to_bw( rle_pixel *red_row,
277  rle_pixel *green_row,
278  rle_pixel *blue_row,
279  rle_pixel *bw_row,
280  int rowlen );
281 
282  /*****************************************************************
283  * TAG( rle_puteof )
284  *
285  * Write an End-of-image opcode to the RLE file.
286  */
287  extern void rle_puteof( rle_hdr *the_hdr );
288 
289  /*****************************************************************
290  * TAG( rle_putrow )
291  *
292  * Write a scanline of data to the RLE file.
293  */
294  extern void rle_putrow( rle_pixel *rows[], int rowlen, rle_hdr *the_hdr );
295 
296  /*****************************************************************
297  * TAG( rle_put_init )
298  *
299  * Initialize header for output, but don't write it to the file.
300  */
301  extern void rle_put_init( rle_hdr * the_hdr );
302 
303  /*****************************************************************
304  * TAG( rle_put_setup )
305  *
306  * Write header information to a new RLE image file.
307  */
308  extern void rle_put_setup( rle_hdr * the_hdr );
309 
310  /*****************************************************************
311  * TAG( rle_skiprow )
312  *
313  * Skip nrow scanlines in the output file.
314  */
315  extern void rle_skiprow( rle_hdr *the_hdr, int nrow );
316 
317  /* From rle_cp.c */
318  /*****************************************************************
319  * TAG( rle_cp )
320  * Copy image data from input to output with minimal interpretation.
321  */
322  extern void rle_cp( rle_hdr *in_hdr, rle_hdr *out_hdr );
323 
324  /* From rle_row_alc.c. */
325  /*****************************************************************
326  * TAG( rle_row_alloc )
327  *
328  * Allocate scanline memory for use by rle_getrow.
329  */
330  extern int rle_row_alloc( rle_hdr * the_hdr,
331  rle_pixel *** scanp );
332 
333  /*****************************************************************
334  * TAG( rle_row_free )
335  *
336  * Free the above.
337  */
338  extern void rle_row_free( rle_hdr *the_hdr, rle_pixel **scanp );
339 
340  /* From buildmap.c. */
341  /*
342  * buildmap - build a more usable colormap from data in the_hdr struct.
343  */
344  extern rle_pixel **buildmap( rle_hdr *the_hdr,
345  int minmap,
346  double orig_gamma,
347  double new_gamma );
348 
349  /* From rle_getcom.c. */
350  /*****************************************************************
351  * TAG( rle_getcom )
352  *
353  * Get a specific comment from the image comments.
354  */
355  extern char * rle_getcom( CONST_DECL char * name, rle_hdr * the_hdr );
356 
357  /* From rle_putcom.c. */
358  /*****************************************************************
359  * TAG( rle_delcom )
360  *
361  * Delete a specific comment from the image comments.
362  */
363  extern CONST_DECL char *
364  rle_delcom( CONST_DECL char * name, rle_hdr * the_hdr );
365 
366  /*****************************************************************
367  * TAG( rle_putcom )
368  *
369  * Put (or replace) a comment into the image comments.
370  */
371  extern CONST_DECL char *
372  rle_putcom( CONST_DECL char * value, rle_hdr * the_hdr );
373 
374  /* From dither.c. */
375  /*****************************************************************
376  * TAG( bwdithermap )
377  * Create a color map for ordered dithering in grays.
378  */
379  extern void bwdithermap( int levels, double gamma, int bwmap[],
380  int divN[256], int modN[256], int magic[16][16] );
381  /*****************************************************************
382  * TAG( ditherbw )
383  * Dither a gray-scale value.
384  */
385  extern int ditherbw( int x, int y, int val,
386  int divN[256], int modN[256], int magic[16][16] );
387  /*****************************************************************
388  * TAG( dithergb )
389  * Dither a color value.
390  */
391  extern int dithergb( int x, int y, int r, int g, int b,
392  int divN[256], int modN[256], int magic[16][16] );
393  /*****************************************************************
394  * TAG( dithermap )
395  * Create a color map for ordered dithering in color.
396  */
397  extern void dithermap( int levels, double gamma, int rgbmap[][3],
398  int divN[256], int modN[256], int magic[16][16] );
399  /*****************************************************************
400  * TAG( make_square )
401  * Make a 16x16 magic square for ordered dithering.
402  */
403  extern void make_square( double N, int divN[256], int modN[256],
404  int magic[16][16] );
405 
406  /* From float_to_exp.c. */
407  /*****************************************************************
408  * TAG( float_to_exp )
409  * Convert a list of floating point numbers to "exp" format.
410  */
411  extern void float_to_exp( int count, float * floats, rle_pixel * pixels );
412 
413  /* From rle_open_f.c. */
414  /*****************************************************************
415  * TAG( rle_open_f )
416  *
417  * Open an input/output file with default.
418  */
419  extern FILE *
420  rle_open_f( CONST_DECL char *prog_name,
421  CONST_DECL char *f_name,
422  CONST_DECL char *mode );
423 
424  /*****************************************************************
425  * TAG( rle_open_f_noexit )
426  *
427  * Open an input/output file with default.
428  */
429  extern FILE *
430  rle_open_f_noexit( CONST_DECL char *prog_name,
431  CONST_DECL char *f_name,
432  CONST_DECL char *mode );
433 
434  /*****************************************************************
435  * TAG( rle_close_f )
436  *
437  * Close a file opened by rle_open_f. If the file is stdin or stdout,
438  * it will not be closed.
439  */
440  extern void
441  rle_close_f( FILE *fd );
442 
443  /* From colorquant.c. */
444  /*****************************************************************
445  * TAG( colorquant )
446  * Compute a colormap for quantizing an image to a limited set of colors.
447  */
448  extern int colorquant( rle_pixel *red, rle_pixel *green, rle_pixel *blue,
449  unsigned long pixels, rle_pixel *colormap[3],
450  int colors, int bits,
451  rle_pixel *rgbmap, int fast, int otherimages );
452 
453  /* From rle_addhist.c. */
454  /*****************************************************************
455  * TAG( rle_addhist )
456  * Append history information to the HISTORY comment.
457  */
458  extern void rle_addhist( char *argv[],
459  rle_hdr *in_hdr,
460  rle_hdr *out_hdr );
461 
462  /* From cmd_name.c. */
463  /*****************************************************************
464  * TAG( cmd_name )
465  * Extract command name from argv.
466  */
467  extern char *cmd_name( char **argv );
468 
469  /* From scanargs.c. */
470  /*****************************************************************
471  * TAG( scanargs )
472  * Scan command argument list and parse arguments.
473  */
474  extern int scanargs( int argc,
475  char **argv,
476  CONST_DECL char *format,
477  ... );
478 
479  /* From hilbert.c */
480  /*****************************************************************
481  * TAG( hilbert_i2c )
482  * Convert an index into a Hilbert curve to a set of coordinates.
483  */
484  extern void hilbert_c2i( int n, int m, int a[], long int *r );
485 
486  /*****************************************************************
487  * TAG( hilbert_c2i )
488  * Convert coordinates of a point on a Hilbert curve to its index.
489  */
490  extern void hilbert_i2c( int n, int m, long int r, int a[] );
491 
492  /* From inv_cmap.c */
493  /*****************************************************************
494  * TAG( inv_cmap )
495  * Compute an inverse colormap efficiently.
496  */
497  extern void inv_cmap( int colors,
498  unsigned char *colormap[3],
499  int bits,
500  unsigned long *dist_buf,
501  unsigned char *rgbmap );
502 
503 #else /* USE_PROTOTYPES */
504  /* Return value decls for "K&R" C. See above for full descriptions. */
505  /* From rle_error.c. */
506  extern int rle_alloc_error();
507  extern int rle_get_error();
508 
509  /* From rle_getrow.c. */
510  extern void rle_debug();
511  extern int rle_get_setup();
512  extern void rle_get_setup_ok();
513  extern int rle_getrow();
514 
515  /* From rle_getskip.c */
516  extern unsigned int rle_getskip();
517 
518  /* From rle_hdr.c */
519  extern void rle_names();
520  extern rle_hdr *rle_hdr_cp();
521  extern rle_hdr *rle_hdr_init();
522  extern void rle_hdr_clear();
523 
524  /* From rle_putrow.c. */
525  extern void rgb_to_bw();
526  extern void rle_puteof();
527  extern void rle_putrow();
528  extern void rle_put_init();
529  extern void rle_put_setup();
530  extern void rle_skiprow();
531 
532  /* From rle_cp.c */
533  extern void rle_cp();
534 
535  /* From rle_row_alc.c. */
536  extern int rle_row_alloc();
537  extern void rle_row_free();
538 
539  /* From buildmap.c. */
540  extern rle_pixel **buildmap();
541 
542  /* From rle_getcom.c. */
543  extern char *rle_getcom();
544 
545  /* From rle_putcom.c. */
546  extern char *rle_delcom();
547  extern char *rle_putcom();
548 
549  /* From dither.c. */
550  extern void bwdithermap();
551  extern int ditherbw();
552  extern int dithergb();
553  extern void dithermap();
554  extern void magic4x4();
555  extern void make_square();
556 
557  /* From float_to_exp.c. */
558  extern void float_to_exp();
559 
560  /* From rle_open_f.c. */
561  extern FILE *rle_open_f();
562  extern FILE *rle_open_f_noexit();
563  extern void rle_close_f( );
564 
565  /* From colorquant.c. */
566  extern int colorquant();
567 
568  /* From rle_addhist.c. */
569  extern void rle_addhist();
570 
571  /* From cmd_name.c. */
572  extern char *cmd_name();
573 
574  /* From scanargs.c. */
575  extern int scanargs();
576 
577  /* From hilbert.c */
578  extern void hilbert_c2i();
579  extern void hilbert_i2c();
580 
581  /* From inv_cmap.c */
582  extern void inv_cmap();
583 
584 #endif /* USE_PROTOTYPES */
585 
586 #endif /* RLE_H */
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
int xmin
Definition: rle.h:100
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
void rle_cp(rle_hdr *in_hdr, rle_hdr *the_hdr)
Definition: rle_cp.c:69
const char ** comments
Definition: rle.h:113
void make_square(double N, divN, modN, magic)
Definition: dither.c:192
#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
void rle_debug(int on_off)
Definition: rle_getrow.c:293
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void rle_close_f(FILE *fd)
Definition: rle_open_f.c:244
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
rle_map * cmap
Definition: rle.h:112
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
void hilbert_i2c(int n, int m, long int r, a)
Definition: hilbert.c:136
void hilbert_c2i(int n, int m, a, long int *r)
Definition: hilbert.c:245
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
int * bg_color
Definition: rle.h:100
int ymin
Definition: rle.h:100
int rle_get_error(int code, const char *pgmname, const char *fname)
Definition: rle_error.c:76
long int is_init
Definition: rle.h:131
void rgb_to_bw(rle_pixel *red_row, rle_pixel *green_row, rle_pixel *blue_row, rle_pixel *bw_row, int rowlen)
Definition: rle_putrow.c:680
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
void dithermap(int levels, double gamma, rgbmap, divN, modN, magic)
Definition: dither.c:80
rle_pixel ** buildmap(rle_hdr *the_hdr, int minmap, double orig_gamma, double new_gamma)
Definition: buildmap.c:56
const char * cmd
Definition: rle.h:133
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
Definition: rle.h:96
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
Definition: rle_putrow.c:96
int xmax
Definition: rle.h:100
enum rle_dispatch dispatch
Definition: rle.h:99
int img_num
Definition: rle.h:135
int colorquant(unsigned char *red, unsigned char *green, unsigned char *blue, unsigned long pixels, colormap, int colors, int bits, unsigned char *rgbmap, int flags, int accum_hist)
Definition: colorquant.c:242
void float_to_exp(int count, float *floats, rle_pixel *pixels)
Definition: float_to_exp.c:42
const char * rle_delcom(char *name, the_hdr) const
Definition: rle_putcom.c:140
#define CONST_DECL
Definition: rle_config.h:42
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
unsigned int rle_getskip(rle_hdr *the_hdr)
Definition: rle_getskip.c:57
void inv_cmap(int colors, colormap, int bits, unsigned long *dist_buf,*rgbmap)
Definition: inv_cmap.c:214
char bits[256/8]
Definition: rle.h:128
FILE * rle_open_f_noexit(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:57
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
Definition: rle_getrow.c:254
void rle_skiprow(rle_hdr *the_hdr, int nrow)
Definition: rle_putrow.c:393
char * rle_getcom(char *name, rle_hdr *the_hdr) const
Definition: rle_getcom.c:81
int background
Definition: rle.h:100
int rle_alloc_error(char *pgm, char *name) const
Definition: rle_error.c:46
int ncmap
Definition: rle.h:100
int ymax
Definition: rle.h:100
void rle_put_init(rle_hdr *the_hdr)
Definition: rle_putrow.c:415
int ditherbw(int x, int y, int val, divN, modN, magic)
Definition: dither.c:279
unsigned char rle_pixel
Definition: rle.h:56
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
const char * rle_putcom(char *value, rle_hdr *the_hdr) const
Definition: rle_putcom.c:82
int cmaplen
Definition: rle.h:100
int alpha
Definition: rle.h:100
unsigned short rle_map
Definition: rle.h:57
void rle_hdr_clear(rle_hdr *the_hdr)
Definition: rle_hdr.c:222
const char * file_name
Definition: rle.h:134
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
rle_hdr rle_dflt_hdr
Definition: rle_global.c:66
int dithergb(int x, int y, int r, int g, int b, int levels, divN, modN, magic)
Definition: dither.c:247
FILE * rle_file
Definition: rle.h:114
rle_dispatch
Definition: rle.h:44
int ncolors
Definition: rle.h:100
void bwdithermap(int levels, double gamma, bwmap, divN, modN, magic)
Definition: dither.c:142