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

  1. // bmp_enc.cc: Writing an OS/2 bitmap 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 (bmp_enc, args, ,
  39.   "bmp_enc (FILE, COLORMAP, IMG [,TYPE])\n\
  40. \n\
  41. Write the image in OS/2 or Windows bitmap 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 type = BMP_OS2;
  50.  
  51.   int nargin = args.length ();
  52.  
  53.   if (nargin == 4)
  54.     {
  55.       if (!args(3).is_string ())
  56.     {
  57.       ::error ("bmp_enc: fourth argument must be a string");
  58.       return retval;
  59.     }
  60.       
  61.       if (args(3).string_value () == "windows")   type = BMP_WIN;
  62.       else if (args(3).string_value () == "os2")  type = BMP_OS2;
  63.       else
  64.     {
  65.       ::error ("bmp_enc: fourth argument must be \"windows\" or \"os2\"");
  66.       return retval;
  67.     }
  68.     }
  69.  
  70.   if ((nargin == 3) || (nargin == 4))
  71.     {
  72.       if (!args(0).is_string ())
  73.     {
  74.       ::error ("bmp_enc: first argument must be a string");
  75.       return retval;
  76.     }
  77.  
  78.       string filename = args(0).string_value ();
  79.  
  80.       FILE *fp = fopen (filename.c_str (), "wb");
  81.  
  82.       if (fp)
  83.     {
  84.       cm = gif_colormap (args(1), &colors, NULL);
  85.       if (!cm)
  86.         {
  87.           fclose (fp);
  88.           ::error ("bmp_enc: error calculating colormap");
  89.           return retval;
  90.         }
  91.  
  92.       x = gif_pixels (args(2), colors, &nr, &nc);
  93.       if (!x)
  94.         {
  95.           free (cm);
  96.           fclose (fp);
  97.           ::error ("bmp_enc: error calculating pixels");
  98.           return retval;
  99.         }
  100.  
  101.           if (BMP_Encode (fp, type, nc, nr, x, colors, cm) != 0)
  102.         {
  103.           free (cm);
  104.           free (x);
  105.           fclose (fp);
  106.           ::error ("bmp_enc: error writing bitmap");
  107.           return retval;
  108.         }
  109. #
  110.       free (cm);
  111.       free (x);
  112.       fclose (fp);
  113.  
  114.       retval = 0.0;
  115.     }
  116.       else
  117.     ::error ("bmp_enc: unable to open \"%s\"", filename.c_str ());
  118.     }
  119.   else
  120.     print_usage ("bmp_enc");
  121.  
  122.   return retval;
  123. }
  124.