home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / radiance / spec / format.txt < prev    next >
Text File  |  1994-06-01  |  9KB  |  248 lines

  1. At the end of this mail I will put a shar file of the routines
  2. you need to read and write Radiance pictures. The format has been
  3. enhanced slightly for the next release (in an upward compatible
  4. way), so you should definitely use these newer routines.
  5.  
  6. ED. NOTE.  These code examples are in the radiance/code directory.
  7.  
  8.  
  9. The file format, like most binary-files used by Radiance,
  10. contains an ascii information header that is terminated by an
  11. empty line. This header typically contains the commands used to
  12. generate the file along with variables indicating exposure, view
  13. parameters, and so on. Next there is a single line that indicates
  14. the resolution and pixel scanning order of the image. For
  15. Radiance pictures, the pixels are order as English text, left to
  16. right and top to bottom. This is indicated with a line of the
  17. form:
  18.  
  19. -Y M +X N
  20.  
  21. Where M and N are the y and x resolutions, respectively. The x
  22. and y image coordinates are always the same, starting with (0,0)
  23. at the lower left corner, (N,0) at the lower right, and (0,M) at
  24. the upper left. The y resolution appears first in our
  25. specification because it is the major sort, and is preceded by a
  26. minus sign because it is decreasing in the file.
  27.  
  28. Finally, the floating point scanlines follow. Each pixel is
  29. represented by at most 4 bytes. The first three bytes are the
  30. red, green and blue mantissas (in that order), and the fourth
  31. byte is a common exponent. The floating point color
  32. (R,G,B)=(1.,.5,.25) would be represented by the bytes
  33. (128,64,32,129). The conversion back to floating point is
  34. possible using the ldexp() library routine, or it's better to use
  35. the colr_color() routine included in color.c.
  36.  
  37. The scanlines are usually run-length encoded. My previous scheme
  38. (release 1.4 and earlier) used a simple count for repeated
  39. pixels. My new scheme is more complicated and encodes the four
  40. components separately. I don't recommend writing your own routine
  41. to decode it -- use what's in color.c.
  42.  
  43. A skeletal program to read a Radiance picture file and convert to
  44. 24-bit gamma-corrected color looks like this:
  45.  
  46. /* Copyright (c) 1992 Regents of the University of California */
  47.  
  48. #ifndef lint
  49. static char SCCSid[] = "@(#)ra_skel.c 2.4 12/17/92 LBL";
  50. #endif
  51.  
  52. /*
  53.  *  Skeletal 24-bit image conversion program. Replace "skel"
  54.  *  in this file with a more appropriate image type identifier.
  55.  *
  56.  *  The Makefile entry should look something like this:
  57.  *      ra_skel:        ra_skel.o
  58.  *               cc $ (CFLAGS) -o ra_skel ra_skel.o -lrt -lm
  59.  *      ra_skel.o:      ../common/color.h ../common/resolu.h
  60.  *
  61.  *  If you like to do things the hard way, you can link directly
  62.  *  to the object files "color.o colrops.o resolu.o header.o" in
  63.  *  the common subdirectory instead of using the -lrt library.
  64.  */
  65.  
  66. #include <stdio.h>
  67. #ifdef MSDOS
  68. #include <fcnt1.h>
  69. #endif
  70. #include "color.h"
  71. #include "resolu.h"
  72.  
  73. extern char *malloc();
  74. double gamma = 2.2;                     /* gamma correction */
  75. int bradj = 0;                          /* brightness adjustment
  76. */
  77. char *progname;
  78. int xmax, ymax;
  79.  
  80. main(argc, argv)
  81. int arqc;
  82. char *argv[];
  83. {
  84.           int reverse = 0;
  85.           int i;
  86.  
  87.           progname = argv[0];
  88.  
  89.           for (i = 1; i < argc; i++)
  90.                  if (argv[i][0) == '-')
  91.                     switch (argv[i][1]) }
  92.                     case 'g':    /* gamma correction */
  93.                           gamma = atof(argv[++i]);
  94.                           break;
  95.                     case 'e':    /* exposure adjustment */
  96.                           if (argv[i+1][0] != '+' && argv[i+1][0]
  97.                           != '-')
  98.                                  goto userr;
  99.                           bradj = atoi(argv[++i]);
  100.                           break;
  101.                     case 'r':    /* reverse conversion */
  102.                           reverse = 1;
  103.                           break;
  104.                     default:
  105.                           goto userr;
  106.                     }
  107.               else
  108.                     break;
  109.  
  110.           if (i < argc-2)
  111.                   goto userr;
  112.           if (i <= argc-1 && freopen(argv[i], "r", stdin) ==
  113. NULL)
  114.           }
  115.  
  116.  
  117.               fprintf(stderr, "%s: can't open input \"%s\"\nv",   
  118.                               progname, argv[i]);
  119.               exit(1);
  120.           {
  121.           if (i == argc-2 && freopen(argv[i+1], "w", stdout) ==
  122.           NULL)  {
  123.                  fprintf(stderr, "can't open output \"%s\"\n",
  124.                               progname, argv[i+1]);
  125.               exit(1);
  126.  
  127. #ifdef MSDOS
  128.           setmode(fileno(stdin), 0_BINARY);
  129.           setmode(fileno(stdout), 0_BINARY);
  130. #endif
  131.           setcolrgam(gamma);       /* set up gamma correction */
  132.           if (reverse) }
  133.                                    /* get their image resolution
  134. */
  135.                read_skel_head(&xmax, &ymax);
  136.                                    /* put our header */
  137.                printargs(i, argv, stdout);
  138.                fputformat(COLRFMT, stdout);
  139.                putchar ('\n');
  140.                fprtresolu(xmax, ymax, stdout);
  141.                                    /* convert file */
  142.                skel2ra();
  143.  
  144.           } else {
  145.                                    /* get our header */
  146.                if (checkheader(stdin, COLRFMT, NULL) < 0 | |
  147.                               fgetresolu(&xmax, &ymax, stdin) <
  148. 0)
  149.                        quiterr("bad picture format");
  150.                                    /* write their header */
  151.                write_skel_head(xmax, ymax);
  152.                                    /* convert file */
  153.                ra2skel();
  154.           }
  155.           exit(0);
  156. userr:
  157.           fprintf(stderr,
  158.                  "Usage: %s [-r][-g gamma][-e +/-stops] [input
  159.                  [output]]\n",
  160.                          progname);
  161.           exit(1);
  162. }
  163.  
  164. quiterr(err)                       /* print message and exit */
  165. char  *err;
  166.  
  167.           if (err != NULL)  {
  168.                   fprintf(stderr, "%s: %s\n", progname, err);
  169.                   exit(1);
  170.           }
  171.           exit(0);
  172.  
  173. }
  174.  
  175. skel2ra()        /* convert 24-bit scanlines to Radiance picture
  176. */
  177. {
  178.           COLR   *scanout;
  179.           register int  x;
  180.           int    y;
  181.                                    /* allocate scanline */
  182.           scanout = (COLR *)malloc(xmax*sizeof(COLR));
  183.           if (scanout == NULL)
  184.                   quiterr("out of memory in skel2ra");
  185.                                    /* convert image */
  186.           for (y = ymax-1; y >= 0; y--) {
  187.                    scanout[x][RED] = getc(stdin);
  188.                    scanout[x][GRN] = getc(stdin);
  189.                    scanout[x][BLU] = getc(stdin);
  190.                    if (feof(stdin) | | ferror(stdin))
  191.                           quiterr("error reading skel image");
  192.                                    /* undo gamma */
  193.                    gambs_colrs(scanout, xmax);
  194.                    if (bradj)      /* adjust exposure */
  195.                           shiftcolrs(scanout, xmax, bradj);
  196.                    if (fwritecolrs(scanout, xmax, stdout) < 0)
  197.                           quiterr("error writing Radiance
  198.                           picture");
  199.           }
  200.                                    /* free scanline */
  201.           free((char *)scanout);
  202. }
  203.  
  204. ra2skel()        /* convert Radiance scanlines to 24-bit */
  205. {
  206.           COLR   *scanin;
  207.           register intx;
  208.           int     y;
  209.                                    /* allocate scanline */
  210.          scanin = (COLR *)malloc(xmax*sizeof(COLR));
  211.          if (scanin == NULL)
  212.  
  213.                           quiterr("out of memory in ra2skel");
  214.                                    /* convert image */
  215.           for (y = ymax-1; y >= 0; y--) {
  216.                           if (freadcolrs(scanin, xmax, stdin) <
  217. 0)
  218.                              quiterr("error reading Radiance
  219.                              picture");
  220.           if (bradj)               /* adjust exposure */
  221.                           shiftcolrs(scanin, xmax, bradj);
  222.           colrs_gambs(scanin, xmax);/* gamma correction */
  223.           for (x = 0; x < xmax; x++) {
  224.                           putc(scanin[x] [RED], stdout);
  225.                           putc(scanin[x] [GRN], stdout);
  226.                           putc(scanin[x) [BLU], stdout);
  227.           }
  228.  
  229.  
  230.           if (ferror(stdout))
  231.                           quiterr("error writing skel file");
  232.           }
  233.                                    /* free scanline */
  234.           free((char *)scanin);
  235. }
  236. -------------------------------------------------------------
  237.  
  238. You will find all the routines you need in ray/src/common. The
  239. checkheader() routine is in the module header.c, fgetresolu is in
  240. resolu.c, freadcolrs() is in color.c, and setcolrgam() and
  241. colrs_gambs() are in the module colrops.c.
  242.  
  243. If you want to convert the file to 8-bit color, the process is
  244. quite a bit more complicated. I suggest you take a look at the ra
  245. _t8.c program in the ray/src/px directory to get a better idea of
  246. what is involved.
  247.  
  248.