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