home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / rexx / library2 / gbmrexx / gbm / gbmiax.c < prev    next >
C/C++ Source or Header  |  1993-09-27  |  5KB  |  239 lines

  1. /*
  2.  
  3. GBMIAX.C  IBM Image Access eXecutive support
  4.  
  5. Reads array as 8 bit greyscale.
  6. Writes grey equivelent of passed in 8 bit colour data (no palette written).
  7. Input options: width=# (default: 512)
  8. Output options: r,g,b,k (default: k)
  9.  
  10. */
  11.  
  12. /*...sincludes:0:*/
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <stddef.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <memory.h>
  19. #include <malloc.h>
  20. #ifdef AIX
  21. #include <unistd.h>
  22. #else
  23. #include <io.h>
  24. #endif
  25. #include <fcntl.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include "standard.h"
  29. #include "gbm.h"
  30.  
  31. /*...vgbm\46\h:0:*/
  32. /*...e*/
  33.  
  34. /*...suseful:0:*/
  35. #define    low_byte(w)    ((byte)  ((w)&0x00ff)    )
  36. #define    high_byte(w)    ((byte) (((w)&0xff00)>>8))
  37. #define    make_word(a,b)    (((word)a) + (((word)b) << 8))
  38. /*...e*/
  39. /*...ssame:0:*/
  40. static BOOLEAN same(char *s1, char *s2, int n)
  41.     {
  42.     for ( ; n--; s1++, s2++ )
  43.         if ( tolower(*s1) != tolower(*s2) )
  44.             return ( FALSE );
  45.     return ( TRUE );
  46.     }
  47. /*...e*/
  48. /*...sfind_word:0:*/
  49. static char *find_word(char *str, char *substr)
  50.     {
  51.     char    buf [100+1], *s;
  52.     int    len = strlen(substr);
  53.  
  54.     for ( s  = strtok(strcpy(buf, str), " \t,");
  55.           s != NULL;
  56.           s  = strtok(NULL, " \t,") )
  57.         if ( same(s, substr, len) && s [len] == '\0' )
  58.             return ( str + (s - buf) );
  59.     return ( NULL );
  60.     }
  61. /*...e*/
  62. /*...sfind_word_prefix:0:*/
  63. static char *find_word_prefix(char *str, char *substr)
  64.     {
  65.     char    buf [100+1], *s;
  66.     int    len = strlen(substr);
  67.  
  68.     for ( s  = strtok(strcpy(buf, str), " \t,");
  69.           s != NULL;
  70.           s  = strtok(NULL, " \t,") )
  71.         if ( same(s, substr, len) )
  72.             return ( str + (s - buf) );
  73.     return ( NULL );
  74.     }
  75. /*...e*/
  76. /*...smake_output_palette:0:*/
  77. #define    SW4(a,b,c,d)    ((a)*8+(b)*4+(c)*2+(d))
  78.  
  79. static BOOLEAN make_output_palette(GBMRGB gbmrgb [], byte grey [], char *opt)
  80.     {
  81.     BOOLEAN    k = ( find_word(opt, "k") != NULL );
  82.     BOOLEAN    r = ( find_word(opt, "r") != NULL );
  83.     BOOLEAN    g = ( find_word(opt, "g") != NULL );
  84.     BOOLEAN    b = ( find_word(opt, "b") != NULL );
  85.     int    i;
  86.  
  87.     switch ( SW4(k,r,g,b) )
  88.         {
  89.         case SW4(0,0,0,0):
  90.             /* Default is the same as "k" */
  91.         case SW4(1,0,0,0):
  92.             for ( i = 0; i < 0x100; i++ )
  93.                 grey [i] = (byte) ( ((word) gbmrgb [i].r *  77 +
  94.                              (word) gbmrgb [i].g * 151 +
  95.                              (word) gbmrgb [i].b *  28) >> 8 );
  96.             return ( TRUE );
  97.         case SW4(0,1,0,0):
  98.             for ( i = 0; i < 0x100; i++ )
  99.                 grey [i] = gbmrgb [i].r;
  100.             return ( TRUE );
  101.         case SW4(0,0,1,0):
  102.             for ( i = 0; i < 0x100; i++ )
  103.                 grey [i] = gbmrgb [i].g;
  104.             return ( TRUE );
  105.         case SW4(0,0,0,1):
  106.             for ( i = 0; i < 0x100; i++ )
  107.                 grey [i] = gbmrgb [i].b;
  108.             return ( TRUE );
  109.         }
  110.     return ( FALSE );
  111.     }
  112. /*...e*/
  113.  
  114. static GBMFT iax_gbmft =
  115.     {
  116.     "IAX",
  117.     "IBM Image Access eXecutive",
  118.     "IAX",
  119.     GBM_FT_R8|
  120.     GBM_FT_W8,
  121.     };
  122.  
  123. #define    GBM_ERR_IAX_SIZE    ((GBM_ERR) 1500)
  124.  
  125. /*...siax_qft:0:*/
  126. GBM_ERR iax_qft(GBMFT *gbmft)
  127.     {
  128.     *gbmft = iax_gbmft;
  129.     return ( GBM_ERR_OK );
  130.     }
  131. /*...e*/
  132. /*...siax_rhdr:0:*/
  133. GBM_ERR iax_rhdr(char *fn, int fd, GBM *gbm, char *opt)
  134.     {
  135.     long    length;
  136.     int    w, h;
  137.     char    *width;
  138.  
  139.     fn=fn; fd=fd; /* Suppress 'unref arg' compiler warnings */
  140.  
  141.     length = lseek(fd, 0L, SEEK_END);
  142.     lseek(fd, 0L, SEEK_SET);
  143.  
  144.     if ( (width = find_word_prefix(opt, "width=")) != NULL )
  145.         sscanf(width + 6, "%d", &w);
  146.     else
  147.         w = 512;
  148.  
  149.     h = (int) (length / w);
  150.  
  151.     if ( w <= 0 || h <= 0 )
  152.         return ( GBM_ERR_BAD_SIZE );
  153.  
  154.     if ( w * h != length )
  155.         return ( GBM_ERR_IAX_SIZE );
  156.  
  157.     gbm -> w   = w;
  158.     gbm -> h   = h;
  159.     gbm -> bpp = 8;
  160.  
  161.     return ( GBM_ERR_OK );
  162.     }
  163. /*...e*/
  164. /*...siax_rpal:0:*/
  165. GBM_ERR iax_rpal(int fd, GBM *gbm, GBMRGB *gbmrgb)
  166.     {
  167.     int    i;
  168.  
  169.     fd=fd; gbm=gbm; /* Suppress 'unref arg' compiler warnings */
  170.  
  171.     for ( i = 0; i < 0x100; i++ )
  172.         gbmrgb [i].r =
  173.         gbmrgb [i].g =
  174.         gbmrgb [i].b = (byte) i;
  175.  
  176.     return ( GBM_ERR_OK );
  177.     }
  178. /*...e*/
  179. /*...siax_rdata:0:*/
  180. GBM_ERR iax_rdata(int fd, GBM *gbm, byte *data)
  181.     {
  182.     int    i, stride;
  183.     byte    *p;
  184.  
  185.     stride = ((gbm -> w + 3) & ~3);
  186.     p = data + ((gbm -> h - 1) * stride);
  187.     for ( i = gbm -> h - 1; i >= 0; i-- )
  188.         {
  189.         read(fd, p, gbm -> w);
  190.         p -= stride;
  191.         }
  192.     return ( GBM_ERR_OK );
  193.     }
  194. /*...e*/
  195. /*...siax_w:0:*/
  196. GBM_ERR iax_w(char *fn, int fd, GBM *gbm, GBMRGB *gbmrgb, byte *data, char *opt)
  197.     {
  198.     int    i, j, stride;
  199.     byte    grey [0x100];
  200.     byte    *p, *linebuf;
  201.  
  202.     fn=fn; /* Suppress 'unref arg' compiler warning */
  203.  
  204.     if ( gbm -> bpp != 8 )
  205.         return ( GBM_ERR_NOT_SUPP );
  206.  
  207.     if ( !make_output_palette(gbmrgb, grey, opt) )
  208.         return ( GBM_ERR_BAD_OPTION );
  209.  
  210.     if ( (linebuf = malloc(gbm -> w)) == NULL )
  211.         return ( GBM_ERR_MEM );
  212.  
  213.     stride = ((gbm -> w + 3) & ~3);
  214.     p = data + ((gbm -> h - 1) * stride);
  215.     for ( i = gbm -> h - 1; i >= 0; i-- )
  216.         {
  217.         for ( j = 0; j < gbm -> w; j++ )
  218.             linebuf [j] = grey [p [j]];
  219.         write(fd, linebuf, gbm -> w);
  220.         p -= stride;
  221.         }
  222.  
  223.     free(linebuf);
  224.  
  225.     return ( GBM_ERR_OK );
  226.     }
  227. /*...e*/
  228. /*...siax_err:0:*/
  229. char *iax_err(GBM_ERR rc)
  230.     {
  231.     switch ( (int) rc )
  232.         {
  233.         case GBM_ERR_IAX_SIZE:
  234.             return ( "file length is not a multiple of its width" );
  235.         }
  236.     return ( NULL );
  237.     }
  238. /*...e*/
  239.