Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasinsert.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 //#include <Base/Common/LeakChecking.h>
26 
27 #include "Base/Image/ImageIO.hh"
28 #include "Base/Image/ImageConvert.hh"
29 #include "Filter/Rescale.hh"
30 #include "Utils/Param.hh"
31 
32 using namespace BIAS;
33 using namespace std;
34 
35 
36 /**
37  @file
38  @ingroup g_tools
39  @brief program for inserting an image into another, see biasinsert.cpp
40  @author grest 04/05 */
41 int main(int argc, char *argv[])
42 {
43  Param params;
44  params.DisableDestructorWarning();
45  params.AddParamInt("tlx","top left insert x-position",-1,-1,10000,'x');
46  params.AddParamInt("tly","top left insert y-position",-1,-1,10000,'y');
47 
48  params.AddParamInt("xsize","pixel size of insertion in x-direction"
49  " defaults to same",-1,-1,10000,'X');
50 
51  if (argc<3) {
52  cout<<"usage:"<<argv[0]<<" <img> <imgToInsert> [resultimage]"<<endl;
53  cout<<endl<<
54  " inserts the <imgToinsert> into <img> at the specified"<<
55  " position, the position can be anyone! "<<endl<<
56  " default is right next to <img>"<<endl<<
57  " [resultImg] optional mip or ppm image"
58  " , default: result is written to result.mip"<<
59  endl<<endl;
60  params.Usage();
61  return 0;
62  }
63  int nextParam = params.ParseCommandLine(argc,argv);
64 
65  BIAS::ImageBase img, imgToIns,imgRescaled;
66  if (ImageIO::Load(argv[nextParam],img)!=0) {
67  cerr<<"error loading:"<<argv[nextParam]<<endl;
68  }
69  nextParam++;
70  if (ImageIO::Load(argv[nextParam],imgToIns)!=0) {
71  cerr<<"error loading:"<<argv[nextParam]<<endl;
72  }
73 
76  cerr<<"only implemented for unsigned char images yet"<<endl;
77  exit(-1);
78  }
79  if ( (img.GetColorModel()!=imgToIns.GetColorModel() ) ) {
80  cerr<<"WARNING: colormodels do not match"<<endl<<
81  " converting <imgToinsert> to:"<<int(img.GetColorModel()) <<endl;
82  BIAS::ImageBase dummy;
83  if (ImageConvert::Convert(imgToIns,dummy,img.GetColorModel()) !=0 ) {
84  cerr<<"couldn't convert, exiting"<<endl;
85  exit(-1);
86  }
87  imgToIns=dummy;
88  }
89 
90  nextParam++;
91  std::string resultFile;
92  if (nextParam<argc) {
93  resultFile=argv[nextParam];
94  } else resultFile="result.mip";
95 
96  double insertionSize= *params.GetParamInt("xsize");
97  Image<unsigned char> scaledImg(imgToIns), resultImg(imgToIns);
98  if (insertionSize!=-1) {
100  scaler.SetFactor((double)imgToIns.GetWidth()/ insertionSize);
101  scaler.Filter(resultImg,scaledImg);
102  }
103  int insertionX= *params.GetParamInt("tlx");
104  if (insertionX==-1) insertionX=img.GetWidth();
105  int insertionY= *params.GetParamInt("tly");
106  if (insertionY==-1) insertionY=0;
107 
108  int resultXSize= insertionX + scaledImg.GetWidth();
109  if ( resultXSize<(int)img.GetWidth() )
110  resultXSize=img.GetWidth();
111  int resultYSize= insertionY + scaledImg.GetHeight();
112  if ( resultYSize<(int)img.GetHeight() )
113  resultYSize=img.GetHeight();
114 
115  resultImg.Release();
116  resultImg.Init(resultXSize,resultYSize,img.GetChannelCount());
117  resultImg.SetColorModel(img.GetColorModel());
118  resultImg.SetROI(0,0, img.GetWidth(), img.GetHeight());
119  resultImg.Paste2ROI(img);
120  resultImg.SetROI(insertionX,insertionY,
121  insertionX+scaledImg.GetWidth(),
122  insertionY+scaledImg.GetHeight());
123  resultImg.Paste2ROI(scaledImg);
124 
125  if (resultFile.find(".ppm") != string::npos) {
126  ImageIO::Save(resultFile,resultImg,ImageIO::FF_ppm);
127  cout<<"resulting ppm-image written to "<<resultFile<<endl;
128  } else {
129  //ImageIO::Save(resultFile,resultImg);
130  ImageIO::Save(resultFile,resultImg);
131  cout<<"resulting mip-image written to "<<resultFile<<endl;
132  }
133  return 0;
134 }
void DisableDestructorWarning()
Uses this just before end of your program to avoid error from destructor.
Definition: Param.hh:513
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int ParseCommandLine(int &argc, char *argv[])
scan command line arguments for valid parameters
Definition: Param.cpp:1028
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Definition: Param.cpp:176
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
scales the src to dst using the downsampling factor from SetFactor()
Definition: Rescale.cpp:89
int * GetParamInt(const std::string &name) const
Definition: Param.cpp:618
unsigned int GetHeight() const
Definition: ImageBase.hh:319
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
This class Param provides generic support for parameters.
Definition: Param.hh:231
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
static int Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false)
main general conversion function, calls desired specialized functions, always initializes the destIma...
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
int * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
Definition: Param.cpp:276
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
void SetFactor(double factor)
the downsampling factor
Definition: Rescale.hh:361