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