Adonthell  0.4
image.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 1999/2000/2001/2004 Alexandre Courbot
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 /**
21  * @file image.h
22  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
23  *
24  * @brief Declares the image class.
25  *
26  *
27  */
28 
29 
30 #ifndef IMAGE_H_
31 #define IMAGE_H_
32 
33 #include "fileops.h"
34 #include "screen.h"
35 
36 
37 /**
38  * Image manipulation class.
39  * Designed to work with single images, without having to care about the bit
40  * depth. This class is widely used through the %game - in fact it handles
41  * everything that is displayed on the %screen.
42  * This class highly relies on surface, so you'll probably want to have a look
43  * at it before using image.
44  */
45 class image : public surface
46 {
47 public:
48  /**
49  * Default constructor.
50  * The image created via this constructor is totally empty.
51  */
52  image ();
53 
54 #ifndef SWIG
55  /** Creates an image with a specified size.
56  * @param l length of the image.
57  * @param h height of the image.
58  * @param scale the initial scale of the image (default 1)
59  *
60  * @attention Not accessible from Python.
61  */
62  image (u_int16 l, u_int16 h, const u_int8 & scale = 1);
63 
64  /**
65  * Create image from SDL_Surface.
66  * @param s surface
67  */
68  image (SDL_Surface *s, const SDL_Color & color);
69 #endif
70 
71  /** Destructor.
72  */
73  virtual ~image ();
74 
75  /**
76  * Resize this image.
77  * All the content will be lost.
78  * If you want to zoom the image you'll want to see the zoom () function
79  * instead.
80  *
81  * @param l new length.
82  * @param h new height.
83  * @sa zoom ()
84  */
85  void resize (u_int16 l, u_int16 h);
86 
87  /**
88  * Resets the image to it's initial state, that is totally
89  * empty.
90  *
91  */
92  void clear ();
93 
94 
95  /**
96  * @name Loading / Saving Methods.
97  * These methods allows you to load and save
98  * an image in different formats.
99  *
100  */
101  //@{
102 
103 
104  /** Loads an image from an opened file, saved in %game internal format,
105  * with alpha and mask values.
106  * @param file the opened file from which to read.
107  * @return
108  * @li 0 in case of success.
109  * @li -1 in case of error.
110  * @sa load ()
111  */
112  s_int8 get (igzstream& file);
113 
114  /** Loads an image from a file name, in game internal format, with alpha
115  * and mask values.
116  * @param fname the name of the file to load.
117  * @return
118  * @li 0 in case of success.
119  * @li -1 in case of error.
120  * @sa get ()
121  */
122  s_int8 load (string fname);
123 
124  /** Loads an image from an opened file, saved in %game internal format,
125  * without alpha and mask values.
126  * @param file the opened file from which to read.
127  * @return
128  * @li 0 in case of success.
129  * @li -1 in case of error.
130  * @sa load_raw ()
131  */
132  s_int8 get_raw (igzstream& file);
133 
134  /** Loads an image from a file name, in game internal format, without alpha
135  * and mask values.
136  * @param fname the name of the file to load.
137  * @return
138  * @li 0 in case of success.
139  * @li -1 in case of error.
140  * @sa get_raw ()
141  */
142  s_int8 load_raw (string fname);
143 
144  /** Loads an image from an opened file, in PNM format, without
145  * alpha and mask values.
146  * @param file the opened file from which to read.
147  * @return
148  * @li 0 in case of success.
149  * @li -1 in case of error.
150  * @sa load_pnm ()
151  */
152  s_int8 get_pnm (SDL_RWops * file);
153 
154  /** Loads an image from a file name, in PNM format, without
155  * alpha and mask values.
156  * @param fname the name of the file to load.
157  * @return
158  * @li 0 in case of success.
159  * @li -1 in case of error.
160  * @sa get_pnm ()
161  */
162  s_int8 load_pnm (string fname);
163 
164  /** Saves an image into an opened file, in %game format, with
165  * alpha and mask values.
166  * @warning as the image which is saved comes from a %screen's depth
167  * surface, it will be slightly altered during the save.
168  * If you want a class capable of saving images with full
169  * truecolor quality, use image_edit instead.
170  * @param file opened file where to save into.
171  * @return
172  * @li 0 in case of success.
173  * @li -1 in case of error.
174  * @sa save ()
175  */
176  s_int8 put (ogzstream& file) const;
177 
178  /** Saves an image into an file, in %game format, with
179  * alpha and mask values.
180  * @warning as the image which is saved comes from a %screen's depth
181  * surface, it will be slightly altered during the save.
182  * If you want a class capable of saving images with full
183  * truecolor quality, use image_edit instead.
184  * @param fname file name where to save into.
185  * @return
186  * @li 0 in case of success.
187  * @li -1 in case of error.
188  * @sa put ()
189  */
190  s_int8 save (string fname) const;
191 
192  /** Saves an image into an opened file, in %game format, without
193  * alpha and mask values.
194  * @warning as the image which is saved comes from a %screen's depth
195  * surface, it will be slightly altered during the save.
196  * If you want a class capable of saving images with full
197  * truecolor quality, use image_edit instead.
198  * @param file opened file where to save into.
199  * @return
200  * @li 0 in case of success.
201  * @li -1 in case of error.
202  * @sa save_raw ()
203  */
204  s_int8 put_raw (ogzstream& file) const;
205 
206  /** Saves an image into an file, in %game format, without
207  * alpha and mask values.
208  * @warning as the image which is saved comes from a %screen's depth
209  * surface, it will be slightly altered during the save.
210  * If you want a class capable of saving images with full
211  * truecolor quality, use image_edit instead.
212  * @param fname file name where to save into.
213  * @return
214  * @li 0 in case of success.
215  * @li -1 in case of error.
216  * @sa put_raw ()
217  */
218  s_int8 save_raw (string fname) const;
219 
220  /** Saves an image into an opened file, in PNM format, without
221  * alpha and mask values.
222  * @warning as the image which is saved comes from a %screen's depth
223  * surface, it will be slightly altered during the save.
224  * If you want a class capable of saving images with full
225  * truecolor quality, use image_edit instead.
226  * @param file opened file where to save into.
227  * @return
228  * @li 0 in case of success.
229  * @li -1 in case of error.
230  * @sa save_pnm ()
231  */
232  s_int8 put_pnm (SDL_RWops * file) const;
233 
234  /** Saves an image into an file, in PNM format, without
235  * alpha and mask values.
236  * @warning as the image which is saved comes from a %screen's depth
237  * surface, it will be slightly altered during the save.
238  * If you want a class capable of saving images with full
239  * truecolor quality, use image_edit instead.
240  * @param fname file name where to save into.
241  * @return
242  * @li 0 in case of success.
243  * @li -1 in case of error.
244  * @sa put_pnm ()
245  */
246  s_int8 save_pnm (string fname) const;
247 
248 
249  //@}
250 
251 
252  /**
253  * @name Special FX Methods.
254  * Allows you to put fantasy in your image manipulations! Can
255  * eventually even be usefull...
256  *
257  */
258  //@{
259 
260 
261  /** Zooms a surface.
262  * Zoom the content of the src surface into this image, to it's own size.
263  * @param src the source image to zoom.
264  */
265  void zoom (const surface& src)
266  {
267  zoom (src, length (), height (), 0, 0);
268  }
269 
270 #ifndef SWIG
271  /**
272  * Zooms a surface.
273  * Zoom the content of the src surface into this image, to the size
274  * (l, h), at position (x, y) on this image.
275  *
276  * @param src The source surface to zoom.
277  * @param l length of the zoomed image.
278  * @param h height of the zoomed image.
279  * @param x X offset on the destination image.
280  * @param y Y offset on the destination image.
281  *
282  * @attention Not available from Python. Use zoom_to () from Python instead.
283  * @sa zoom_to ()
284  */
285  void zoom (const surface& src, u_int16 l, u_int16 h, u_int16 x = 0, u_int16 y = 0);
286 #endif
287 
288  /**
289  * Synonym of zoom () to guarantee its access from Python.
290  *
291  * @sa zoom ()
292  *
293  */
294  void zoom_to (const surface& src, u_int16 l, u_int16 h, u_int16 x = 0, u_int16 y = 0)
295  {
296  zoom (src, l, h, x, y);
297  }
298 
299  /** Tiles a surface.
300  * Tiles the src surface so this image is totally filled.
301  * @param source the source surface to tile.
302  */
303  void tile (const surface& src)
304  {
305  tile (src, length (), height ());
306  }
307 
308 #ifndef SWIG
309  /**
310  * Tiles a surface.
311  * Tiles the src surface so the area of this image starting at position
312  * (x, y) and (l, h) sized is totally filled.
313  * @param source the source surface to tile.
314  * @param l length of the area to tile.
315  * @param h height of the area to tile.
316  * @param x X offset on the destination image.
317  * @param y Y offset on the destination image.
318  *
319  * @attention Not available from Python. Use tile_to () from Python instead.
320  * @sa tile_to ()
321  */
322  void tile (const surface& src, u_int16 l, u_int16 h, u_int16 x = 0, u_int16 y = 0);
323 #endif
324 
325  /**
326  * Synonym of tile () to guarantee its access from Python.
327  *
328  * @sa tile ()
329  *
330  */
331  void tile_to (const surface& src, u_int16 l, u_int16 h, u_int16 x = 0, u_int16 y = 0)
332  {
333  tile (src, l, h, x, y);
334  }
335 
336  /**
337  * Applies a "brightness" to a surface.
338  * Lighten (or darken) the src surface and put the result into this image.
339  * This image will be resized to the src surface's size.
340  * @param src the source surface to lighten/darken.
341  * @param cont the "brightness" value, if < 256 the image will be
342  * darkened.
343  * @todo modify it so when < 128 -> darken, > 128 -> brighten.
344  * @param proceed_mask if set to true, then the translucent pixels will
345  * be lightened/darkened too.
346  */
347  void brightness (const surface& src, u_int8 cont, bool proceed_mask = false);
348  //@}
349 
350 
351 #ifndef SWIG
352  /**
353  * Image copy (similar to copy ()).
354  *
355  * @attention Not available from Python. Use copy () from Python instead.
356  * @sa copy ()
357  */
358  image& operator = (const image& src);
359 #endif
360 
361  /**
362  * Synonym of operator = to guarantee its access from Python.
363  *
364  * @sa operator =
365  */
366  void copy (const image& src)
367  {
368  *this = src;
369  }
370 
371 private:
372  /**
373  * Forbid value passing.
374  */
375  image(const image& src);
376 
377  /**
378  * Converts a raw image source recorded in RGB to the current screen depth
379  * and put it to this image.
380  *
381  * @param rawdata raw data to convert.
382  * @param l length of the raw image.
383  * @param h height of the raw image.
384  */
385  void raw2display (void * rawdata, u_int16 l, u_int16 h);
386 };
387 
388 
389 #endif
void tile_to(const surface &src, u_int16 l, u_int16 h, u_int16 x=0, u_int16 y=0)
Synonym of tile () to guarantee its access from Python.
Definition: image.h:331
s_int8 get_pnm(SDL_RWops *file)
Loads an image from an opened file, in PNM format, without alpha and mask values. ...
Definition: image.cc:144
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
u_int16 length() const
Returns the length of the drawable.
Definition: drawable.h:80
u_int8 scale() const
Get the surfaces current scaling factor.
Definition: surface.h:163
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
void resize(u_int16 l, u_int16 h)
Resize this image.
Definition: image.cc:63
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
virtual ~image()
Destructor.
Definition: image.cc:59
s_int8 load(string fname)
Loads an image from a file name, in game internal format, with alpha and mask values.
Definition: image.cc:92
Class where drawables can actually be drawn to.
Definition: surface.h:85
Declares the screen class.
Image manipulation class.
Definition: image.h:45
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
image()
Default constructor.
Definition: image.cc:35
void tile(const surface &src)
Tiles a surface.
Definition: image.h:303
void clear()
Resets the image to it&#39;s initial state, that is totally empty.
Definition: image.cc:68
s_int8 save(string fname) const
Saves an image into an file, in game format, with alpha and mask values.
Definition: image.cc:192
void zoom_to(const surface &src, u_int16 l, u_int16 h, u_int16 x=0, u_int16 y=0)
Synonym of zoom () to guarantee its access from Python.
Definition: image.h:294
s_int8 load_raw(string fname)
Loads an image from a file name, in game internal format, without alpha and mask values.
Definition: image.cc:128
s_int8 put_pnm(SDL_RWops *file) const
Saves an image into an opened file, in PNM format, without alpha and mask values. ...
Definition: image.cc:230
s_int8 save_pnm(string fname) const
Saves an image into an file, in PNM format, without alpha and mask values.
Definition: image.cc:239
s_int8 put(ogzstream &file) const
Saves an image into an opened file, in game format, with alpha and mask values.
Definition: image.cc:179
image & operator=(const image &src)
Image copy (similar to copy ()).
Definition: image.cc:331
void copy(const image &src)
Synonym of operator = to guarantee its access from Python.
Definition: image.h:366
u_int16 height() const
Returns the height of the drawable.
Definition: drawable.h:91
void zoom(const surface &src)
Zooms a surface.
Definition: image.h:265
s_int8 put_raw(ogzstream &file) const
Saves an image into an opened file, in game format, without alpha and mask values.
Definition: image.cc:204
void brightness(const surface &src, u_int8 cont, bool proceed_mask=false)
Applies a "brightness" to a surface.
Definition: image.cc:298
Declares the igzstream, ogzstream and fileops classes.
s_int8 load_pnm(string fname)
Loads an image from a file name, in PNM format, without alpha and mask values.
Definition: image.cc:163
s_int8 save_raw(string fname) const
Saves an image into an file, in game format, without alpha and mask values.
Definition: image.cc:218
s_int8 get_raw(igzstream &file)
Loads an image from an opened file, saved in game internal format, without alpha and mask values...
Definition: image.cc:104
#define s_int8
8 bits long signed integer
Definition: types.h:44