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_enc.cc < prev    next >
C/C++ Source or Header  |  1999-04-29  |  3KB  |  127 lines

  1. // img_enc.cc: Writing in 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_enc, args, ,
  39.   "img_enc (FILE, COLORMAP, IMG)\n\
  40. \n\
  41. Write the image in Octaves IMG format to the file FILE.")
  42. {
  43.   double retval = -1.0;
  44.  
  45.   int nargin = args.length ();
  46.  
  47.   if (nargin == 3)
  48.     {
  49.       if (!args(0).is_string ())
  50.     {
  51.       ::error ("img_enc: first argument must be a string");
  52.       return retval;
  53.     }
  54.  
  55.       string filename = args(0).string_value ();
  56.  
  57.       FILE *fp = fopen (filename.c_str (), "w");
  58.  
  59.       if (fp)
  60.     {
  61.       octave_value map = args(1);
  62.       INT map_nr = map.rows ();
  63.       INT map_nc = map.columns ();
  64.  
  65.       if (!map.is_matrix_type () || !map.is_real_type () ||
  66.           (map_nc != 3) || (map_nr < 1))
  67.         {
  68.           ::error ("img_enc: colormap must be a real N x 3 matrix");
  69.           return retval;
  70.         }
  71.  
  72.       octave_value img = args(2);
  73.       UINT img_nr = img.rows ();
  74.       UINT img_nc = img.columns ();
  75.  
  76.       if (!img.is_matrix_type () || !img.is_real_type () ||
  77.           (img_nc < 1) || (img_nr < 1))
  78.         {
  79.           ::error ("img_enc: img must be a real matrix");
  80.           return retval;
  81.         }
  82.  
  83.       INT c;
  84.       Matrix Map = map.matrix_value ();
  85.  
  86.       fprintf (fp, "# name: map\n");
  87.       fprintf (fp, "# name: matrix\n");
  88.       fprintf (fp, "# rows: %i\n",    map_nr);
  89.       fprintf (fp, "# columns: %i", map_nc);
  90.  
  91.       for (c = 0; c < map_nr; c++)
  92.         fprintf(fp, "\n%7.5f %7.5f %7.5f",
  93.             Map (c, 0), Map (c, 1), Map (c, 2));
  94.  
  95.       UINT i, j, k;
  96.       Matrix X   = img.matrix_value ();
  97.  
  98.       fprintf (fp, "\n# name: X");
  99.       fprintf (fp, "\n# name: matrix");
  100.       fprintf (fp, "\n# rows: %i",    img_nr);
  101.       fprintf (fp, "\n# columns: %i", img_nc);
  102.  
  103.       for (i = 0; i < img_nr; i++)
  104.         {
  105.           k = 0;
  106.  
  107.           for (j=0; j<img_nc; j++)
  108.         {
  109.           if (k == 0)     fprintf (fp, "\n");
  110.           if (++k == 20)  k = 0;
  111.           fprintf(fp, "%3.0f ", X (i, j));
  112.         }
  113.         }
  114.  
  115.       fclose (fp);
  116.  
  117.       retval = 0.0;
  118.     }
  119.       else
  120.     ::error ("img_enc: unable to open \"%s\"", filename.c_str ());
  121.     }
  122.   else
  123.     print_usage ("img_enc");
  124.  
  125.   return retval;
  126. }
  127.