2 ## @deftypefn {Function File} bmpwrite (@var{X}, @var{map}, @var{file})
3 ## Write the bitmap @var{X} into @var{file} (8-bit indexed uncompressed).
4 ## The values in @var{X} are indices into the given RGB colour @var{map}.
5 ## @deftypefnx{Function File} bmpwrite (@var{X}, @var{file})
6 ## Write the bitmap @var{X} into @var{file} (24-bit truecolor uncompressed).
7 ## @var{X} is an m x n x 3 array of R,G,B integer values in the range 0 to 255.
10 ## This code is in the public domain.
11 ## Author: Paul Kienzle <pkienzle@users.sf.net>
14 function bmpwrite(x,colormap_or_file,file)
16 bmpwrite_truecolor(x(:,:,1),x(:,:,2),x(:,:,3),colormap_or_file);
18 bmpwrite_indexed(x,colormap_or_file,file);
22 function bmpwrite_truecolor(R,G,B,file)
23 h = rows(R); w = columns(R);
24 padw = ceil(3*w/4)*4-3*w;
26 filesize = header+h*(3*w+padw);
28 file = fopen(file, "wb");
29 fwrite(file,toascii("BM"),"uchar",0,arch); # file tag
30 fwrite(file,filesize,"long",0,arch); # length of file
31 fwrite(file,0,"long",0,arch); # reserved
32 fwrite(file,header,"long",0,arch); # offset of raster data in file
34 fwrite(file,40,"long",0,arch); # header size
35 fwrite(file,w,"long",0,arch); # image width
36 fwrite(file,h,"long",0,arch); # image height
37 fwrite(file,1,"short",0,arch); # number of planes
38 fwrite(file,24,"short",0,arch); # pixels per plane
39 fwrite(file,0,"long",0,arch); # compression (none)
40 fwrite(file,0,"long",0,arch); # compressed size of image
41 resolution = 72/2.54*100; # 72 dpi / 2.54 cm/in * 100 cm/m
42 fwrite(file,resolution,"long",0,arch); # horizontal resolution
43 fwrite(file,resolution,"long",0,arch); # vertical resolution
44 fwrite(file,0,"long",0,arch); # number of colours used
45 fwrite(file,0,"long",0,arch); # number of "important" colors
47 ## raster image, lines written bottom to top.
51 RGB=[B(:),G(:),R(:)]'; # Now [[B;G;R],[B;G;R],...,[B;G;R]]
52 RGB=reshape(RGB,3*w,h); # Now [[B;G;R;...;B;G;R],...,[B;G;R;...;B;G;R]]
53 fwrite(file,[RGB;zeros(padw,h)],"uchar",0,arch);
57 function bmpwrite_indexed(x,map,file)
60 bmpwrite_truecolor(reshape(map(x,1),size(x))*255,
61 reshape(map(x,2),size(x))*255,
62 reshape(map(x,3),size(x))*255,
68 header = 14+40+4*rows(map);
69 filesize = header+(w+padw)*h;
71 file = fopen(file, "wb");
72 fwrite(file,toascii("BM"),"uchar",0,arch); # file tag
73 fwrite(file,filesize,"long",0,arch); # length of file
74 fwrite(file,0,"long",0,arch); # reserved
75 fwrite(file,header,"long",0,arch); # offset of raster data in file
77 fwrite(file,40,"long",0,arch); # header size
78 fwrite(file,w,"long",0,arch); # image width
79 fwrite(file,h,"long",0,arch); # image height
80 fwrite(file,1,"short",0,arch); # number of planes
81 fwrite(file,8,"short",0,arch); # pixels per plane
82 fwrite(file,0,"long",0,arch); # compression (none)
83 fwrite(file,0,"long",0,arch); # compressed size of image
84 resolution = 72/2.54*100; # 72 dpi / 2.54 cm/in * 100 cm/m
85 fwrite(file,resolution,"long",0,arch); # horizontal resolution
86 fwrite(file,resolution,"long",0,arch); # vertical resolution
87 fwrite(file,rows(map),"long",0,arch); # number of colours used
88 fwrite(file,0,"long",0,arch); # number of "important" colors
90 ## colormap BGR0BGR0BGR0BGR0...
91 map=[round(map*255), zeros(rows(map),1)];
93 fwrite(file,map',"uchar",0,arch);
95 ## raster image, each line on a 32-bit boundary, padded with zeros
96 ## lines written bottom to top.
97 fwrite(file,[flipud(x-1)';zeros(padw,h)],"uchar",0,arch);