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