Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleJPEGCompress.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 /**
27  @example ExampleJPEGCompress.cpp
28  @relates CompressJpeg
29  @brief Load an arbitrary format from disk and use libJpeg to convert it
30  to JPEG-image with given (dflt 60) quality
31  @ingroup g_examples
32  @author Arne Petersen
33 */
34 
35 //#include <Base/Common/LeakChecking.h>
36 #include <Base/Image/ImageBase.hh>
37 #include <Base/Image/ImageIO.hh>
38 #include <Base/Image/CompressJpeg.hh>
39 
40 using namespace BIAS;
41 using namespace std;
42 
43 int main(int argc, char *argv[])
44 {
45  if ((argc<3)||(argc>4)) {
46  cout <<"usage: "<<argv[0]<<" <in-image-file> <out-image-file>"
47  " [<jpeg-quality: 0..100>]"<<endl;
48  return -1;
49  }
50 
52  int result = ImageIO::Load(argv[1], srcImg);
53  if (result!=0){
54  BIASERR("Error while loading '"<<argv[1]<<"' with result="<<result);
55  return result; // for Dart testing
56  }
57  int quality = 60;
58  if (argc > 3) quality = atoi(argv[3]);
59  if ((quality < 0)||(quality > 100)) {
60  BIASERR("Given quality (" << quality << ") not valid!");
61  return -1;
62  }
63 
64  JpegHandler jHandler;
65  jHandler.Init();
66  int res = jHandler.Compress(srcImg, quality);
67  if (res < 0) {
68  BIASERR("Compression failed (returned "<< res <<")!!!");
69  return -1;
70  }
71  string filename(argv[2]);
72  if (jHandler.WriteJPEG(filename) < 0) {
73  BIASERR("Error writing jpeg-image to " << argv[2] << "!!!");
74  return -1;
75  }
76 
77  return 0;
78 }
Wrapper for fast libjpeg methods.
Definition: CompressJpeg.hh:50
int WriteJPEG(const std::string &filename)
Write previously compressed data to file named filename.
int Compress(BIAS::Image< unsigned char > &srcImg, int quality=60)
Start compression for given BIAS::Image into buffer.
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
int Init(long size=10485760)
Initialize object with standard values and limit shared memory area to given size (in byte...