Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleColourGradient.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  * @brief Example for ColourGradient
27  *
28  * Loads an image, converts it to grayscale (if necessary) and recolours it
29  * using ColourGradient.
30  *
31  * @author Robert Wulff
32  * @date 10/2010
33  */
34 
35 // std
36 #include <iostream>
37 #include <vector>
38 
39 // BIAS
40 #include <Utils/ColourGradient.hh>
41 #include <Base/Image/Image.hh>
42 #include <Base/Image/ImageIO.hh>
43 #include <Base/Image/ImageConvert.hh>
44 
45 using namespace std;
46 using namespace BIAS;
47 
48 // flag determines if black pixels are to be leaved untouched
49 const bool SKIP_BLACK_PIXELS = true;
50 
51 /**
52  * @brief main
53  */
54 int main(int argc, char* argv[])
55 {
56  // ensure filenames are given as parameters
57  if (argc != 3) {
58  cout << endl
59  << "Loads an image, recolours it and saves it." << endl << endl
60  << "USAGE: "
61  << argv[0] << " <input_image> <output_image>" << endl << endl;
62  exit(-1);
63  }
64 
65  // load image
66  Image<unsigned char> imageIn;
67  if (ImageIO::Load(argv[1], imageIn) != 0) {
68  cout << "loading image failed" << endl;
69  exit(-1);
70  }
71 
72  // ensure gray scale
73  if (imageIn.GetChannelCount() != 1) {
74  cout << "converting image to gray scale" << endl;
75  ImageConvert::IP_ToGrey(imageIn);
76  }
77 
78  // either create custom gradient...
79 // vector<unsigned char> red(3), green(3), blue(3);
80 // red[0] = 255; red[1] = 0; red[2] = 0;
81 // green[0] = 0; green[1] = 255; green[2] = 0;
82 // blue[0] = 0; blue[1] = 0; blue[2] = 255;
83 
84 // ColourGradient<unsigned char> gradient(red, blue);
85 // gradient.AddColour(0.5, green);
86 
87  // ... or a preset
89 
90  // recolour the image with the gradient
91  Image<unsigned char> imageOut(imageIn.GetWidth(),
92  imageIn.GetHeight(),
93  3);
94  unsigned char* idaIn = imageIn.GetImageData();
95  unsigned char* idaOut = imageOut.GetImageData();
96  vector<unsigned char> col(3);
97 
98  for (unsigned int y = 0; y < imageIn.GetHeight(); ++y) {
99  for (unsigned int x = 0; x < imageIn.GetWidth(); ++x) {
100  const unsigned int offsetIn = y * imageIn.GetWidth() + x;
101  const unsigned int offsetOut = y * imageIn.GetWidth() * imageOut.GetChannelCount()
102  + x * imageOut.GetChannelCount();
103 
104  const float curColFloat = float(idaIn[offsetIn]) / 255.0f;
105  if (!SKIP_BLACK_PIXELS || curColFloat != 0.0f) {
106  gradient.GetColour(curColFloat, col);
107  idaOut[offsetOut] = col[0];
108  idaOut[offsetOut + 1] = col[1];
109  idaOut[offsetOut + 2] = col[2];
110  }
111  }
112  }
113 
114  // save the image
115  ImageIO::Save(argv[2],
116  imageOut);
117 
118  return 0;
119 }
unsigned int GetWidth() const
Definition: ImageBase.hh:312
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137