www.pudn.com > jpegtool_matlab.rar > jpeg.m


% jpeg - convert image via transform->quantize->invert
%
% SYNOPSIS
%	jpeg(X)
%	jpeg(X, Q)
%	[Y, r] = jpeg(X)
%	[Y, r] = jpeg(X, Q)
%
% DESCRIPTION
%	jpeg takes X (which may be a matrix or a filename) and uses the
%	cosine transform and the specified quantizing matrix Q to generate
%	the new image Y. The process is lossy at the quantizing stage, and
%	Y will usually differ from X.
%
%	If the quantizer is not given, then the global quantizer QMAT
%	will be used. Initially, QMAT is the 8x8 JPEG luminance matrix.
%	The quantizer becomes the new value for QMAT.
% 
%	If the ratio r is requested, then a calculation of the "lossy 
%	compression" is performed. This is returned as a percentage, and
%	measures the amount of savings obtained at the quantizing stage.
%
% BUGS
%       The ratio measures only the savings obtained by removing 
%       "trailing zeros" in the submatrices. This often gives useful 
%	information about the choice of quantizer, but it is not the 
%	whole story of compression.

% Copyright (C) 1995-1997 Darrel Hankerson and Greg A. Harris
%
% This file is part of the JPEGtool collection of scripts for Octave
% and Matlab; see http://www.dms.auburn.edu/compression
%
% The JPEGtool collection is free software; you can redistribute it
% and/or modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2, or
% (at your option) any later version.
%
% The collection is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this package; see the file COPYING.  If not, write to the
% Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
% 02111-1307, USA.

function [Y, r] = jpeg(X, Q)

global QMAT

if (isstr(X))
  X = getpgm(X);
end

if (nargin == 1)
  Q = [];
  if exist('QMAT')
    if size(QMAT,1)
      Q = QMAT;
    end
  end
  if (~length(Q))
    Q = stdQ;
  end
end

% Make Q the new default quantizer
QMAT = Q;

n = size(Q, 1);

if (nargout == 2)
  [Y, r] = quant(dct(X, n), Q);
else
  Y = quant(dct(X, n), Q);
end
Y = invdct(dequant(Y, Q), n);