bmpwrite.m
上传用户:m_sun_001
上传日期:2014-07-30
资源大小:1115k
文件大小:2k
源码类别:

matlab例程

开发平台:

Matlab

  1. function bmpwrite(X,map, filename);
  2. %BMPWRITE Write a BMP (Microsoft Windows Bitmap) file to disk.
  3. % BMPWRITE(X,MAP,'filename') writes a BMP file containing the
  4. % indexed image X and colormap MAP to a disk file called 'filename'.
  5. % If no file extension is given with the filename, the extension
  6. % '.bmp' is assumed.
  7. %
  8. % See also: BMPREAD, GIFWRITE, HDFWRITE, PCXWRITE, TIFFWRITE,
  9. %           XWDWRITE.
  10. % Mounil Patel 3/10/94
  11. % Copyright (c) 1994 by The MathWorks, Inc.
  12. % $Revision: 1.13 $  $Date: 1995/02/09 15:28:11 $
  13. if (nargin~=3)
  14. error('Requires three arguments.');
  15. end;
  16. if (isstr(filename)~=1)
  17. error('Requires a string filename as the third argument.');
  18. end;
  19. if (isempty(findstr(filename,'.'))==1)
  20. filename=[filename,'.bmp'];
  21. end;
  22. fid=fopen(filename,'wb','l');
  23. if (fid==-1)
  24. error(['Error opening ',filename,' for output.']);
  25. end;
  26. fwrite(fid,[66;77],'uchar');
  27. [biHeight,biWidth]=size(X);
  28. % What will be the physical file size?  First term: image data,
  29. % with width padded to be multiple of 4.  Second term: length of
  30. % bitmap header.  Third term: length of bitmap information
  31. % header.  Third term: length of color palette.
  32. bfSize = biHeight*(biWidth+(4-rem(biWidth,4))) + 14 + 40 + 256*4;
  33. fwrite(fid,bfSize,'uint');
  34. bfReserved1=0;
  35. fwrite(fid,bfReserved1,'ushort');
  36. bfReserved2=0;
  37. fwrite(fid,bfReserved2,'ushort');
  38. bfOffBytes=1078;
  39. fwrite(fid,bfOffBytes,'uint');
  40. biSize=40;
  41. fwrite(fid,biSize,'uint');
  42. fwrite(fid,biWidth,'uint');
  43. fwrite(fid,biHeight,'uint');
  44. biPlanes=1;
  45. fwrite(fid,biPlanes,'ushort');
  46. biBitCount=8;
  47. fwrite(fid,biBitCount,'ushort');
  48. biCompression=0;
  49. fwrite(fid,biCompression,'uint');
  50. biSizeImage=biHeight*(biWidth+(4-rem(biWidth,4)));
  51. fwrite(fid,biSizeImage,'uint');
  52. biXPels=0;
  53. fwrite(fid,biXPels,'uint');
  54. biYPels=0;
  55. fwrite(fid,biYPels,'uint');
  56. biClrUsed=256;
  57. fwrite(fid,biClrUsed,'uint');
  58. biClrImportant=0;
  59. fwrite(fid,biClrImportant,'uint');
  60. [m,n]=size(map);
  61. if (m<256)
  62. map=[map;zeros(256-m,3)];
  63. elseif (m>256)
  64.  error('Colormap exceeds 256 colors! Only 8-bit BMP file output is supported.');
  65. end;
  66. map=[fliplr(map*255),zeros(256,1)]';
  67. fwrite(fid,map(:),'uchar');
  68. X=(X-1);
  69. if (rem(biWidth,4)~=0)
  70. X=[X,zeros(biHeight,4-rem(biWidth,4))];
  71. end;
  72. X=rot90(X,3);
  73. fwrite(fid,X(:),'uchar');
  74. fclose(fid);