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

  1. // pnm_enc.cc: Writing a PNM file.
  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_enc, args, ,
  39.   "pnm_enc (FILE, COLORMAP, IMG)\n\
  40. \n\
  41. Write the image in PNM format to the file FILE.")
  42. {
  43.   double retval = -1.0;
  44.  
  45.   UINT nr, nc;
  46.   INT colors;
  47.   UCHAR **cm;
  48.   INT **x;
  49.   INT grey;
  50.  
  51.   int nargin = args.length ();
  52.  
  53.   if ((nargin == 3) || (nargin == 4))
  54.     {
  55.       if (!args(0).is_string ())
  56.     {
  57.       ::error ("pnm_enc: first argument must be a string");
  58.       return retval;
  59.     }
  60.  
  61.       string filename = args(0).string_value ();
  62.  
  63.       FILE *fp = fopen (filename.c_str (), "wb");
  64.  
  65.       if (fp)
  66.     {
  67.       cm = gif_colormap (args(1), &colors, &grey);
  68.       if (!cm)
  69.         {
  70.           fclose (fp);
  71.           ::error ("pnm_enc: error calculating colormap");
  72.           return retval;
  73.         }
  74.  
  75.       x = gif_pixels (args(2), colors, &nr, &nc);
  76.       if (!x)
  77.         {
  78.           free (cm);
  79.           fclose (fp);
  80.           ::error ("pnm_enc: error calculating pixels");
  81.           return retval;
  82.         }
  83.  
  84.           if (PNM_Encode (fp, grey, nc, nr, x, colors, cm) != 0)
  85.         {
  86.           free (cm);
  87.           free (x);
  88.           fclose (fp);
  89.           ::error ("pnm_enc: error writing bitmap");
  90.           return retval;
  91.         }
  92.  
  93.       free (cm);
  94.       free (x);
  95.       fclose (fp);
  96.  
  97.       retval = 0.0;
  98.     }
  99.       else
  100.     ::error ("pnm_enc: unable to open \"%s\"", filename.c_str ());
  101.     }
  102.   else
  103.     print_usage ("pnm_enc");
  104.  
  105.   return retval;
  106. }
  107.