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