Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasadd.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 #include <Base/Debug/Error.hh>
27 #include <Base/Image/ImageIO.hh>
28 #include <Base/Image/ImageConvert.hh>
29 #include <iostream>
30 
31 using namespace BIAS;
32 using namespace std;
33 
34 /**
35  * @file
36  * @brief Tool for adding two images pixelwise, see biasadd.cpp
37  * @ingroup g_tools
38  * @author MIP
39 **/
40 int main(int argc, char ** argv)
41 {
42  //system("pwd");
43  if (argc<4){
44  if (argc>0) cerr << argv[0] <<" <image 1> <image 2> <outfile> \n";
45  cerr << " adds image 1 to image 2 and writes the result to outfile"
46  <<" scaling and shifting is done for unsigned char"<<
47  endl;
48  return 0; // for Dart testing
49  }
50 
51  ImageBase im1, im2, result;
52  if (ImageIO::Load(argv[1], im1)!=0){
53  BIASERR("error loading "<<argv[1]);
54  return -1;
55  }
56  if (ImageIO::Load(argv[2], im2)!=0){
57  BIASERR("error loading "<<argv[2]);
58  return -1;
59  }
60  if (!im1.SamePixelAndChannelCount(im2)) {
61  BIASERR("image size,depth,or ST does not match");
62  exit(-2);
63  }
64  BIASASSERT(im1.GetWidth()==im2.GetWidth());
65  BIASASSERT(im1.GetHeight()==im2.GetHeight());
66 
67  Image<float> sumImg;
68  Image<unsigned char> ucim1, ucim2,ucSum;
69  sumImg.Init(im1.GetWidth(),im1.GetHeight(),im1.GetChannelCount());
70  switch (im1.GetStorageType()){
72  {
73  ucim1=im1;
74  ucim2=im2;
75  unsigned char *p1, *p2, *end;
76  float *pSum;
77  p1=ucim1.GetImageData();
78  p2=ucim2.GetImageData();
79  pSum=sumImg.GetImageData();
80  end=p1+im1.GetSizeByte();
81  //add
82  float max=0;
83  while (p1!=end) {
84  *pSum= (float)(*p1) + (float)(*p2);
85  if (*pSum>max)
86  max=*pSum;
87  p1++; p2++; pSum++;
88  }
89 
90  // scale
91  if (max>255.0) {
92  for (unsigned int i=0;
93  i<sumImg.GetPixelCount()*sumImg.GetChannelCount();
94  i++)
95  sumImg.GetImageData()[i] *= (float)(255.0/max);
96  }
97 
99  if (ImageIO::Save(string(argv[3]), ucSum)!=0){
100  BIASERR("error writing "<<argv[3]);
101  }
102  // ucdiff.GetMinMaxPixelValue(ucmin, ucmax);
103 // // cerr << "differences between "<<(int)ucmin<<" and "<<(int)ucmax<<endl;
104 // if (ucdiff.ScaleShiftBetween(0, 255)!=0){
105 // BIASERR("error scaling image");
106 // }
107 // if (ImageIO::Save("diff-scaled.mip", ucdiff)!=0){
108 // cerr << "error writing diff-scaled.mip "<<endl;
109 // }
110 
111  }
112  break;
113  case ImageBase::ST_float:
114 
115  default:
116  cerr << "storage type not supported \n";
117  break;
118  }
119  return 0;
120 }
unsigned int GetSizeByte() const
returns the nr.
Definition: ImageBase.hh:352
float image storage type
Definition: ImageBase.hh:118
unsigned int GetWidth() const
Definition: ImageBase.hh:312
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
bool SamePixelAndChannelCount(const ImageBase &Image) const
checks if data area has same &quot;size&quot; as Image of other type
Definition: ImageBase.hh:73
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
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
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
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
This is the base class for images in BIAS.
Definition: ImageBase.hh:102