TDME2  1.9.200
imageprocessor-main.cpp
Go to the documentation of this file.
1 #include <string>
2 
3 #include <tdme/tdme.h>
6 #include <tdme/engine/Texture.h>
8 #include <tdme/engine/Version.h>
11 #include <tdme/utilities/Console.h>
13 
14 using std::string;
15 
25 
26 int main(int argc, char** argv)
27 {
28  Console::println(string("imageprocessor ") + Version::getVersion());
29  Console::println(Version::getCopyright());
30  Console::println();
31 
32  //
33  if (argc != 3) {
34  Console::println("Usage: imageprocessor input.png output.png");
35  Application::exit(Application::EXITCODE_FAILURE);
36  }
37 
38  //
39  auto inputImageFileName = string(argv[1]);
40  auto outputImageFileName = string(argv[2]);
41 
42  //
43  try {
44  Console::println("Loading image: " + inputImageFileName);
45  auto image = TextureReader::read(
46  FileSystem::getInstance()->getPathName(inputImageFileName),
47  FileSystem::getInstance()->getFileName(inputImageFileName)
48  );
49 
50  //
51  Console::println("Processing image");
52 
53  // for now: do black pixel -> transparent pixels, every other pixel gets white
54  // later we can provide color transform matrices with preset matrices
55  auto bytesPerPixel = image->getRGBDepthBitsPerPixel() / 8;
56  auto imageTextureData = image->getRGBTextureData();
57  for (auto y = 0; y < image->getTextureHeight(); y++) {
58  for (auto x = 0; x < image->getTextureWidth(); x++) {
59  auto offset = y * bytesPerPixel * image->getTextureWidth() + x * bytesPerPixel;
60  auto red = imageTextureData.get(offset + 0);
61  auto green = imageTextureData.get(offset + 1);
62  auto blue = imageTextureData.get(offset + 2);
63  auto alpha = bytesPerPixel == 4?imageTextureData.get(offset + 3):0xff;
64  // transform black pixels to transparent pixels
65  if (red < 5 && green < 5 && blue < 5) {
66  alpha = 0;
67  } else {
68  // everything else should be white
69  red = 0xff;
70  green = 0xff;
71  blue = 0xff;
72  }
73  imageTextureData.getBuffer()[offset + 0] = red;
74  imageTextureData.getBuffer()[offset + 1] = green;
75  imageTextureData.getBuffer()[offset + 2] = blue;
76  if (bytesPerPixel == 4) {
77  imageTextureData.getBuffer()[offset + 3] = alpha;
78  }
79 
80  }
81  }
82  // write back to image
83  image->setTextureData(Texture::getRGBFormatByPixelBitsPerPixel(image->getRGBDepthBitsPerPixel()), imageTextureData);
84 
85  // smooth
86  auto smoothedTexture = TextureReader::smooth(image);
87  image->releaseReference();
88  image = smoothedTexture;
89 
90  //
91  Console::println("Saving image: " + outputImageFileName);
92  PNGTextureWriter::write(
93  image,
94  FileSystem::getInstance()->getPathName(outputImageFileName),
95  FileSystem::getInstance()->getFileName(outputImageFileName),
96  false,
97  false
98  );
99 
100  //
101  image->releaseReference();
102  } catch (Exception& exception) {
103  Console::println("An error occurred: " + string(exception.what()));
104  }
105 
106  //
107  Application::exit(Application::EXITCODE_SUCCESS);
108 }
Application base class, please make sure to allocate application on heap to have correct application ...
Definition: Application.h:41
Texture entity.
Definition: Texture.h:24
File system singleton class.
Definition: FileSystem.h:17
Console class.
Definition: Console.h:29
int main(int argc, char **argv)
std::exception Exception
Exception base class.
Definition: Exception.h:18