home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / image / codecs / raw_dec.cc < prev    next >
C/C++ Source or Header  |  1999-04-29  |  4KB  |  183 lines

  1. // raw_dec.cc: Decode raw image files
  2. // (c) George White, 1997
  3. // (c) Klaus Gebhardt, 1997
  4. // George White <aa056@chebucto.ns.ca>
  5. // Klaus Gebhardt <gebhardt@crunch.ikp.physik.th-darmstdt.de>
  6.  
  7. #include <octave/config.h>
  8.  
  9. #include <iostream.h>
  10.  
  11. #include <octave/lo-utils.h>
  12. #include <octave/lo-ieee.h>
  13. #include <octave/mx-base.h>
  14. #include <octave/str-vec.h>
  15.  
  16. #include <octave/defun-dld.h>
  17. #include <octave/error.h>
  18. #include <octave/gripes.h>
  19. #include <octave/help.h>
  20. //#include <octave/mappers.h>
  21. #include <octave/oct-fstrm.h>
  22. #include <octave/oct-iostrm.h>
  23. #include <octave/oct-map.h>
  24. #include <octave/oct-obj.h>
  25. #include <octave/oct-prcstrm.h>
  26. #include <octave/oct-stream.h>
  27. #include <octave/oct-strstrm.h>
  28. #include <octave/ops.h>
  29. #include <octave/ov-base.h>
  30. #include <octave/ov-typeinfo.h>
  31. #include <octave/ov.h>
  32. #include <octave/ov-base.h>
  33. #include <octave/ov-re-mat.h>
  34. #include <octave/pager.h>
  35. #include <octave/pr-output.h>
  36. #include <octave/symtab.h>
  37. #include <octave/variables.h>
  38.  
  39. #include <octave/oct-img.h>
  40.  
  41. #ifdef __EMX__
  42. static __inline__ unsigned short int
  43. swapbytes(unsigned short int x)
  44. {
  45.   register unsigned short int tmp __asm__ ("ax") = x;
  46.   __asm__ __volatile__ ("xchgb %%al,%%ah\n\t"   /* swap bytes */
  47.                 : "=a" (tmp) : "a" (tmp));
  48.   return(tmp);
  49. }
  50. #else
  51. static __inline__ unsigned short int
  52. swapbytes(unsigned short int x)
  53. {
  54.     return ( x<<8 ) | ( x >>8 );
  55. }
  56. #endif
  57.  
  58. DEFUN_DLD (raw_dec, args, ,
  59.   "raw_dec nh rows cols bpp (FILE)\n\
  60. \n\
  61. Decode raw image formats.\n\
  62.   nh -- number of header bytes\n\
  63.   rows -- number of Y-pixels\n\
  64.   cols -- number of X-pixels\n\
  65.   bpp -- bits per pixel (8 or [-]16)\n")
  66. {
  67.   octave_value_list retval;
  68.   retval (1) = -1.0;
  69.   retval (0) = -1.0;
  70.  
  71.   UINT nh, rows, cols;
  72.   int bpp;
  73.   int nargin = args.length ();
  74.  
  75.   if (nargin == 5)
  76.     {
  77.       if (!args(0).is_real_scalar ())
  78.     {
  79.       ::error ("raw_dec: first argument (header bytes) must be a number");
  80.       return retval;
  81.     }
  82.       nh = (unsigned int) args(0).double_value ();
  83.       if (!args(1).is_real_scalar ())
  84.     {
  85.       ::error ("raw_dec: second argument (rows) must be a number");
  86.       return retval;
  87.     }
  88.       rows = (unsigned int) args(1).double_value ();
  89.       if (!args(2).is_real_scalar ())
  90.     {
  91.       ::error ("raw_dec: third argument (cols) must be a number");
  92.       return retval;
  93.     }
  94.       cols = (unsigned int) args(2).double_value ();
  95.       if (!args(3).is_real_scalar ())
  96.     {
  97.       ::error ("raw_dec: forth argument (bpp) must be a number");
  98.       return retval;
  99.     }
  100.       bpp = (int) args(3).double_value ();
  101.       if ( !((bpp == 8)||(bpp==16)||(bpp==-16)) )
  102.         {
  103.       ::error ("raw_dec: forth argument (bpp) must be 8 or [-]16");
  104.       return retval;
  105.     }
  106.  
  107.       if (!args(4).is_string ())
  108.     {
  109.       ::error ("raw_dec: last argument must be a string");
  110.       return retval;
  111.     }
  112.  
  113.       string filename = args(4).string_value ();
  114.  
  115.       FILE *fp = fopen (filename.c_str (), "r");
  116.  
  117. // dummy color map:
  118.       if (fp)
  119.     {
  120.       INT map_nr = 1;
  121.       INT map_nc = 3;
  122.  
  123.       INT c, d;
  124.       OCTAVE x;
  125.       Matrix Map (map_nr, map_nc);
  126.  
  127.       for (c = 0; c < map_nr; c++)
  128.         {
  129.           for (d = 0; d < map_nc; d++)
  130.         {
  131.           Map(c, d) = 0;
  132.         }
  133.         }
  134.  
  135.       if ((cols < 1) || (rows < 1))
  136.         {
  137.           ::error ("raw_dec: image must be a real matrix");
  138.           fclose (fp);
  139.           return retval;
  140.         }
  141.  
  142.       UINT i, j;
  143.           unsigned char uc;
  144.           unsigned short int us;
  145.       Matrix X (rows, cols);
  146.  
  147.           if ( nh > 0 )
  148.         fseek(fp, (long)nh, SEEK_SET);
  149.  
  150.       for (i = 0; i < rows; i++)
  151.         {
  152.           for (j = 0; j < cols; j++)
  153.         {
  154.           if ( bpp == 8 )
  155.                      {
  156.                        fread((char *)&uc, 1, 1, fp);
  157.                        X (i, j) = (double) uc;
  158.                      }
  159.                   else /* |bpp| = 16 */
  160.                      {
  161.                        fread((char*)&us, 2, 1, fp);
  162.                        if ( bpp == -16 )
  163.                   X (i, j) = (double) (short int) swapbytes(us);
  164.                        else
  165.                     X (i, j) = (double) (short int) us;
  166.                      }
  167.         }
  168.         }
  169.  
  170.       fclose (fp);
  171.  
  172.       retval (1) = Map;
  173.       retval (0) = X;
  174.     }
  175.       else
  176.     ::error ("raw_dec: unable to open \"%s\"", filename.c_str ());
  177.     }
  178.   else
  179.     print_usage ("raw_dec");
  180.  
  181.   return retval;
  182. }
  183.