home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / graphic / csg_rt / fio.c < prev    next >
C/C++ Source or Header  |  1993-01-12  |  5KB  |  249 lines

  1. /*
  2.  
  3. FIO.C  New bitmap file IO
  4.  
  5. This version uses the Generalised Bitmap Module to do the I/O.
  6. This code is also portable, therefore the whole raytracer still is.
  7.  
  8. */
  9.  
  10. /*...sincludes:0:*/
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <stdlib.h>
  14. #include <stddef.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <memory.h>
  18. #include <malloc.h>
  19. #ifdef AIX
  20. #include <unistd.h>
  21. #else
  22. #include <io.h>
  23. #endif
  24. #include <fcntl.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include "standard.h"
  28. #include "gbm.h"
  29.  
  30. #ifndef O_BINARY
  31. #define    O_BINARY 0
  32. #endif
  33. /*...e*/
  34.  
  35. typedef struct
  36.     {
  37.     GBM gbm;
  38.     GBMRGB gbmrgb [0x100];
  39.     byte *data;
  40.     int stride;
  41.     int ref_count;
  42.     } BITMAP;
  43.  
  44. /*...sfio_init:0:*/
  45. void fio_init(void)
  46.     {
  47.     gbm_init();
  48.     }
  49. /*...e*/
  50. /*...sfio_deinit:0:*/
  51. void fio_deinit(void)
  52.     {
  53.     gbm_deinit();
  54.     }
  55. /*...e*/
  56. /*...sfio_create_bitmap:0:*/
  57. BITMAP *fio_create_bitmap(int w, int h)
  58.     {
  59.     BITMAP *bitmap;
  60.  
  61.     if ( (bitmap = malloc(sizeof(BITMAP))) == NULL )
  62.         return ( NULL );
  63.  
  64.     bitmap -> gbm.w   = w;
  65.     bitmap -> gbm.h   = h;
  66.     bitmap -> gbm.bpp = 24;
  67.     bitmap -> stride  = ((w * 24 + 31)/32)*4;
  68.     if ( (bitmap -> data = malloc(bitmap -> stride * h)) == NULL )
  69.         { free(bitmap); return ( NULL ); }
  70.  
  71.     /* Initialse surface to medium grey */
  72.     memset(bitmap -> data, 0x80, bitmap -> stride * h);
  73.  
  74.     bitmap -> ref_count = 1;
  75.  
  76.     return ( bitmap );
  77.     }
  78. /*...e*/
  79. /*...sfio_copy_bitmap:0:*/
  80. BITMAP *fio_copy_bitmap(BITMAP *bitmap)
  81.     {
  82.     (bitmap -> ref_count)++;
  83.     return ( bitmap );
  84.     }
  85. /*...e*/
  86. /*...sfio_destroy_bitmap:0:*/
  87. void fio_destroy_bitmap(BITMAP *bitmap)
  88.     {
  89.     if ( --(bitmap -> ref_count) == 0 )
  90.         {
  91.         free(bitmap -> data);
  92.         free(bitmap);
  93.         }
  94.     }
  95. /*...e*/
  96. /*...sfio_read_bitmap:0:*/
  97. BITMAP *fio_read_bitmap(char *fn)
  98.     {
  99.     BITMAP *bitmap;
  100.     char *opt;
  101.     int fd, ft;
  102.  
  103.     if ( (opt = strchr(fn, ',')) != NULL )
  104.         *opt++ = '\0';
  105.     else
  106.         opt = "";
  107.  
  108.     if ( gbm_guess_filetype(fn, &ft) != GBM_ERR_OK )
  109.         return ( NULL );
  110.  
  111.     if ( (fd = open(fn, O_RDONLY | O_BINARY)) == -1 )
  112.         return ( NULL );
  113.  
  114.     if ( (bitmap = malloc(sizeof(BITMAP))) == NULL )
  115.         { close(fd); return ( NULL ); }
  116.  
  117.     if ( gbm_read_header(fn, fd, ft, &(bitmap -> gbm), opt) != GBM_ERR_OK )
  118.         { free(bitmap); close(fd); return ( NULL ); }
  119.  
  120.     if ( gbm_read_palette(fd, ft, &(bitmap -> gbm), bitmap -> gbmrgb) != GBM_ERR_OK )
  121.         { free(bitmap); close(fd); return ( NULL ); }
  122.  
  123.     bitmap -> stride = ((bitmap -> gbm.w * bitmap -> gbm.bpp + 31)/32)*4;
  124.     if ( (bitmap -> data = malloc(bitmap -> stride * bitmap -> gbm.h)) == NULL )
  125.         { free(bitmap); close(fd); return ( NULL ); }
  126.  
  127.     if ( gbm_read_data(fd, ft, &(bitmap -> gbm), bitmap -> data) )
  128.         { free(bitmap); close(fd); return ( NULL ); }
  129.  
  130.     close(fd);
  131.  
  132.     bitmap -> ref_count = 1;
  133.  
  134.     return ( bitmap );
  135.     }
  136. /*...e*/
  137. /*...sfio_write_bitmap:0:*/
  138. BOOLEAN fio_write_bitmap(BITMAP *bitmap, char *fn)
  139.     {
  140.     char *opt;
  141.     int fd, ft;
  142.     GBMFT gbmft;
  143.  
  144.     if ( (opt = strchr(fn, ',')) != NULL )
  145.         *opt++ = '\0';
  146.     else
  147.         opt = "";
  148.  
  149.     if ( gbm_guess_filetype(fn, &ft) != GBM_ERR_OK )
  150.         return ( FALSE );
  151.  
  152.     gbm_query_filetype(ft, &gbmft);
  153.  
  154.     if ( (gbmft.flags & GBM_FT_W24) == 0 )
  155.         return ( FALSE );
  156.  
  157.     if ( (fd = open(fn, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1 )
  158.         return ( FALSE );
  159.  
  160.     if ( gbm_write(fn, fd, ft, &(bitmap -> gbm), bitmap -> gbmrgb,
  161.             bitmap -> data, opt) != GBM_ERR_OK )
  162.         { close(fd); unlink(fn); return ( FALSE ); }
  163.  
  164.     close(fd);
  165.  
  166.     return ( TRUE );
  167.     }
  168. /*...e*/
  169. /*...sfio_get_pixel:0:*/
  170. void fio_get_pixel(
  171.     BITMAP *bitmap,
  172.     int x, int y,
  173.     byte *r, byte *g, byte *b
  174.     )
  175.     {
  176.     byte inx, *data = bitmap -> data + y * bitmap -> stride;
  177.  
  178.     switch ( bitmap -> gbm.bpp )
  179.         {
  180. /*...s1:16:*/
  181. case 1:
  182.     inx = data [x >> 3];
  183.     inx >>= ( 7 - (x & 7) );
  184.     inx &= 1;    
  185.     inx ^= 1;            /* B/W reverse palette fix */
  186.     *b = bitmap -> gbmrgb [inx].b;
  187.     *g = bitmap -> gbmrgb [inx].g;
  188.     *r = bitmap -> gbmrgb [inx].r;
  189.     break;
  190. /*...e*/
  191. /*...s4:16:*/
  192. case 4:
  193.     inx = data [x >> 1];
  194.     if ( x & 1 )
  195.         inx &= 0x0f;
  196.     else
  197.         inx >>= 4;
  198.     *b = bitmap -> gbmrgb [inx].b;
  199.     *g = bitmap -> gbmrgb [inx].g;
  200.     *r = bitmap -> gbmrgb [inx].r;
  201.     break;
  202. /*...e*/
  203. /*...s8:16:*/
  204. case 8:
  205.     inx = data [x];
  206.     *b = bitmap -> gbmrgb [inx].b;
  207.     *g = bitmap -> gbmrgb [inx].g;
  208.     *r = bitmap -> gbmrgb [inx].r;
  209.     break;
  210. /*...e*/
  211. /*...s24:16:*/
  212. case 24:
  213.     data += (x * 3);
  214.     *b = *data++;
  215.     *g = *data++;
  216.     *r = *data;
  217.     break;
  218. /*...e*/
  219.         }
  220.     }
  221. /*...e*/
  222. /*...sfio_set_pixel:0:*/
  223. void fio_set_pixel(
  224.     BITMAP *bitmap,
  225.     int x, int y,
  226.     byte r, byte g, byte b
  227.     )
  228.     {
  229.     int stride = bitmap -> stride;
  230.     byte *data = bitmap -> data + y * stride + x * 3;
  231.  
  232.     *data++ = b;
  233.     *data++ = g;
  234.     *data   = r;
  235.     }
  236. /*...e*/
  237. /*...sfio_width:0:*/
  238. int fio_width(BITMAP *bitmap)
  239.     {
  240.     return ( bitmap -> gbm.w );
  241.     }
  242. /*...e*/
  243. /*...sfio_height:0:*/
  244. int fio_height(BITMAP *bitmap)
  245.     {
  246.     return ( bitmap -> gbm.h );
  247.     }
  248. /*...e*/
  249.