]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/bmpwrite.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / bmpwrite.m
1 ## -*- texinfo -*-
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.
8 ## @end deftypefn
9
10 ## This code is in the public domain.
11 ## Author: Paul Kienzle <pkienzle@users.sf.net>
12
13
14 function bmpwrite(x,colormap_or_file,file)
15   if nargin==2
16      bmpwrite_truecolor(x(:,:,1),x(:,:,2),x(:,:,3),colormap_or_file);
17   else
18      bmpwrite_indexed(x,colormap_or_file,file);
19   endif
20 endfunction
21
22 function bmpwrite_truecolor(R,G,B,file)
23     h = rows(R); w = columns(R);
24     padw = ceil(3*w/4)*4-3*w;
25     header = 14+40;
26     filesize = header+h*(3*w+padw);
27     arch = "ieee-le";
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
33
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
46
47     ## raster image, lines written bottom to top.
48     R = R(end:-1:1,:)';
49     G = G(end:-1:1,:)';
50     B = B(end:-1:1,:)';
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);
54     fclose(file);
55 endfunction
56
57 function bmpwrite_indexed(x,map,file)
58
59     if rows(map) > 256, 
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,
63                          file);
64       return;
65     endif
66     [h,w] = size(x);
67     padw = ceil(w/4)*4-w;
68     header = 14+40+4*rows(map);
69     filesize = header+(w+padw)*h;
70     arch = "ieee-le";
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
76
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
89
90     ## colormap BGR0BGR0BGR0BGR0...
91     map=[round(map*255), zeros(rows(map),1)];
92     map=map(:,[3,2,1,4]);
93     fwrite(file,map',"uchar",0,arch);
94
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);
98     fclose(file);
99 endfunction