function compress(IN, quantizemap, filename);

%initialize some values
[a,b] = size(IN);
iterations = quantizemap(1);

%file "filename" is opened for writing
handle = fopen(filename, 'w');

%important initial values are writen to the file
%first, the number of iterations for the wavelet transform

fwrite(handle, iterations, 'ubit3');    %note: this limits the number of iterations to 7

%now write out our quantization vector, 4 bits per value

fwrite(handle, quantizemap(2:iterations+2), 'ubit4');
%this allows up to 15 bits of quantization, more than we'll probably need.

%initialize quantization region info:

regiontop = 1;
regionbottom = a/(2^iterations);
regionleft = 1;
regionright = b/(2^iterations);

count = 0;

while count <= iterations,
  if count == 0

%we define the size of the first block of data, we'll allow 8 bits
	fwrite(handle, regionbottom, 'ubit8');
	fwrite(handle, regionright, 'ubit8');
%now figure out our number of quantization bits
	bits = quantizemap(count+2);
%set up the region we want to quantize
	tempA = IN(regiontop:regionbottom, regionleft:regionright);
%determine the scaling necessary
	Amin = min(min(tempA));
	Amax = max(max(tempA));
	Amean = round(mean(mean(tempA)));
%store these values, rounded, at 8 bits apiece
	fwrite(handle, Amin, 'bit9');  %because the min could be negative
	fwrite(handle, Amax, 'ubit8'); %because the max should always be positive
	fwrite(handle, Amean, 'bit9'); %because the mean could be negative
%now, quantize the region
	tempA = quantize(tempA, bits, Amin, Amax, Amean); %floor((tempA - Amin)*(2^bits)/(Amax-Amin));
%and write that region to a file
	bitstring = returnstring(bits);
	fwrite(handle, tempA, bitstring);
%note: we don't worry about zero bit quantization here
%since the first region is the most important, it would be foolish to
%quantize it to zero bits.

else
	regionheight = regionbottom - regiontop + 1;
	regionwidth = regionright - regionleft + 1;
	tempA = zeros(regionheight, regionwidth*3);
	tempA(:,1:regionwidth) = IN(1:regiontop - 1,regionleft:regionright);
	tempA(:,regionwidth+1:2*regionwidth) = IN(regiontop:regionbottom, 1:regionleft - 1);
	tempA(:, 2*regionwidth+1:3*regionwidth) = IN(regiontop:regionbottom, regionleft:regionright);
	Amean = round(mean(mean(tempA)));
	Amin = min(min(tempA));
	Amax = max(max(tempA));

%write the min and max to the file
	fwrite(handle, Amin, 'bit9');
	fwrite(handle, Amax, 'ubit8');
	fwrite(handle, Amean, 'bit9');
%quantize the region
	bits = quantizemap(count+2);
	bitstring = returnstring(bits);

%write the region to file
	if bitstring ~= 'null0'
		tempA = quantize(tempA, bits, Amin, Amax,Amean); %floor((tempA - Amin)*(2^bits)/(Amax-Amin));
		fwrite(handle, tempA, bitstring);	
	end
  end
  count = count + 1;
  regiontop = regionbottom + 1;
  regionbottom = regionbottom * 2;
  regionleft = regionright + 1;
  regionright = regionright*2;
end

fclose(handle);
