home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / rexx / library2 / gbmrexx / gbm / gbmgamma.c < prev    next >
C/C++ Source or Header  |  1993-02-20  |  9KB  |  374 lines

  1. /*
  2.  
  3. GBMGAMMA.C  Gamma correct General Bitmap
  4.  
  5. */
  6.  
  7. /*...sincludes:0:*/
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <math.h>
  11. #include <string.h>
  12. #include <stddef.h>
  13. #include <stdlib.h>
  14. #include <stdarg.h>
  15. #include <memory.h>
  16. #include <malloc.h>
  17. #ifdef AIX
  18. #include <unistd.h>
  19. #else
  20. #include <io.h>
  21. #endif
  22. #include <fcntl.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #ifndef O_BINARY
  26. #define    O_BINARY    0
  27. #endif
  28. #include "standard.h"
  29. #include "gbm.h"
  30.  
  31. /*...vgbm\46\h:0:*/
  32. /*...e*/
  33.  
  34. static char progname [] = "gbmgamma";
  35. static char sccs_id [] = "@(#)Gamma Correct General Bitmap  1/3/93";
  36.  
  37. /*...sfatal:0:*/
  38. static void fatal(const char *fmt, ...)
  39.     {
  40.     va_list    vars;
  41.     char    s [256+1];
  42.  
  43.     va_start(vars, fmt);
  44.     vsprintf(s, fmt, vars);
  45.     va_end(vars);
  46.     fprintf(stderr, "%s: %s\n", progname, s);
  47.     exit(1);
  48.     }
  49. /*...e*/
  50. /*...susage:0:*/
  51. static void usage(void)
  52.     {
  53.     int    ft, n_ft;
  54.  
  55.     fprintf(stderr, "usage: %s [-m map] [-g gamma] [-s shelf] fn1.ext{,opt} [fn2.ext{,opt}]\n", progname);
  56.     fprintf(stderr, "flags: -m map         mapping in the form ?_to_? (default: i_to_l), where ? is\n");
  57.     fprintf(stderr, "                      i  physical intensitys (eg: raytracer output)\n");
  58.     fprintf(stderr, "                      p  gamma corrected for a specific monitor\n");
  59.     fprintf(stderr, "                      l  L* cyclometric linear perceived intensitys (as in PM)\n");
  60.     fprintf(stderr, "       -g gamma       set monitor gamma (default 2.1)\n");
  61.     fprintf(stderr, "       -s shelf       set monitor shelf (default 0.0)\n");
  62.     fprintf(stderr, "                      gamma and shelf only used for mapping to or from p\n");
  63.     fprintf(stderr, "                      for 8514 monitor gamma=2.3,shelf=0.136\n");
  64.     fprintf(stderr, "                      for 8515 monitor gamma=2.1,shelf=0.0\n");
  65.     fprintf(stderr, "       fn1.ext{,opt}  input filename (with any format specific options)\n");
  66.     fprintf(stderr, "       fn2.ext{,opt}  optional output filename (or will use fn1 if not present)\n");
  67.     fprintf(stderr, "                      ext's are used to deduce desired bitmap file formats\n");
  68.  
  69.     gbm_init();
  70.     gbm_query_n_filetypes(&n_ft);
  71.     for ( ft = 0; ft < n_ft; ft++ )
  72.         {
  73.         GBMFT gbmft;
  74.  
  75.         gbm_query_filetype(ft, &gbmft);
  76.         fprintf(stderr, "                      %s when ext in [%s]\n",
  77.             gbmft.short_name, gbmft.extensions);
  78.         }
  79.     gbm_deinit();
  80.  
  81.     fprintf(stderr, "       opt's          bitmap format specific options\n");
  82.  
  83.     exit(1);
  84.     }
  85. /*...e*/
  86. /*...sget_opt_double:0:*/
  87. static double get_opt_double(char *s, char *name)
  88.     {
  89.     double    v;
  90.  
  91.     if ( s == NULL )
  92.         fatal("missing %s argument", name);
  93.     sscanf(s, "%lf", &v);
  94.  
  95.     return ( v );
  96.     }
  97. /*...e*/
  98. /*...ssame:0:*/
  99. static BOOLEAN same(char *s1, char *s2, int n)
  100.     {
  101.     for ( ; n--; s1++, s2++ )
  102.         if ( tolower(*s1) != tolower(*s2) )
  103.             return ( FALSE );
  104.     return ( TRUE );
  105.     }
  106. /*...e*/
  107. /*...smain:0:*/
  108. /*...smapinfos:0:*/
  109. #define    CVT_NONE    0
  110. #define    CVT_I_TO_P    1
  111. #define    CVT_P_TO_I    2
  112. #define    CVT_I_TO_L    3
  113. #define    CVT_L_TO_I    4
  114. #define    CVT_P_TO_L    5
  115. #define    CVT_L_TO_P    6
  116.  
  117. typedef struct { char *name; int m; } MAPINFO;
  118.  
  119. static MAPINFO mapinfos [] =
  120.     {
  121.     "none",        CVT_NONE,
  122.     "i_to_p",    CVT_I_TO_P,
  123.     "p_to_i",    CVT_P_TO_I,
  124.     "i_to_l",    CVT_I_TO_L,
  125.     "l_to_i",    CVT_L_TO_I,
  126.     "p_to_l",    CVT_P_TO_L,
  127.     "l_to_p",    CVT_L_TO_P,
  128.     };
  129.  
  130. #define    N_MAPINFOS    (sizeof(mapinfos)/sizeof(mapinfos [0]))
  131. /*...e*/
  132.  
  133. /*...smap_compute:0:*/
  134. /*...slstar_from_i:0:*/
  135. static double lstar_from_i(double y)
  136.     {
  137.     y = pow(1.16 * y, 1.0/3.0) - 0.16;
  138.  
  139.     if ( y < 0.0 ) y = 0.0; else if ( y > 1.0 ) y = 1.0;
  140.  
  141.     return ( y );
  142.     }
  143. /*...e*/
  144. /*...si_from_lstar:0:*/
  145. static double i_from_lstar(double y)
  146.     {
  147.     y = pow(y + 0.16, 3.0) / 1.16;
  148.  
  149.     if ( y < 0.0 ) y = 0.0; else if ( y > 1.0 ) y = 1.0;
  150.  
  151.     return ( y );
  152.     }
  153. /*...e*/
  154. /*...spal_from_i:0:*/
  155. static double pal_from_i(double y, double gam, double shelf)
  156.     {
  157.     y = pow(y,1.0 / gam) * (1.0 - shelf) + shelf;
  158.  
  159.     if ( y < 0.0 ) y = 0.0; else if ( y > 1.0 ) y = 1.0;
  160.  
  161.     return ( y );
  162.     }
  163. /*...e*/
  164. /*...si_from_pal:0:*/
  165. static double i_from_pal(double y, double gam, double shelf)
  166.     {
  167.     if ( y >= shelf )
  168.         y = pow((y - shelf) / (1.0 - shelf), gam);
  169.     else
  170.         y = 0.0;
  171.  
  172.     if ( y < 0.0 ) y = 0.0; else if ( y > 1.0 ) y = 1.0;
  173.  
  174.     return ( y );
  175.     }
  176. /*...e*/
  177.  
  178. static void map_compute(int m, byte remap [], double gam, double shelf)
  179.     {
  180.     int    i;
  181.  
  182.     for ( i = 0; i < 0x100; i++ )
  183.         {
  184.         double y = (double) i / 255.0;
  185.  
  186.         switch ( m )
  187.             {
  188.             case CVT_I_TO_P: y = pal_from_i(y, gam, shelf); break;
  189.             case CVT_P_TO_I: y = i_from_pal(y, gam, shelf); break;
  190.             case CVT_I_TO_L: y = lstar_from_i(y); break;
  191.             case CVT_L_TO_I: y = i_from_lstar(y); break;
  192.             case CVT_P_TO_L: y = lstar_from_i(i_from_pal(y, gam, shelf)); break;
  193.             case CVT_L_TO_P: y = pal_from_i(i_from_lstar(y), gam, shelf); break;
  194.             }
  195.  
  196.         remap [i] = (byte) (y * 255.0);
  197.         }
  198.     }
  199. /*...e*/
  200. /*...smap_data:0:*/
  201. static void map_data(byte *data, int w, int h, byte remap [])
  202.     {
  203.     int stride = ((w * 3 + 3) & ~3);
  204.     int x, y;
  205.  
  206.     for ( y = 0; y < h; y++, data += stride )
  207.         for ( x = 0; x < w * 3; x++ )
  208.             data [x] = remap [data [x]];
  209.     }
  210. /*...e*/
  211. /*...smap_palette:0:*/
  212. static void map_palette(GBMRGB *gbmrgb, int npals, byte remap [])
  213.     {
  214.     for ( ; npals--; gbmrgb++ )
  215.         {
  216.         gbmrgb -> b = remap [gbmrgb -> b];
  217.         gbmrgb -> g = remap [gbmrgb -> g];
  218.         gbmrgb -> r = remap [gbmrgb -> r];
  219.         }
  220.     }
  221. /*...e*/
  222.  
  223. int    main(int argc, char *argv [])
  224.     {
  225.     char    fn_src [500+1], fn_dst [500+1], *opt_src, *opt_dst;
  226.     int    fd, ft_src, ft_dst, i, stride, bytes, flag, m;
  227.     GBM_ERR    rc;
  228.     GBMFT    gbmft;
  229.     GBM    gbm;
  230.     GBMRGB    gbmrgb [0x100];
  231.     byte    *data;
  232.     char    *map = "none";
  233.     byte    remap [0x100];
  234.     double gam = 2.1, shelf = 0.0;
  235.  
  236. /*...scommand line arguments:8:*/
  237. for ( i = 1; i < argc; i++ )
  238.     {
  239.     if ( argv [i][0] != '-' )
  240.         break;
  241.     switch ( argv [i][1] )
  242.         {
  243.         case 'm':
  244.             if ( ++i == argc )
  245.                 fatal("expected map argument");
  246.             map = argv [i];
  247.             break;
  248.         case 'g':
  249.             if ( ++i == argc ) usage();
  250.             gam = get_opt_double(argv [i], "gam");
  251.             if ( gam < 0.1 || gam > 10.0 )
  252.                 fatal("only gammas in the range 0.1 to 10.0 are sensible");
  253.             break;
  254.         case 's':
  255.             if ( ++i == argc ) usage();
  256.             shelf = get_opt_double(argv [i], "shelf");
  257.             break;
  258.         default:
  259.             usage();
  260.             break;
  261.         }
  262.     }
  263. /*...e*/
  264.  
  265. /*...sdeduce mapping and bits per pixel etc\46\:8:*/
  266. {
  267. int    j;
  268.  
  269. for ( j = 0; j < N_MAPINFOS; j++ )
  270.     if ( same(map, mapinfos [j].name, strlen(map) + 1) )
  271.         break;
  272. if ( j == N_MAPINFOS )
  273.     fatal("unrecognised mapping %s", map);
  274. m = mapinfos [j].m;
  275. }
  276. /*...e*/
  277.  
  278.     if ( i == argc )
  279.         usage();
  280.     strcpy(fn_src, argv [i++]);
  281.     strcpy(fn_dst, ( i == argc ) ? fn_src : argv [i++]);
  282.     if ( i < argc )
  283.         usage();
  284.  
  285.     if ( (opt_src = strchr(fn_src, ',')) != NULL )
  286.         *opt_src++ = '\0';
  287.     else
  288.         opt_src = "";
  289.  
  290.     if ( (opt_dst = strchr(fn_dst, ',')) != NULL )
  291.         *opt_dst++ = '\0';
  292.     else
  293.         opt_dst = "";
  294.  
  295.     gbm_init();
  296.  
  297.     if ( gbm_guess_filetype(fn_src, &ft_src) != GBM_ERR_OK )
  298.         fatal("can't guess bitmap file format for %s", fn_src);
  299.  
  300.     if ( gbm_guess_filetype(fn_dst, &ft_dst) != GBM_ERR_OK )
  301.         fatal("can't guess bitmap file format for %s", fn_dst);
  302.  
  303.     if ( (fd = open(fn_src, O_RDONLY | O_BINARY)) == -1 )
  304.         fatal("can't open %s", fn_src);
  305.  
  306.     if ( (rc = gbm_read_header(fn_src, fd, ft_src, &gbm, opt_src)) != GBM_ERR_OK )
  307.         {
  308.         close(fd);
  309.         fatal("can't read header of %s: %s", fn_src, gbm_err(rc));
  310.         }
  311.  
  312.     gbm_query_filetype(ft_dst, &gbmft);
  313.     switch ( gbm.bpp )
  314.         {
  315.         case 24:    flag = GBM_FT_W24;    break;
  316.         case 8:        flag = GBM_FT_W8;    break;
  317.         case 4:        flag = GBM_FT_W4;    break;
  318.         case 1:        flag = GBM_FT_W1;    break;
  319.         }
  320.     if ( (gbmft.flags & flag) == 0 )
  321.         {
  322.         close(fd);
  323.         fatal("output bitmap format %s does not support writing %d bpp data",
  324.             gbmft.short_name, gbm.bpp);
  325.         }
  326.  
  327.     if ( (rc = gbm_read_palette(fd, ft_src, &gbm, gbmrgb)) != GBM_ERR_OK )
  328.         {
  329.         close(fd);
  330.         fatal("can't read palette of %s: %s", fn_src, gbm_err(rc));
  331.         }
  332.  
  333.     stride = ( ((gbm.w * gbm.bpp + 31)/32) * 4 );
  334.     bytes = stride * gbm.h;
  335.     if ( (data = malloc(bytes)) == NULL )
  336.         {
  337.         close(fd);
  338.         fatal("out of memory allocating %d bytes for bitmap", bytes);
  339.         }
  340.  
  341.     if ( (rc = gbm_read_data(fd, ft_src, &gbm, data)) != GBM_ERR_OK )
  342.         {
  343.         close(fd);
  344.         fatal("can't read bitmap data of %s: %s", fn_src, gbm_err(rc));
  345.         }
  346.  
  347.     close(fd);
  348.  
  349.     map_compute(m, remap, gam, shelf);
  350.  
  351.     if ( gbm.bpp == 24 )
  352.         map_data(data, gbm.w, gbm.h, remap);
  353.     else
  354.         map_palette(gbmrgb, 1 << gbm.bpp, remap);
  355.  
  356.     if ( (fd = open(fn_dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, S_IREAD | S_IWRITE)) == -1 )
  357.         fatal("can't create %s", fn_dst);
  358.  
  359.     if ( (rc = gbm_write(fn_dst, fd, ft_dst, &gbm, gbmrgb, data, opt_dst)) != GBM_ERR_OK )
  360.         {
  361.         close(fd);
  362.         fatal("can't write %s: %s", fn_dst, gbm_err(rc));
  363.         }
  364.  
  365.     close(fd);
  366.  
  367.     free(data);
  368.  
  369.     gbm_deinit();
  370.  
  371.     return ( 0 );
  372.     }
  373. /*...e*/
  374.