SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MFXImageHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // missing_desc
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
10 // Copyright (C) 2001-2012 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 #include <FXTIFImage.h>
36 #include "MFXImageHelper.h"
37 
38 #include <cassert>
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 void
46  if (comparecase(ext, "png") == 0) {
47  if (!FXPNGImage::supported) {
48  throw InvalidArgument("Fox was compiled without png support!");
49  }
50  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
51  if (!FXJPGImage::supported) {
52  throw InvalidArgument("Fox was compiled without jpg support!");
53  }
54  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
55  if (!FXTIFImage::supported) {
56  throw InvalidArgument("Fox was compiled without tif support!");
57  }
58  }
59 }
60 
61 
62 FXImage*
63 MFXImageHelper::loadImage(FXApp* a, const std::string& file) {
64  FXString ext = FXPath::extension(file.c_str());
65  checkSupported(ext);
66  FXImage* img = NULL;
67  if (comparecase(ext, "gif") == 0) {
68  img = new FXGIFImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
69  } else if (comparecase(ext, "bmp") == 0) {
70  img = new FXBMPImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
71  } else if (comparecase(ext, "xpm") == 0) {
72  img = new FXXPMImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
73  } else if (comparecase(ext, "pcx") == 0) {
74  img = new FXPCXImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
75  } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
76  img = new FXICOImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
77  } else if (comparecase(ext, "tga") == 0) {
78  img = new FXTGAImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
79  } else if (comparecase(ext, "rgb") == 0) {
80  img = new FXRGBImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
81  } else if (comparecase(ext, "xbm") == 0) {
82  img = new FXXBMImage(a, NULL, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
83  } else if (comparecase(ext, "png") == 0) {
84  img = new FXPNGImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
85  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
86  img = new FXJPGImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
87  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
88  img = new FXTIFImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
89  } else {
90  throw InvalidArgument("Unknown file extension for image '" + file + "'!");
91  }
92 
93  FXFileStream stream;
94  if (img != NULL && stream.open(file.c_str(), FXStreamLoad)) {
95  a->beginWaitCursor();
96  img->loadPixels(stream);
97  stream.close();
98 
99  img->create();
100  a->endWaitCursor();
101  } else {
102  throw InvalidArgument("Loading failed!");
103  }
104  return img;
105 }
106 
107 
108 FXbool
110  FXint newHeight;
111  for (FXint exp = 30; exp >= 0; exp--) {
112  newHeight = 2 << exp;
113  if (image->getHeight() & newHeight) {
114  break;
115  }
116  }
117  if (2 * newHeight - image->getHeight() < image->getHeight() - newHeight) {
118  newHeight *= 2;
119  }
120  FXint newWidth;
121  for (FXint exp = 30; exp >= 0; exp--) {
122  newWidth = 2 << exp;
123  if (image->getWidth() & newWidth) {
124  break;
125  }
126  }
127  if (2 * newWidth - image->getWidth() < image->getWidth() - newWidth) {
128  newWidth *= 2;
129  }
130  if (newHeight == image->getHeight() && newWidth == image->getWidth()) {
131  return false;
132  }
133  image->scale(newWidth, newHeight);
134  return true;
135 }
136 
137 
138 // smell: yellow (the save functions may have additional options, not regarded)
139 // Save file
140 FXbool
141 MFXImageHelper::saveImage(const std::string& file,
142  int width, int height, FXColor* data) {
143  FXString ext = FXPath::extension(file.c_str());
144  checkSupported(ext);
145  FXFileStream stream;
146  if (!stream.open(file.c_str(), FXStreamSave)) {
147  throw InvalidArgument("Could not open file for writing!");
148  }
149  if (comparecase(ext, "gif") == 0) {
150  return fxsaveGIF(stream, data, width, height, false /* !!! "fast" */);
151  } else if (comparecase(ext, "bmp") == 0) {
152  return fxsaveBMP(stream, data, width, height);
153  } else if (comparecase(ext, "xpm") == 0) {
154  return fxsaveXPM(stream, data, width, height);
155  } else if (comparecase(ext, "pcx") == 0) {
156  return fxsavePCX(stream, data, width, height);
157  } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
158  return fxsaveICO(stream, data, width, height);
159  } else if (comparecase(ext, "tga") == 0) {
160  return fxsaveTGA(stream, data, width, height);
161  } else if (comparecase(ext, "rgb") == 0) {
162  return fxsaveRGB(stream, data, width, height);
163  } else if (comparecase(ext, "xbm") == 0) {
164  return fxsaveXBM(stream, data, width, height);
165  } else if (comparecase(ext, "png") == 0) {
166  return fxsavePNG(stream, data, width, height);
167  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
168  return fxsaveJPG(stream, data, width, height, 75);
169  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
170  return fxsaveTIF(stream, data, width, height, 0);
171  }
172  throw InvalidArgument("Unknown file extension for image!");
173 }
174 
175 
176 
177 /****************************************************************************/
178