Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleJPEGCompress.cpp

Load an arbitrary format from disk and use libJpeg to convert it to JPEG-image with given (dflt 60) quality

Author
Arne Petersen
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleJPEGCompress.cpp
@relates CompressJpeg
@brief Load an arbitrary format from disk and use libJpeg to convert it
to JPEG-image with given (dflt 60) quality
@ingroup g_examples
@author Arne Petersen
*/
//#include <Base/Common/LeakChecking.h>
#include <Base/Image/ImageBase.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/Image/CompressJpeg.hh>
using namespace BIAS;
using namespace std;
int main(int argc, char *argv[])
{
if ((argc<3)||(argc>4)) {
cout <<"usage: "<<argv[0]<<" <in-image-file> <out-image-file>"
" [<jpeg-quality: 0..100>]"<<endl;
return -1;
}
int result = ImageIO::Load(argv[1], srcImg);
if (result!=0){
BIASERR("Error while loading '"<<argv[1]<<"' with result="<<result);
return result; // for Dart testing
}
int quality = 60;
if (argc > 3) quality = atoi(argv[3]);
if ((quality < 0)||(quality > 100)) {
BIASERR("Given quality (" << quality << ") not valid!");
return -1;
}
JpegHandler jHandler;
jHandler.Init();
int res = jHandler.Compress(srcImg, quality);
if (res < 0) {
BIASERR("Compression failed (returned "<< res <<")!!!");
return -1;
}
string filename(argv[2]);
if (jHandler.WriteJPEG(filename) < 0) {
BIASERR("Error writing jpeg-image to " << argv[2] << "!!!");
return -1;
}
return 0;
}