Eclipse SUMO - Simulation of Urban MObility
MFXImageHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // missing_desc
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <string>
23 #include <fx.h>
24 #include <FXPNGImage.h>
25 #include <FXJPGImage.h>
26 #ifdef _MSC_VER
27 #pragma warning(push)
28 #pragma warning(disable: 4244) // do not warn about integer conversions
29 #endif
30 #include <FXTIFImage.h>
31 #ifdef _MSC_VER
32 #pragma warning(pop)
33 #endif
34 #include <utils/common/ToString.h>
35 #include "MFXImageHelper.h"
36 
37 #include <cassert>
38 
39 void
41  if (comparecase(ext, "png") == 0) {
42  if (!FXPNGImage::supported) {
43  throw InvalidArgument("Fox was compiled without png support!");
44  }
45  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
46  if (!FXJPGImage::supported) {
47  throw InvalidArgument("Fox was compiled without jpg support!");
48  }
49  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
50  if (!FXTIFImage::supported) {
51  throw InvalidArgument("Fox was compiled without tif support!");
52  }
53  }
54 }
55 
56 
57 FXImage*
58 MFXImageHelper::loadImage(FXApp* a, const std::string& file) {
59  FXString ext = FXPath::extension(file.c_str());
60  checkSupported(ext);
61  FXImage* img = nullptr;
62  if (comparecase(ext, "gif") == 0) {
63  img = new FXGIFImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
64  } else if (comparecase(ext, "bmp") == 0) {
65  img = new FXBMPImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
66  } else if (comparecase(ext, "xpm") == 0) {
67  img = new FXXPMImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
68  } else if (comparecase(ext, "pcx") == 0) {
69  img = new FXPCXImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
70  } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
71  img = new FXICOImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
72  } else if (comparecase(ext, "tga") == 0) {
73  img = new FXTGAImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
74  } else if (comparecase(ext, "rgb") == 0) {
75  img = new FXRGBImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
76  } else if (comparecase(ext, "xbm") == 0) {
77  img = new FXXBMImage(a, nullptr, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
78  } else if (comparecase(ext, "png") == 0) {
79  img = new FXPNGImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
80  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
81  img = new FXJPGImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
82  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
83  img = new FXTIFImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
84  } else {
85  throw InvalidArgument("Unknown file extension '" + toString(ext.text()) + "' for image '" + file + "'!");
86  }
87 
88  FXFileStream stream;
89  if (img != nullptr && stream.open(file.c_str(), FXStreamLoad)) {
90  a->beginWaitCursor();
91  img->loadPixels(stream);
92  stream.close();
93 
94  img->create();
95  a->endWaitCursor();
96  } else {
97  delete img;
98  throw InvalidArgument("Loading failed!");
99  }
100  return img;
101 }
102 
103 
104 FXbool
105 MFXImageHelper::scalePower2(FXImage* image, const int maxSize) {
106  FXint newHeight = 0;
107  for (FXint exp = 30; exp >= 0; exp--) {
108  newHeight = 2 << exp;
109  if (newHeight <= maxSize && (image->getHeight() & newHeight)) {
110  break;
111  }
112  }
113  if (2 * newHeight <= maxSize && (2 * newHeight - image->getHeight() < image->getHeight() - newHeight)) {
114  newHeight *= 2;
115  }
116  FXint newWidth = 0;
117  for (FXint exp = 30; exp >= 0; exp--) {
118  newWidth = 2 << exp;
119  if (newWidth <= maxSize && (image->getWidth() & newWidth)) {
120  break;
121  }
122  }
123  if (2 * newWidth <= maxSize && (2 * newWidth - image->getWidth() < image->getWidth() - newWidth)) {
124  newWidth *= 2;
125  }
126  if (newHeight == image->getHeight() && newWidth == image->getWidth()) {
127  return false;
128  }
129  image->scale(newWidth, newHeight);
130  return true;
131 }
132 
133 
134 // smell: yellow (the save functions may have additional options, not regarded)
135 // Save file
136 FXbool
137 MFXImageHelper::saveImage(const std::string& file,
138  int width, int height, FXColor* data) {
139  FXString ext = FXPath::extension(file.c_str());
140  checkSupported(ext);
141  FXFileStream stream;
142  if (!stream.open(file.c_str(), FXStreamSave)) {
143  throw InvalidArgument("Could not open file for writing!");
144  }
145  if (comparecase(ext, "gif") == 0) {
146  return fxsaveGIF(stream, data, width, height, false /* !!! "fast" */);
147  } else if (comparecase(ext, "bmp") == 0) {
148  return fxsaveBMP(stream, data, width, height);
149  } else if (comparecase(ext, "xpm") == 0) {
150  return fxsaveXPM(stream, data, width, height);
151  } else if (comparecase(ext, "pcx") == 0) {
152  return fxsavePCX(stream, data, width, height);
153  } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
154  return fxsaveICO(stream, data, width, height);
155  } else if (comparecase(ext, "tga") == 0) {
156  return fxsaveTGA(stream, data, width, height);
157  } else if (comparecase(ext, "rgb") == 0) {
158  return fxsaveRGB(stream, data, width, height);
159  } else if (comparecase(ext, "xbm") == 0) {
160  return fxsaveXBM(stream, data, width, height);
161  } else if (comparecase(ext, "png") == 0) {
162  return fxsavePNG(stream, data, width, height);
163  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
164  return fxsaveJPG(stream, data, width, height, 75);
165  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
166  return fxsaveTIF(stream, data, width, height, 0);
167  }
168  throw InvalidArgument("Unknown file extension for image!");
169 }
170 
171 
172 /****************************************************************************/
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
static void checkSupported(FXString ext)
static FXImage * loadImage(FXApp *a, const std::string &file)
static FXbool scalePower2(FXImage *image, int maxSize=(2<< 29))
static FXbool saveImage(const std::string &file, int width, int height, FXColor *data)