function OUT = decompress(filename);

%read in major file information
handle = fopen(filename, 'r');
iterations = fread(handle, 1, 'ubit3');
vec = fread(handle, iterations+1, 'ubit4');

%read in info on first sector
regionheight = fread(handle, 1, 'ubit8');
regionwidth = fread(handle, 1, 'ubit8');

%now we can initialize our output matrix

OUT = zeros(regionheight*2^iterations, regionwidth*2^iterations);

count = 0;
regionleft = 1;
regionright = regionwidth;
regiontop = 1;
regionbottom = regionheight;

while count <= iterations
  if count == 0
	Amin = fread(handle, 1, 'bit9');  
	Amax = fread(handle, 1, 'ubit8');
	Amean = fread(handle, 1, 'bit9');
	bits = vec(count+1);
	bitstring = returnstring(bits);
	tempA = fread(handle, [regionheight, regionwidth], bitstring);
	tempA = dequantize(tempA, bits, Amin, Amax, Amean);
	OUT(1:regionbottom, 1:regionright) = tempA; %(Amax-Amin)/(2^(bits+1)) +Amin + tempA*(Amax-Amin)/(2^bits);
  else
	Amin = fread(handle, 1, 'bit9');
	Amax = fread(handle, 1, 'bit8');
	Amean = fread(handle, 1, 'bit9');
	regionwidth = regionright - regionleft + 1;
	regionheight = regionbottom - regiontop + 1;
	bits = vec(count+1);
	bitstring = returnstring(bits);
	if bits == 0
	  tempA = zeros(regionheight, regionwidth*3);
	  tempA = tempA+Amean;
	else
	  tempA = fread(handle, [regionheight, regionwidth*3], bitstring);
	end
	if bits >= 1
		tempA = dequantize(tempA, bits, Amin, Amax, Amean); %(Amax-Amin)/(2^(bits+1)) +Amin + tempA*(Amax-Amin)/(2^bits);
	end
	OUT(1:regiontop - 1, regionleft:regionright) = tempA(:,1:regionwidth);
	OUT(regiontop:regionbottom, 1:regionleft - 1) = tempA(:,regionwidth+1:regionwidth*2);
	OUT(regiontop:regionbottom, regionleft:regionright) = tempA(:, 2*regionwidth+1:3*regionwidth);
  end
  regiontop = regionbottom+1;
  regionbottom = regionbottom*2;
  regionleft = regionright+1;
  regionright = regionright*2;
  count = count+1;
end

fclose(handle);





