]> git.lyx.org Git - features.git/blob - src/graphics/GraphicsImageXPM.h
Really dull and boring header shit
[features.git] / src / graphics / GraphicsImageXPM.h
1 // -*- C++ -*-
2 /**
3  *  \file GraphicsImageXPM.h
4  *  Read the file COPYING
5  *
6  *  \author Baruch Even 
7  *  \author Angus Leeming 
8  *
9  * Full author contact details are available in file CREDITS
10  *
11  *  An instantiation of Image that makes use of libXPM to load and store
12  *  the image in memory.
13  */
14
15 #ifndef GRAPHICSIMAGEXPM_H
16 #define GRAPHICSIMAGEXPM_H
17
18 #include "GraphicsImage.h"
19 #include XPM_H_LOCATION
20
21 #include <boost/shared_ptr.hpp>
22
23 #ifdef __GNUG__
24 #pragma interface
25 #endif
26
27 namespace grfx {
28
29 class ImageXPM : public Image
30 {
31 public:
32         /// Access to this class is through this static method.
33         static ImagePtr newImage();
34
35         /// Return the list of loadable formats.
36         static FormatList loadableFormats();
37
38         ///
39         ~ImageXPM();
40
41         /// Create a copy
42         Image * clone() const;
43
44         ///
45         Pixmap getPixmap() const;
46
47         /// Get the image width
48         unsigned int getWidth() const;
49
50         /// Get the image height
51         unsigned int getHeight() const;
52
53         bool isDrawable() const;
54
55         /** Load the image file into memory.
56          *  In this case (ImageXPM), the process is blocking.
57          */
58         void load(string const & filename);
59
60         /** Generate the pixmap, based on the current state of the
61          *  xpm_image_ (clipped, rotated, scaled etc).
62          *  Uses the params to decide on color, grayscale etc.
63          *  Returns true if the pixmap is created.
64          */
65         bool setPixmap(Params const & params);
66
67         /// Clip the image using params.
68         void clip(Params const & params);
69
70         /// Rotate the image using params.
71         void rotate(Params const & params);
72
73         /// Scale the image using params.
74         void scale(Params const & params);
75
76 private:
77         /// Access to the class is through newImage() and clone.
78         ImageXPM();
79         ///
80         ImageXPM(ImageXPM const &);
81
82         /** Contains the data read from file.
83          *  This class is a wrapper for a XpmImage struct, but all views
84          *  of a single file's data will share the same color table.
85          *  This is done by ensuring that the color table contains a "none"
86          *  c_color together with g_color and m_color entries for each c_color
87          *  entry when it is first stored.
88          */
89         class Data
90         {
91         public:
92                 /// Default c-tor. Initialise everything to zero.
93                 Data();
94                 ~Data();
95
96                 bool empty() const { return width_ == 0; }
97
98                 /** Wrap an XpmImage in a nice, clean C++ interface.
99                  *  Empty the original XpmImage.
100                  *  Does some analysis of the color table to ensure that
101                  *  it is suitable for all future eventualities. (See above
102                  *  description.)
103                  */
104                 void reset(XpmImage & image);
105
106                 /// Reset the data struct with this data.
107                 void resetData(int width, int height, unsigned int * data);
108
109                 /** Returns a ptr to an initialised block of memory.
110                  *  the data is initialised to the color "none" entry.
111                  */
112                 unsigned int * initialisedData(int width, int height) const;
113
114                 /** Construct an XpmImage from the stored contents.
115                  *  To pass to XpmCreatePixmapFromXpmImage.
116                  *  Efficient, because we only copy the ptrs to the structs.
117                  */
118                 XpmImage get() const;
119
120                 unsigned int width()   const { return width_; }
121                 unsigned int height()  const { return height_; }
122                 unsigned int cpp()     const { return cpp_; }
123                 unsigned int ncolors() const { return ncolors_; }
124                 unsigned int const * data() const
125                         { return data_.get(); }
126                 XpmColor const * colorTable() const
127                         { return colorTable_.get(); }
128
129         private:
130                 unsigned int width_;
131                 unsigned int height_;
132                 unsigned int cpp_;
133                 unsigned int ncolors_;
134                 boost::shared_ptr<unsigned int> data_;
135                 boost::shared_ptr<XpmColor> colorTable_;
136
137                 unsigned int color_none_id() const;
138         };
139
140         Data image_;
141
142         /// The pixmap itself.
143         Pixmap pixmap_;
144
145         /// Is the pixmap initialized?
146         enum PixmapStatus {
147                 ///
148                 PIXMAP_UNINITIALISED,
149                 ///
150                 PIXMAP_FAILED,
151                 ///
152                 PIXMAP_SUCCESS
153         };
154
155         PixmapStatus pixmap_status_;
156 };
157
158 } // namespace grfx
159
160 #endif // GRAPHICSIMAGEXPM_H