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

  1. function [X,map] = imread(filename);
  2. %IMREAD    Read an image file.
  3. %    IMREAD('imagefile.raw') reads a "raw" image file produced by the
  4. %    Image Alchemy program from Handmade Software, Inc.
  5. %    [X,map] = IMREAD('imagefile.raw') returns both the image and a
  6. %    color map, so that
  7. %            [X,map] = imread('imagefile.raw');
  8. %            image(X)
  9. %            colormap(map)
  10. %    will display the result with the proper colors.
  11. %
  12. %    Image Alchemy can be used to convert a GIF file, for example, with
  13. %            alchemy -r -c 128 -z 4 imagefile.gif
  14. %    The relevant options are:
  15. %            -r      Produce a "raw" file.
  16. %            -c 128  Limit the color map length to 128 colors.
  17. %            -z 4    Sort color map by luminance, so colormap(gray)
  18. %                    works pretty well.
  19. %
  20. %    See also IMWRITE, IMAGE, COLORMAP.
  21.  
  22. %    C. Moler, 8/9/91, 10/28/92.
  23. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  24.  
  25. dot = max(find(filename == '.'));
  26. suffix = filename(dot+1:dot+3);
  27.  
  28. if strcmp(suffix,'raw')
  29.    % Read and crack the 32-byte header
  30.    
  31.    fp = fopen(filename,'rb','b');  % "Big-endian" byte order.
  32.    if (fp<0) error(['Cannot open ' filename '.']), end
  33.    
  34.    head = fread(fp,6,'uchar')';
  35.    verno = fread(fp,1,'ushort');
  36.    n = fread(fp,1,'ushort');
  37.    m = fread(fp,1,'ushort');
  38.    p = fread(fp,1,'ushort');
  39.    notused = fread(fp,18,'uchar');
  40.    
  41.    if ~strcmp(head,'mhwanh') | (verno ~= 4)
  42.       disp(['header = ' head]);
  43.       disp(['verno  = ' int2str(verno)]);
  44.       error([filename ' is not an HSI version 4 raw image ' ...
  45.              'from Image Alchemy version 1.5.']);
  46.    end
  47.    
  48.    % Read the palette and scale to [0,1).
  49.    
  50.    map = fread(fp,[3,p],'uchar')'/256;
  51.    
  52.    % Read the image
  53.    
  54.    [X,l] = fread(fp,[n,m],'uchar');
  55.    if l ~= m*n, l, error('HSI image file is wrong length'), end
  56.    % Image elements are colormap indices, so start at 1.
  57.    X = X'+1;
  58.    fclose(fp);
  59. else
  60.    error('Image file name must end in ''raw''.')
  61. end
  62.