00001 /* 00002 * This software is copyrighted as noted below. It may be freely copied, 00003 * modified, and redistributed, provided that the copyright notice is 00004 * preserved on all copies. 00005 * 00006 * There is no warranty or other guarantee of fitness for this software, 00007 * it is provided solely "as is". Bug reports or fixes may be sent 00008 * to the author, who may or may not act on them as he desires. 00009 * 00010 * You may not include this software in a program or other software product 00011 * without supplying the source, or without informing the end-user that the 00012 * source is available for no extra charge. 00013 * 00014 * If you modify this software, you should include a notice giving the 00015 * name of the person performing the modification, the date of modification, 00016 * and the reason for such modification. 00017 */ 00018 /** @addtogroup utahrle */ 00019 /*@{*/ 00020 /** 00021 * @file rle.h - Global declarations for Utah Raster Toolkit RLE programs. 00022 * 00023 * Author: Todd W. Fuqua 00024 * Computer Science Dept. 00025 * University of Utah 00026 * Date: Sun Jul 29 1984 00027 * Copyright (c) 1984 Todd W. Fuqua 00028 * 00029 * $Id: rle.h,v 14.4 2006/09/18 05:24:07 lbutler Exp $ 00030 */ 00031 00032 #ifndef RLE_H 00033 #define RLE_H 00034 00035 #include "rle_config.h" /* Configuration parameters. */ 00036 00037 #include <stdio.h> /* Declare FILE. */ 00038 #include <stdlib.h> 00039 00040 #ifdef c_plusplus 00041 #define USE_PROTOTYPES 00042 #endif 00043 #ifndef CONST_DECL 00044 #define CONST_DECL 00045 #endif 00046 00047 enum rle_dispatch { 00048 NO_DISPATCH = -1, 00049 RUN_DISPATCH = 0 00050 }; 00051 00052 /* **************************************************************** 00053 * TAG( rle_pixel rle_map ) 00054 * 00055 * Typedef for 8-bit (or less) pixel data. 00056 * 00057 * Typedef for 16-bit color map data. 00058 */ 00059 typedef unsigned char rle_pixel; 00060 typedef unsigned short rle_map; 00061 00062 /* 00063 * Defines for traditional channel numbers. 00064 */ 00065 #define RLE_RED 0 /* Red channel traditionally here. */ 00066 #define RLE_GREEN 1 /* Green channel traditionally here. */ 00067 #define RLE_BLUE 2 /* Blue channel traditionally here. */ 00068 #define RLE_ALPHA -1 /* Alpha channel here. */ 00069 00070 /* 00071 * Return values from rle_get_setup. 00072 */ 00073 #define RLE_SUCCESS 0 00074 #define RLE_NOT_RLE -1 00075 #define RLE_NO_SPACE -2 00076 #define RLE_EMPTY -3 00077 #define RLE_EOF -4 00078 00079 /* 00080 * "Magic" value for is_init field. Pi * 2^29. 00081 */ 00082 #define RLE_INIT_MAGIC 0x6487ED51L 00083 00084 /***************************************************************** 00085 * TAG( RLE_CHECK_ALLOC ) 00086 * 00087 * Test for allocation failure, scream and die if so. 00088 */ 00089 #define RLE_CHECK_ALLOC( pgm, ptr, name ) \ 00090 ( !(ptr) ? rle_alloc_error( pgm, name ) : 0 ) 00091 00092 /* 00093 * TAG( rle_hdr ) 00094 * 00095 * Definition of header structure used by RLE routines. 00096 */ 00097 00098 #ifndef c_plusplus 00099 typedef 00100 #endif 00101 struct rle_hdr { 00102 enum rle_dispatch dispatch; /* Type of file to create. */ 00103 int ncolors, /* Number of color channels. */ 00104 *bg_color, /* Pointer to bg color vector. */ 00105 alpha, /* If !0, save alpha channel. */ 00106 background, /* 0->just save all pixels, */ 00107 /* 1->overlay, 2->clear to bg first. */ 00108 xmin, /* Lower X bound (left.) */ 00109 xmax, /* Upper X bound (right.) */ 00110 ymin, /* Lower Y bound (bottom.) */ 00111 ymax, /* Upper Y bound (top.) */ 00112 ncmap, /* Number of color channels in color map. */ 00113 /* Map only saved if != 0. */ 00114 cmaplen; /* Log2 of color map length. */ 00115 rle_map *cmap; /* Pointer to color map array. */ 00116 CONST_DECL char **comments; /* Pointer to array of pointers to comments. */ 00117 FILE *rle_file; /* Input or output file. */ 00118 /* 00119 * Bit map of channels to read/save. Indexed by (channel mod 256). 00120 * Alpha channel sets bit 255. 00121 * 00122 * Indexing (0 <= c <= 255): 00123 * bits[c/8] & (1 << (c%8)) 00124 */ 00125 #define RLE_SET_BIT(glob,bit) \ 00126 ((glob).bits[((bit)&0xff)/8] |= (1<<((bit)&0x7))) 00127 #define RLE_CLR_BIT(glob,bit) \ 00128 ((glob).bits[((bit)&0xff)/8] &= ~(1<<((bit)&0x7))) 00129 #define RLE_BIT(glob,bit) \ 00130 ((glob).bits[((bit)&0xff)/8] & (1<<((bit)&0x7))) 00131 char bits[256/8]; 00132 /* Set to magic pattern if following fields are initialized. */ 00133 /* This gives a 2^(-32) chance of missing. */ 00134 long int is_init; 00135 /* Save the command, file name and image number for error messages. */ 00136 CONST_DECL char *cmd; 00137 CONST_DECL char *file_name; 00138 int img_num; 00139 /* 00140 * Local storage for rle_getrow & rle_putrow. 00141 * rle_getrow has 00142 * scan_y int current Y scanline. 00143 * vert_skip int number of lines to skip. 00144 * rle_putrow has 00145 * nblank int number of blank lines. 00146 * brun short(*)[2] Array of background runs. 00147 * fileptr long Position in output file. 00148 */ 00149 union { 00150 struct { 00151 int scan_y, 00152 vert_skip; 00153 char is_eof, /* Set when EOF or EofOp encountered. */ 00154 is_seek; /* If true, can seek input file. */ 00155 } get; 00156 struct { 00157 int nblank; 00158 short (*brun)[2]; 00159 long fileptr; 00160 } put; 00161 } priv; 00162 } 00163 #ifndef c_plusplus 00164 rle_hdr /* End of typedef. */ 00165 #endif 00166 ; 00167 00168 /* 00169 * TAG( rle_dflt_hdr ) 00170 * 00171 * Global variable with possibly useful default values. 00172 */ 00173 extern rle_hdr rle_dflt_hdr; 00174 00175 00176 /* Declare RLE library routines. */ 00177 00178 #ifdef USE_PROTOTYPES 00179 /* From rle_error.c. */ 00180 /***************************************************************** 00181 * TAG( rle_alloc_error ) 00182 * 00183 * Print memory allocation error message and exit. 00184 */ 00185 extern int rle_alloc_error( CONST_DECL char *pgm, 00186 CONST_DECL char *name ); 00187 00188 /***************************************************************** 00189 * TAG( rle_get_error ) 00190 * 00191 * Print an error message based on the error code returned by 00192 * rle_get_setup. 00193 */ 00194 extern int rle_get_error( int code, 00195 CONST_DECL char *pgmname, 00196 CONST_DECL char *fname ); 00197 00198 /* From rle_getrow.c */ 00199 00200 /***************************************************************** 00201 * TAG( rle_debug ) 00202 * 00203 * Turn RLE debugging on or off. 00204 */ 00205 extern void rle_debug( int on_off ); 00206 00207 /***************************************************************** 00208 * TAG( rle_get_setup ) 00209 */ 00210 extern int rle_get_setup( rle_hdr *the_hdr ); 00211 00212 /***************************************************************** 00213 * TAG( rle_get_setup_ok ) 00214 * 00215 * Call rle_get_setup. If it returns an error code, call 00216 * rle_get_error to print the error message, then exit with the error 00217 * code. 00218 */ 00219 extern void rle_get_setup_ok( rle_hdr *the_hdr, 00220 CONST_DECL char *prog_name, 00221 CONST_DECL char *file_name); 00222 00223 /***************************************************************** 00224 * TAG( rle_getrow ) 00225 * 00226 * Read a scanline worth of data from an RLE file. 00227 */ 00228 extern int rle_getrow( rle_hdr * the_hdr, 00229 rle_pixel * scanline[] ); 00230 00231 /* From rle_getskip.c */ 00232 00233 /***************************************************************** 00234 * TAG( rle_getskip ) 00235 * Skip a scanline, return the number of the next one. 00236 */ 00237 extern unsigned int rle_getskip( rle_hdr *the_hdr ); 00238 00239 /* From rle_hdr.c. */ 00240 00241 /***************************************************************** 00242 * TAG( rle_names ) 00243 * 00244 * Load the command and file names into the rle_hdr. 00245 */ 00246 extern void rle_names( rle_hdr *the_hdr, 00247 CONST_DECL char *pgmname, 00248 CONST_DECL char *fname, 00249 int img_num ); 00250 00251 /***************************************************************** 00252 * TAG( rle_hdr_cp ) 00253 * 00254 * Make a "safe" copy of a rle_hdr structure. 00255 */ 00256 extern rle_hdr * rle_hdr_cp( rle_hdr *from_hdr, 00257 rle_hdr *to_hdr ); 00258 00259 /***************************************************************** 00260 * TAG( rle_hdr_init ) 00261 * 00262 * Initialize a rle_hdr structure. 00263 */ 00264 extern rle_hdr * rle_hdr_init( rle_hdr *the_hdr ); 00265 00266 /***************************************************************** 00267 * TAG( rle_hdr_clear ) 00268 * 00269 */ 00270 extern void rle_hdr_clear( rle_hdr *the_hdr ); 00271 00272 /* From rle_putrow.c. */ 00273 00274 /***************************************************************** 00275 * TAG( rgb_to_bw ) 00276 * 00277 * Converts RGB data to gray data via the NTSC Y transform. 00278 */ 00279 extern void rgb_to_bw( rle_pixel *red_row, 00280 rle_pixel *green_row, 00281 rle_pixel *blue_row, 00282 rle_pixel *bw_row, 00283 int rowlen ); 00284 00285 /***************************************************************** 00286 * TAG( rle_puteof ) 00287 * 00288 * Write an End-of-image opcode to the RLE file. 00289 */ 00290 extern void rle_puteof( rle_hdr *the_hdr ); 00291 00292 /***************************************************************** 00293 * TAG( rle_putrow ) 00294 * 00295 * Write a scanline of data to the RLE file. 00296 */ 00297 extern void rle_putrow( rle_pixel *rows[], int rowlen, rle_hdr *the_hdr ); 00298 00299 /***************************************************************** 00300 * TAG( rle_put_init ) 00301 * 00302 * Initialize header for output, but don't write it to the file. 00303 */ 00304 extern void rle_put_init( rle_hdr * the_hdr ); 00305 00306 /***************************************************************** 00307 * TAG( rle_put_setup ) 00308 * 00309 * Write header information to a new RLE image file. 00310 */ 00311 extern void rle_put_setup( rle_hdr * the_hdr ); 00312 00313 /***************************************************************** 00314 * TAG( rle_skiprow ) 00315 * 00316 * Skip nrow scanlines in the output file. 00317 */ 00318 extern void rle_skiprow( rle_hdr *the_hdr, int nrow ); 00319 00320 /* From rle_cp.c */ 00321 /***************************************************************** 00322 * TAG( rle_cp ) 00323 * Copy image data from input to output with minimal interpretation. 00324 */ 00325 extern void rle_cp( rle_hdr *in_hdr, rle_hdr *out_hdr ); 00326 00327 /* From rle_row_alc.c. */ 00328 /***************************************************************** 00329 * TAG( rle_row_alloc ) 00330 * 00331 * Allocate scanline memory for use by rle_getrow. 00332 */ 00333 extern int rle_row_alloc( rle_hdr * the_hdr, 00334 rle_pixel *** scanp ); 00335 00336 /***************************************************************** 00337 * TAG( rle_row_free ) 00338 * 00339 * Free the above. 00340 */ 00341 extern void rle_row_free( rle_hdr *the_hdr, rle_pixel **scanp ); 00342 00343 /* From buildmap.c. */ 00344 /* 00345 * buildmap - build a more usable colormap from data in the_hdr struct. 00346 */ 00347 extern rle_pixel **buildmap( rle_hdr *the_hdr, 00348 int minmap, 00349 double orig_gamma, 00350 double new_gamma ); 00351 00352 /* From rle_getcom.c. */ 00353 /***************************************************************** 00354 * TAG( rle_getcom ) 00355 * 00356 * Get a specific comment from the image comments. 00357 */ 00358 extern char * rle_getcom( CONST_DECL char * name, rle_hdr * the_hdr ); 00359 00360 /* From rle_putcom.c. */ 00361 /***************************************************************** 00362 * TAG( rle_delcom ) 00363 * 00364 * Delete a specific comment from the image comments. 00365 */ 00366 extern CONST_DECL char * 00367 rle_delcom( CONST_DECL char * name, rle_hdr * the_hdr ); 00368 00369 /***************************************************************** 00370 * TAG( rle_putcom ) 00371 * 00372 * Put (or replace) a comment into the image comments. 00373 */ 00374 extern CONST_DECL char * 00375 rle_putcom( CONST_DECL char * value, rle_hdr * the_hdr ); 00376 00377 /* From dither.c. */ 00378 /***************************************************************** 00379 * TAG( bwdithermap ) 00380 * Create a color map for ordered dithering in grays. 00381 */ 00382 extern void bwdithermap( int levels, double gamma, int bwmap[], 00383 int divN[256], int modN[256], int magic[16][16] ); 00384 /***************************************************************** 00385 * TAG( ditherbw ) 00386 * Dither a gray-scale value. 00387 */ 00388 extern int ditherbw( int x, int y, int val, 00389 int divN[256], int modN[256], int magic[16][16] ); 00390 /***************************************************************** 00391 * TAG( dithergb ) 00392 * Dither a color value. 00393 */ 00394 extern int dithergb( int x, int y, int r, int g, int b, 00395 int divN[256], int modN[256], int magic[16][16] ); 00396 /***************************************************************** 00397 * TAG( dithermap ) 00398 * Create a color map for ordered dithering in color. 00399 */ 00400 extern void dithermap( int levels, double gamma, int rgbmap[][3], 00401 int divN[256], int modN[256], int magic[16][16] ); 00402 /***************************************************************** 00403 * TAG( make_square ) 00404 * Make a 16x16 magic square for ordered dithering. 00405 */ 00406 extern void make_square( double N, int divN[256], int modN[256], 00407 int magic[16][16] ); 00408 00409 /* From float_to_exp.c. */ 00410 /***************************************************************** 00411 * TAG( float_to_exp ) 00412 * Convert a list of floating point numbers to "exp" format. 00413 */ 00414 extern void float_to_exp( int count, float * floats, rle_pixel * pixels ); 00415 00416 /* From rle_open_f.c. */ 00417 /***************************************************************** 00418 * TAG( rle_open_f ) 00419 * 00420 * Open an input/output file with default. 00421 */ 00422 extern FILE * 00423 rle_open_f( CONST_DECL char *prog_name, 00424 CONST_DECL char *f_name, 00425 CONST_DECL char *mode ); 00426 00427 /***************************************************************** 00428 * TAG( rle_open_f_noexit ) 00429 * 00430 * Open an input/output file with default. 00431 */ 00432 extern FILE * 00433 rle_open_f_noexit( CONST_DECL char *prog_name, 00434 CONST_DECL char *f_name, 00435 CONST_DECL char *mode ); 00436 00437 /***************************************************************** 00438 * TAG( rle_close_f ) 00439 * 00440 * Close a file opened by rle_open_f. If the file is stdin or stdout, 00441 * it will not be closed. 00442 */ 00443 extern void 00444 rle_close_f( FILE *fd ); 00445 00446 /* From colorquant.c. */ 00447 /***************************************************************** 00448 * TAG( colorquant ) 00449 * Compute a colormap for quantizing an image to a limited set of colors. 00450 */ 00451 extern int colorquant( rle_pixel *red, rle_pixel *green, rle_pixel *blue, 00452 unsigned long pixels, rle_pixel *colormap[3], 00453 int colors, int bits, 00454 rle_pixel *rgbmap, int fast, int otherimages ); 00455 00456 /* From rle_addhist.c. */ 00457 /***************************************************************** 00458 * TAG( rle_addhist ) 00459 * Append history information to the HISTORY comment. 00460 */ 00461 extern void rle_addhist( char *argv[], 00462 rle_hdr *in_hdr, 00463 rle_hdr *out_hdr ); 00464 00465 /* From cmd_name.c. */ 00466 /***************************************************************** 00467 * TAG( cmd_name ) 00468 * Extract command name from argv. 00469 */ 00470 extern char *cmd_name( char **argv ); 00471 00472 /* From scanargs.c. */ 00473 /***************************************************************** 00474 * TAG( scanargs ) 00475 * Scan command argument list and parse arguments. 00476 */ 00477 extern int scanargs( int argc, 00478 char **argv, 00479 CONST_DECL char *format, 00480 ... ); 00481 00482 /* From hilbert.c */ 00483 /***************************************************************** 00484 * TAG( hilbert_i2c ) 00485 * Convert an index into a Hilbert curve to a set of coordinates. 00486 */ 00487 extern void hilbert_c2i( int n, int m, int a[], long int *r ); 00488 00489 /***************************************************************** 00490 * TAG( hilbert_c2i ) 00491 * Convert coordinates of a point on a Hilbert curve to its index. 00492 */ 00493 extern void hilbert_i2c( int n, int m, long int r, int a[] ); 00494 00495 /* From inv_cmap.c */ 00496 /***************************************************************** 00497 * TAG( inv_cmap ) 00498 * Compute an inverse colormap efficiently. 00499 */ 00500 extern void inv_cmap( int colors, 00501 unsigned char *colormap[3], 00502 int bits, 00503 unsigned long *dist_buf, 00504 unsigned char *rgbmap ); 00505 00506 #else /* USE_PROTOTYPES */ 00507 /* Return value decls for "K&R" C. See above for full descriptions. */ 00508 /* From rle_error.c. */ 00509 extern int rle_alloc_error(); 00510 extern int rle_get_error(); 00511 00512 /* From rle_getrow.c. */ 00513 extern void rle_debug(); 00514 extern int rle_get_setup(); 00515 extern void rle_get_setup_ok(); 00516 extern int rle_getrow(); 00517 00518 /* From rle_getskip.c */ 00519 extern unsigned int rle_getskip(); 00520 00521 /* From rle_hdr.c */ 00522 extern void rle_names(); 00523 extern rle_hdr *rle_hdr_cp(); 00524 extern rle_hdr *rle_hdr_init(); 00525 extern void rle_hdr_clear(); 00526 00527 /* From rle_putrow.c. */ 00528 extern void rgb_to_bw(); 00529 extern void rle_puteof(); 00530 extern void rle_putrow(); 00531 extern void rle_put_init(); 00532 extern void rle_put_setup(); 00533 extern void rle_skiprow(); 00534 00535 /* From rle_cp.c */ 00536 extern void rle_cp(); 00537 00538 /* From rle_row_alc.c. */ 00539 extern int rle_row_alloc(); 00540 extern void rle_row_free(); 00541 00542 /* From buildmap.c. */ 00543 extern rle_pixel **buildmap(); 00544 00545 /* From rle_getcom.c. */ 00546 extern char *rle_getcom(); 00547 00548 /* From rle_putcom.c. */ 00549 extern char *rle_delcom(); 00550 extern char *rle_putcom(); 00551 00552 /* From dither.c. */ 00553 extern void bwdithermap(); 00554 extern int ditherbw(); 00555 extern int dithergb(); 00556 extern void dithermap(); 00557 extern void magic4x4(); 00558 extern void make_square(); 00559 00560 /* From float_to_exp.c. */ 00561 extern void float_to_exp(); 00562 00563 /* From rle_open_f.c. */ 00564 extern FILE *rle_open_f(); 00565 extern FILE *rle_open_f_noexit(); 00566 extern void rle_close_f( ); 00567 00568 /* From colorquant.c. */ 00569 extern int colorquant(); 00570 00571 /* From rle_addhist.c. */ 00572 extern void rle_addhist(); 00573 00574 /* From cmd_name.c. */ 00575 extern char *cmd_name(); 00576 00577 /* From scanargs.c. */ 00578 extern int scanargs(); 00579 00580 /* From hilbert.c */ 00581 extern void hilbert_c2i(); 00582 extern void hilbert_i2c(); 00583 00584 /* From inv_cmap.c */ 00585 extern void inv_cmap(); 00586 00587 #endif /* USE_PROTOTYPES */ 00588 00589 #endif /* RLE_H */ 00590 /*@}*/