Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biassetuid.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 #include <Base/Image/ImageBase.hh>
27 #include <Base/Image/ImageIO.hh>
28 #include <stdio.h>
29 
30 
31 using namespace BIAS;
32 using namespace std;
33 
34 
35 /**
36  @file
37  @ingroup g_tools
38  @brief load image given by the command line, assign
39  a uuid and write back to disk, see biassetuid.cpp
40  @author koeser
41 */
42 void usage()
43 {
44  cout << "Usage: biassetuid [options] files"<<endl;
45  cout <<" Options: "<< endl
46  <<" -f : force setting of a new uid"<<endl
47  << endl;
48  cout <<"This program opens the image files given by the command line"
49  <<", checks for a valid id and assigns a new one if there is no."<<endl
50  <<"Returns: 0 on success, 1 if errors occured."<<endl<<endl;
51 }
52 
53 int DetermineFiletype(const string &fn, ImageIO::TFileFormat &thetype)
54 {
55  string::size_type EndOfPrefix;
56  string suffix;
57 
58  EndOfPrefix = fn.rfind('.');
59  if (EndOfPrefix == string::npos) return -1;
60  suffix = fn.substr(EndOfPrefix+1,fn.length());
61  if (suffix == "pnm") thetype = ImageIO::FF_ppm;
62  else if (suffix == "ppm") thetype = ImageIO::FF_ppm;
63  else if (suffix == "pgm") thetype = ImageIO::FF_pgm;
64  else if (suffix == "mip") thetype = ImageIO::FF_mip;
65  else return -1;
66 
67  return 0;
68 }
69 
70 int main (int argc, char *argv[])
71 {
72  bool ForceNewUUID = false;
73  int i = 1;
74  if (argc>1) if (!strcmp("-f",argv[1])) {
75  std::cout << "Existing UIDs are replaced."<<endl;
76  ForceNewUUID = true;
77  i = 2;
78  }
79  if (i>=argc) {
80  usage();
81  return 0; // for Dart testing
82  }
83 
84  ImageIO::TFileFormat FileType;
85  bool ErrorFlag = false;
86  int IOResult = 0;
87  BIAS::ImageBase CurImage;
88 
89  for (;i<argc;i++) {
90  if (DetermineFiletype(argv[i], FileType)!=0) {
91  BIASERR("Skipping current file because of unknown/unsuported format: "
92  <<argv[i]<<endl<<" UIDs can only be written to mip,pgm,ppm,pnm");
93  ErrorFlag = true;
94  continue;
95  }
96 
97  IOResult = ImageIO::Load(argv[i], CurImage);
98  if (IOResult!=0) {
99  ErrorFlag = true;
100  BIASERR("Error reading "<< argv[i]);
101  continue;
102  }
103  // flag indicating the previous/new id status of the file
104  int idstatus = 0;
105  if (CurImage.GetUID().IsValid()) {
106  idstatus = 1;
107  if (ForceNewUUID) idstatus = 2;
108  }
109  /*
110  if (FileType==ImageIO::FF_mip) {
111  IOResult = ImageIO::Save(argv[i], CurImage, false, ForceNewUUID );
112  } else {
113  IOResult = ImageIO::Save(argv[i], CurImage, FileType, false,
114  0, ForceNewUUID);
115  } */
116  IOResult = ImageIO::Save(argv[i],
117  CurImage,
118  FileType,
119  false,
120  0,
121  ForceNewUUID);
122 
123  if (IOResult!=0) {
124  ErrorFlag = true;
125  BIASERR("Error writing "<< argv[i]);
126  continue;
127  }
128  cout << "File " << argv[i] << " has UID "<<CurImage.GetUID();
129  switch (idstatus) {
130  case 1: cout<<" (kept existing)"; break;
131  case 2: cout<<" (replaced old)"; break;
132  default:cout<<" (new)";
133  }
134  cout << endl;
135  }
136  if (ErrorFlag) cout << "There were errors." <<endl;
137  if (ErrorFlag) return 1;
138  return 0;
139 
140 }
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
const BIAS::UUID & GetUID() const
returns the UUID of the image
Definition: ImageBase.hh:449
TFileFormat
format specifier when writing an image to a file Do NOT change order or associated enum value because...
Definition: ImageIO.hh:76
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
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
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
bool IsValid() const
checks whether this uuid is valid(true) or unitialized(false)
Definition: UUID.hh:210