home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gbmsrc.zip / gbmiax.c < prev    next >
C/C++ Source or Header  |  1996-12-15  |  4KB  |  196 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. #include "gbm.h"
  21. #include "gbmhelp.h"
  22.  
  23. /*...vgbm\46\h:0:*/
  24. /*...vgbmhelp\46\h:0:*/
  25. /*...e*/
  26.  
  27. /*...suseful:0:*/
  28. #define    low_byte(w)    ((byte)  ((w)&0x00ff)    )
  29. #define    high_byte(w)    ((byte) (((w)&0xff00)>>8))
  30. #define    make_word(a,b)    (((word)a) + (((word)b) << 8))
  31. /*...e*/
  32. /*...smake_output_palette:0:*/
  33. #define    SW4(a,b,c,d)    ((a)*8+(b)*4+(c)*2+(d))
  34.  
  35. static BOOLEAN make_output_palette(const GBMRGB gbmrgb[], byte grey[], const char *opt)
  36.     {
  37.     BOOLEAN    k = ( gbm_find_word(opt, "k") != NULL );
  38.     BOOLEAN    r = ( gbm_find_word(opt, "r") != NULL );
  39.     BOOLEAN    g = ( gbm_find_word(opt, "g") != NULL );
  40.     BOOLEAN    b = ( gbm_find_word(opt, "b") != NULL );
  41.     int    i;
  42.  
  43.     switch ( SW4(k,r,g,b) )
  44.         {
  45.         case SW4(0,0,0,0):
  46.             /* Default is the same as "k" */
  47.         case SW4(1,0,0,0):
  48.             for ( i = 0; i < 0x100; i++ )
  49.                 grey[i] = (byte) ( ((word) gbmrgb[i].r *  77U +
  50.                             (word) gbmrgb[i].g * 150U +
  51.                             (word) gbmrgb[i].b *  29U) >> 8 );
  52.             return TRUE;
  53.         case SW4(0,1,0,0):
  54.             for ( i = 0; i < 0x100; i++ )
  55.                 grey[i] = gbmrgb[i].r;
  56.             return TRUE;
  57.         case SW4(0,0,1,0):
  58.             for ( i = 0; i < 0x100; i++ )
  59.                 grey[i] = gbmrgb[i].g;
  60.             return TRUE;
  61.         case SW4(0,0,0,1):
  62.             for ( i = 0; i < 0x100; i++ )
  63.                 grey[i] = gbmrgb[i].b;
  64.             return TRUE;
  65.         }
  66.     return FALSE;
  67.     }
  68. /*...e*/
  69.  
  70. static GBMFT iax_gbmft =
  71.     {
  72.     "IAX",
  73.     "IBM Image Access eXecutive",
  74.     "IAX",
  75.     GBM_FT_R8|
  76.     GBM_FT_W8,
  77.     };
  78.  
  79. #define    GBM_ERR_IAX_SIZE    ((GBM_ERR) 1500)
  80.  
  81. /*...siax_qft:0:*/
  82. GBM_ERR iax_qft(GBMFT *gbmft)
  83.     {
  84.     *gbmft = iax_gbmft;
  85.     return GBM_ERR_OK;
  86.     }
  87. /*...e*/
  88. /*...siax_rhdr:0:*/
  89. GBM_ERR iax_rhdr(const char *fn, int fd, GBM *gbm, const char *opt)
  90.     {
  91.     long length;
  92.     int w, h;
  93.     const char *width;
  94.  
  95.     fn=fn; fd=fd; /* Suppress 'unref arg' compiler warnings */
  96.  
  97.     length = gbm_file_lseek(fd, 0L, SEEK_END);
  98.     gbm_file_lseek(fd, 0L, SEEK_SET);
  99.  
  100.     if ( (width = gbm_find_word_prefix(opt, "width=")) != NULL )
  101.         sscanf(width + 6, "%d", &w);
  102.     else
  103.         w = 512;
  104.  
  105.     h = (int) (length / w);
  106.  
  107.     if ( w <= 0 || h <= 0 )
  108.         return GBM_ERR_BAD_SIZE;
  109.  
  110.     if ( w * h != length )
  111.         return GBM_ERR_IAX_SIZE;
  112.  
  113.     gbm->w   = w;
  114.     gbm->h   = h;
  115.     gbm->bpp = 8;
  116.  
  117.     return GBM_ERR_OK;
  118.     }
  119. /*...e*/
  120. /*...siax_rpal:0:*/
  121. GBM_ERR iax_rpal(int fd, GBM *gbm, GBMRGB *gbmrgb)
  122.     {
  123.     int    i;
  124.  
  125.     fd=fd; gbm=gbm; /* Suppress 'unref arg' compiler warnings */
  126.  
  127.     for ( i = 0; i < 0x100; i++ )
  128.         gbmrgb[i].r =
  129.         gbmrgb[i].g =
  130.         gbmrgb[i].b = (byte) i;
  131.  
  132.     return GBM_ERR_OK;
  133.     }
  134. /*...e*/
  135. /*...siax_rdata:0:*/
  136. GBM_ERR iax_rdata(int fd, GBM *gbm, byte *data)
  137.     {
  138.     int    i, stride;
  139.     byte    *p;
  140.  
  141.     stride = ((gbm->w + 3) & ~3);
  142.     p = data + ((gbm->h - 1) * stride);
  143.     for ( i = gbm->h - 1; i >= 0; i-- )
  144.         {
  145.         gbm_file_read(fd, p, gbm->w);
  146.         p -= stride;
  147.         }
  148.     return GBM_ERR_OK;
  149.     }
  150. /*...e*/
  151. /*...siax_w:0:*/
  152. GBM_ERR iax_w(const char *fn, int fd, const GBM *gbm, const GBMRGB *gbmrgb, const byte *data, const char *opt)
  153.     {
  154.     int i, j, stride;
  155.     byte grey[0x100];
  156.     const byte *p;
  157.     byte *linebuf;
  158.  
  159.     fn=fn; /* Suppress 'unref arg' compiler warning */
  160.  
  161.     if ( gbm->bpp != 8 )
  162.         return GBM_ERR_NOT_SUPP;
  163.  
  164.     if ( !make_output_palette(gbmrgb, grey, opt) )
  165.         return GBM_ERR_BAD_OPTION;
  166.  
  167.     if ( (linebuf = malloc((size_t) gbm->w)) == NULL )
  168.         return GBM_ERR_MEM;
  169.  
  170.     stride = ((gbm->w + 3) & ~3);
  171.     p = data + ((gbm->h - 1) * stride);
  172.     for ( i = gbm->h - 1; i >= 0; i-- )
  173.         {
  174.         for ( j = 0; j < gbm->w; j++ )
  175.             linebuf[j] = grey[p[j]];
  176.         gbm_file_write(fd, linebuf, gbm->w);
  177.         p -= stride;
  178.         }
  179.  
  180.     free(linebuf);
  181.  
  182.     return GBM_ERR_OK;
  183.     }
  184. /*...e*/
  185. /*...siax_err:0:*/
  186. const char *iax_err(GBM_ERR rc)
  187.     {
  188.     switch ( (int) rc )
  189.         {
  190.         case GBM_ERR_IAX_SIZE:
  191.             return "file length is not a multiple of its width";
  192.         }
  193.     return NULL;
  194.     }
  195. /*...e*/
  196.