home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / IMWRITE.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.2 KB  |  44 lines

  1. function imwrite(X,map,filename)
  2. %IMWRITE Write an image file.
  3. %    IMWRITE(X,map,'filename.raw') writes a "raw" image file used
  4. %    by the Image Alchemy program from Handmade Software, Inc.
  5. %    The image is in X and its colormap is in map.
  6. %
  7. %    See also IMREAD, IMAGE, COLORMAP.
  8.  
  9. %    C. Moler, 6/18/91, 9/19/91, 6/24/92.
  10. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  11.  
  12. if nargin < 3, 
  13.     filename = map;
  14.     map = colormap;
  15. end
  16. if size( map, 2 ) ~= 3
  17.     error('Invalid colormap');
  18. end
  19. suffix = filename(length(filename)-2:length(filename));
  20.  
  21. if strcmp(suffix,'raw')
  22.    % Write the 32-byte header
  23.    fp = fopen(filename,'wb','b');  % "Big-endian" byte order.
  24.    if (fp<0) error(['Cannot open ' filename '.']), end
  25.    [n,m] = size(X);
  26.    p = size(map,1);
  27.    fwrite(fp,'mhwanh','uchar');
  28.    fwrite(fp,4,'short');
  29.    fwrite(fp,m,'short');
  30.    fwrite(fp,n,'short');
  31.    fwrite(fp,p,'short');
  32.    fwrite(fp,zeros(1,18),'uchar');
  33.    
  34.    % Scale and write the palette (tranpose produces row oriented output).
  35.    map = min(255,fix(256*map));
  36.    fwrite(fp,map','uchar');
  37.  
  38.    % Write the image, based at 0 instead of 1.
  39.    fwrite(fp,X'-1,'uchar');
  40.    fclose(fp);
  41. else
  42.    error('Image file name must end in ''raw''.')
  43. end
  44.