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

  1. // pnm_dec.cc: Reading PNM files
  2. // (c) Klaus Gebhardt, 1997
  3.  
  4. #include <octave/config.h>
  5.  
  6. #include <iostream.h>
  7.  
  8. #include <octave/lo-utils.h>
  9. #include <octave/lo-ieee.h>
  10. #include <octave/mx-base.h>
  11. #include <octave/str-vec.h>
  12.  
  13. #include <octave/defun-dld.h>
  14. #include <octave/error.h>
  15. #include <octave/gripes.h>
  16. #include <octave/help.h>
  17. //#include <octave/mappers.h>
  18. #include <octave/oct-fstrm.h>
  19. #include <octave/oct-iostrm.h>
  20. #include <octave/oct-map.h>
  21. #include <octave/oct-obj.h>
  22. #include <octave/oct-prcstrm.h>
  23. #include <octave/oct-stream.h>
  24. #include <octave/oct-strstrm.h>
  25. #include <octave/ops.h>
  26. #include <octave/ov-base.h>
  27. #include <octave/ov-typeinfo.h>
  28. #include <octave/ov.h>
  29. #include <octave/ov-base.h>
  30. #include <octave/ov-re-mat.h>
  31. #include <octave/pager.h>
  32. #include <octave/pr-output.h>
  33. #include <octave/symtab.h>
  34. #include <octave/variables.h>
  35.  
  36. #include <octave/oct-img.h>
  37.  
  38. DEFUN_DLD (pnm_dec, args, ,
  39.   "pnm_dec (FILE)\n\
  40. \n\
  41. Decode PNM format file FILE.")
  42. {
  43.   octave_value_list retval;
  44.   retval (1) = -1.0;
  45.   retval (0) = -1.0;
  46.  
  47.   UCHAR **cmap;
  48.   INT **x;
  49.   INT col_min, col_max;
  50.   UINT i, j, nr, nc;
  51.  
  52.   int nargin = args.length ();
  53.  
  54.   if (nargin == 1)
  55.     {
  56.       if (!args(0).is_string ())
  57.     {
  58.       ::error ("pnm_dec: first argument must be a string");
  59.       return retval;
  60.     }
  61.  
  62.       string filename = args(0).string_value ();
  63.  
  64.       FILE *fp = fopen (filename.c_str (), "rb");
  65.  
  66.       if (fp)
  67.     {
  68.       if (PNM_Decode (fp, &nc, &nr, &col_min, &col_max, &cmap, &x) != 0)
  69.         {
  70.           fclose (fp);
  71.           ::error ("pnm_dec: error reading PNM");
  72.           return retval;
  73.         }
  74.  
  75.           Matrix Map (col_max - col_min + 1, 3);
  76.       oct_colormap (Map, cmap, col_min, col_max);
  77.           retval (1) = Map;
  78.  
  79.       Matrix X (nr, nc);
  80.       oct_pixels (X, x, col_min, nr, nc);
  81.       retval (0) = X;
  82.  
  83.       fclose (fp);
  84.     }
  85.       else
  86.     ::error ("pnm_dec: unable to open \"%s\"", filename.c_str ());
  87.     }
  88.   else
  89.     print_usage ("pnm_dec");
  90.  
  91.   return retval;
  92. }
  93.