function imwrite_raw12(fname, im)
	% write the image specified in im to the filename fname
	% in raw 12 bit format
	% im ought to be a uint16 Nx x Ny matrix
	
	% convert to uint8 buffer
	% for some reason, fwrite(fid,'ubit12') is exceedingly slow
	% when compared to fwrite(fid,'uint8') or fwrite(fid,'uint16')
	
	assert(isa(im,'uint16'));
	
	Nbytes12				= 1.5 * numel(im);
	buffer_8				= zeros(1, Nbytes12, 'uint8');
	
	% convert pairs of 12 bit words (stored in the LSBs of pairs of uint16 words)
	% to triplets of 8 bit words
	buffer_8(1:3:end) = bitshift(im(1:2:end), -4);				% 8 MSB of every first word
	buffer_8(3:3:end) = bitand(im(2:2:end), uint16(255));		% 8 LSB of every second word
	buffer_8(2:3:end) = bitshift(bitand(im(1:2:end), uint16(15  )),  4) ... % 4 LSB of every first word
							+ bitshift(im(2:2:end), -8);									% 4 MSB of every second word
	
	% save 8 bit buffer to file
	fid					= fopen(fname, 'w');
	if fid == -1
		warning('bad file name');
		return;
	end
	fwrite(fid, buffer_8, 'uint8');
	fclose(fid);
end