function up = untiff(strip,width,total_bytes)
%UNTIFF	Decompress a strip which was compressed with TIFF.
%	A = UNTIFF(S,W,N) decompresses the strip in the matrix S 
%	and returns the result in A.  W is the uncompressed width 
%	of each row in bytes and N is the number of compressed 
%	bytes in the strip.
%
%	This function is used by TIFFREAD.
%
%	See also: TIFF, TIFFREAD.

%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.3 $  $Date: 1993/09/09 21:35:38 $

row_count = 1;		% row number 
c_count = 1;		% count number of compressed bytes read
while c_count < total_bytes
  unc_count = 0;	% count number of uncompressed bytes per row
%
  while unc_count < width
    n = strip(c_count);
    if ((n >= 0) & (n <= 127))
      for j = 1:n+1
        c_count = c_count + 1;
        up(row_count, unc_count+j) = strip(c_count); 
      end
      c_count = c_count + 1;
      unc_count = unc_count + n + 1;
    elseif ((n >= -127) & (n <= -1))
      for j = 1:1-n
        up(row_count, unc_count+j) = strip(c_count+1);
      end
      c_count = c_count + 2;
      unc_count = unc_count - n + 1;
    else
      c_count = c_count + 1;
    end
  end
  row_count = row_count + 1;
end
