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.dev/sumo
3 // Copyright (C) 2005-2024 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 "fxheader.h"
23 #include <FXPNGImage.h>
24 #include <FXJPGImage.h>
25 #ifdef _MSC_VER
26 #pragma warning(push)
27 #pragma warning(disable: 4244 4242) // do not warn about integer conversions
28 #endif
29 #include <FXTIFImage.h>
30 #ifdef _MSC_VER
31 #pragma warning(pop)
32 #endif
33 #include <utils/common/ToString.h>
34 #include "MFXImageHelper.h"
35 
36 
37 void
39  if (comparecase(ext, "png") == 0) {
40  if (!FXPNGImage::supported) {
41  throw InvalidArgument("Fox was compiled without png support!");
42  }
43  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
44  if (!FXJPGImage::supported) {
45  throw InvalidArgument("Fox was compiled without jpg support!");
46  }
47  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
48  if (!FXTIFImage::supported) {
49  throw InvalidArgument("Fox was compiled without tif support!");
50  }
51  }
52 }
53 
54 
55 FXImage*
56 MFXImageHelper::loadImage(FXApp* a, const std::string& file) {
57  FXString ext = FXPath::extension(file.c_str());
58  checkSupported(ext);
59  FXImage* img = nullptr;
60  if (comparecase(ext, "gif") == 0) {
61  img = new FXGIFImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
62  } else if (comparecase(ext, "bmp") == 0) {
63  img = new FXBMPImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
64  } else if (comparecase(ext, "xpm") == 0) {
65  img = new FXXPMImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
66  } else if (comparecase(ext, "pcx") == 0) {
67  img = new FXPCXImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
68  } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
69  img = new FXICOImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
70  } else if (comparecase(ext, "tga") == 0) {
71  img = new FXTGAImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
72  } else if (comparecase(ext, "rgb") == 0) {
73  img = new FXRGBImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
74  } else if (comparecase(ext, "xbm") == 0) {
75  img = new FXXBMImage(a, nullptr, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
76  } else if (comparecase(ext, "png") == 0) {
77  img = new FXPNGImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
78  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
79  img = new FXJPGImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
80  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
81  img = new FXTIFImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
82  } else {
83  throw InvalidArgument("Unknown file extension '" + toString(ext.text()) + "' for image '" + file + "'!");
84  }
85 
86  FXFileStream stream;
87  if (img != nullptr && stream.open(file.c_str(), FXStreamLoad)) {
88  a->beginWaitCursor();
89  img->loadPixels(stream);
90  stream.close();
91 
92  img->create();
93  a->endWaitCursor();
94  } else {
95  delete img;
96  throw InvalidArgument("Loading failed!");
97  }
98  return img;
99 }
100 
101 
102 FXbool
103 MFXImageHelper::scalePower2(FXImage* image, const int maxSize) {
104  FXint newHeight = 0;
105  for (FXint exp = 30; exp >= 0; exp--) {
106  newHeight = 2 << exp;
107  if (newHeight <= maxSize && (image->getHeight() & newHeight)) {
108  break;
109  }
110  }
111  if (2 * newHeight <= maxSize && (2 * newHeight - image->getHeight() < image->getHeight() - newHeight)) {
112  newHeight *= 2;
113  }
114  FXint newWidth = 0;
115  for (FXint exp = 30; exp >= 0; exp--) {
116  newWidth = 2 << exp;
117  if (newWidth <= maxSize && (image->getWidth() & newWidth)) {
118  break;
119  }
120  }
121  if (2 * newWidth <= maxSize && (2 * newWidth - image->getWidth() < image->getWidth() - newWidth)) {
122  newWidth *= 2;
123  }
124  if (newHeight == image->getHeight() && newWidth == image->getWidth()) {
125  return false;
126  }
127  image->scale(newWidth, newHeight);
128  return true;
129 }
130 
131 
132 // smell: yellow (the save functions may have additional options, not regarded)
133 // Save file
134 FXbool
135 MFXImageHelper::saveImage(const std::string& file,
136  int width, int height, FXColor* data) {
137  FXString ext = FXPath::extension(file.c_str());
138  checkSupported(ext);
139  FXFileStream stream;
140  if (!stream.open(file.c_str(), FXStreamSave)) {
141  throw InvalidArgument("Could not open file for writing!");
142  }
143  if (comparecase(ext, "gif") == 0) {
144  return fxsaveGIF(stream, data, width, height, false /* !!! "fast" */);
145  } else if (comparecase(ext, "bmp") == 0) {
146  return fxsaveBMP(stream, data, width, height);
147  } else if (comparecase(ext, "xpm") == 0) {
148  return fxsaveXPM(stream, data, width, height);
149  } else if (comparecase(ext, "pcx") == 0) {
150  return fxsavePCX(stream, data, width, height);
151  } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
152  return fxsaveICO(stream, data, width, height);
153  } else if (comparecase(ext, "tga") == 0) {
154  return fxsaveTGA(stream, data, width, height);
155  } else if (comparecase(ext, "rgb") == 0) {
156  return fxsaveRGB(stream, data, width, height);
157  } else if (comparecase(ext, "xbm") == 0) {
158  return fxsaveXBM(stream, data, width, height);
159  } else if (comparecase(ext, "png") == 0) {
160  return fxsavePNG(stream, data, width, height);
161  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
162  return fxsaveJPG(stream, data, width, height, 75);
163  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
164  return fxsaveTIF(stream, data, width, height, 0);
165  }
166  throw InvalidArgument("Unknown file extension for image!");
167 }
168 
169 
170 /****************************************************************************/
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
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)