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

  1. // img_dec.cc: Decoding Octaves IMG format
  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 (img_dec, args, ,
  39.   "img_dec (FILE)\n\
  40. \n\
  41. Decode Octaves IMG format.")
  42. {
  43.   octave_value_list retval;
  44.   retval (1) = -1.0;
  45.   retval (0) = -1.0;
  46.  
  47.   char name[32];
  48.   char type[32];
  49.   char rows[32];
  50.   char cols[32];
  51.  
  52.   int nargin = args.length ();
  53.  
  54.   if (nargin == 1)
  55.     {
  56.       if (!args(0).is_string ())
  57.     {
  58.       ::error ("img_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 (), "r");
  65.  
  66.       if (fp)
  67.     {
  68.       INT map_nr = -1;
  69.       INT map_nc = -1;
  70.  
  71.       fgets (name, 32, fp);
  72.       fgets (type, 32, fp);
  73.       fgets (rows, 8, fp);
  74.       fscanf (fp, "%i\n", &map_nr);
  75.       fgets (cols, 11, fp);
  76.       fscanf (fp, "%i\n", &map_nc);
  77.  
  78.       if ((map_nc != 3) || (map_nr < 1))
  79.         {
  80.           ::error ("img_dec: colormap must be a real N x 3 matrix");
  81.           fclose (fp);
  82.           return retval;
  83.         }
  84.  
  85.       INT c, d;
  86.       OCTAVE x;
  87.       Matrix Map (map_nr, map_nc);
  88.  
  89.       for (c = 0; c < map_nr; c++)
  90.         {
  91.           for (d = 0; d < map_nc; d++)
  92.         {
  93.           fscanf (fp, "%lf ", &x);
  94.           Map(c, d) = x;
  95.         }
  96.         }
  97.  
  98.       UINT img_nr = 0;
  99.       UINT img_nc = 0;
  100.  
  101.       fgets (name, 32, fp);
  102.       fgets (type, 32, fp);
  103.       fgets (rows, 8, fp);
  104.       fscanf (fp, "%i\n", &img_nr);
  105.       fgets (cols, 11, fp);
  106.       fscanf (fp, "%i\n", &img_nc);
  107.  
  108.       if ((img_nc < 1) || (img_nr < 1))
  109.         {
  110.           ::error ("img_dec: image must be a real matrix");
  111.           fclose (fp);
  112.           return retval;
  113.         }
  114.  
  115.       UINT i, j;
  116.       Matrix X (img_nr, img_nc);
  117.  
  118.       for (i = 0; i < img_nr; i++)
  119.         {
  120.           for (j = 0; j < img_nc; j++)
  121.         {
  122.           fscanf (fp, "%lf", &x);
  123.           X (i, j) = x;
  124.         }
  125.         }
  126.  
  127.       fclose (fp);
  128.  
  129.       retval(1) = Map;
  130.       retval(0) = X;
  131.     }
  132.       else
  133.     ::error ("img_dec: unable to open \"%s\"", filename.c_str ());
  134.     }
  135.   else
  136.     print_usage ("img_dec");
  137.  
  138.   return retval;
  139. }
  140.