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