1 /// \file CVImageGrey.h
2 /// \brief CVImageGrey - 8-bit grey intensity image class
3 // Written by Michael Ellison
4 //-------------------------------------------------------------------------
5 //                      CodeVis's Free License
6 //                         www.codevis.com
7 //
8 // Copyright (c) 2003 by Michael Ellison (mike@codevis.com)
9 // All rights reserved.
10 //
11 // You may use this software in source and/or binary form, with or without
12 // modification, for commercial or non-commercial purposes, provided that
13 // you comply with the following conditions:
14 //
15 // * Redistributions of source code must retain the above copyright notice,
16 //   this list of conditions and the following disclaimer.
17 //
18 // * Redistributions of modified source must be clearly marked as modified,
19 //   and due notice must be placed in the modified source indicating the
20 //   type of modification(s) and the name(s) of the person(s) performing
21 //   said modification(s).
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //
35 //---------------------------------------------------------------------------
36 // Modifications:
37 //
38 //---------------------------------------------------------------------------
39 //
40 /// \class CVImageGrey CVImageGrey.h
41 /// CVImageGrey is an 8-bit greyscale image class derived from CVImage
42 ///
43 /// See CVImage.h for general documentation.
44 ///
45 /// \sa CVImage, CVImageRGB24, CVImageRGBFloat
46 ///
47 /// $RCSfile: CVImageGrey.h,v $
48 /// $Date: 2004/02/08 23:47:39 $
49 /// $Revision: 1.1.1.1 $
50 /// $Author: mikeellison $
51 
52 #ifndef _CVImageGrey_H_
53 #define _CVImageGrey_H_
54 
55 #include "CVResImage.h"
56 #include "CVImage.h"
57 
58 #ifdef WIN32
59    #include <windows.h> // For SetFromWin32Bmp
60 #endif
61 
62 class CVImageRGB24;
63 
64 class CVImageGrey : public CVImage
65 {
66    // Allow CVImage to use our constructor
67    friend CVImage;
68 
69    public:
70       //---------------------------------------------------------------------
71       // Overridden functions
72       //---------------------------------------------------------------------
73       /// GetNumChannels retrieves the number of channels per pixel.
74       /// This is one in greyscale, 3 in RGB, and 4 in RGBA
75       ///
76       /// \return int - number of channels per pixel.
77       /// \sa GetBytesPerPixel()
78       virtual int GetNumChannels() const;
79 
80       /// GetBytesPerPixel retrieves the number of bytes per pixel.
81       /// Note that pixel can be in floating point or integer format, depending
82       /// on the image type.
83       ///
84       /// \return int - bytes per pixel
85       virtual int GetBytesPerPixel() const;
86 
87       /// GetImageType() retrieves the image type.
88       /// See CVIMAGE_TYPE enum.
89       ///
90       /// \return CVIMAGE_TYPE specifying the image's type.
91       virtual CVIMAGE_TYPE   GetImageType   () const;
92 
93       /// GetPNMExtension() retrieves the default file extension for PNM
94       /// file saving. (e.g. ".pgm" for greyscale)
95       ///
96       /// \return const char* - ASCIIZ default file extension,
97       ///                       including preceeding '.'
98       /// \sa Load(), Save()
99       virtual const char *GetPNMExtension() const;
100 
101       /// GetPNMMagicVal() retrieves the magic value for a pnm file
102       /// matching the current image format.
103       ///
104       /// \return char - Magic value for PNM files (e.g. '5' for 'P5' .pgm's)
105       /// \sa Load(), Save()
106       virtual char GetPNMMagicVal() const;
107 
108       /// GetMaxPixelValue() retrieves the maximum value of any pixel in
109       /// the image.
110       ///
111       /// In multichannel images (e.g. RGB triplets), it will
112       /// return the maximum value on any of the channels.
113       ///
114       /// All child classes should implement this.
115       /// \param maxValue - reference to max pixel value, set on return.
116       /// \return CVRES result code
117       /// \sa GetPixel(), SetPixel(), CVImage::GetMaxPixel()
118       virtual CVRES GetMaxPixelValue(float& maxValue) const;
119 
120       /// GetPixel() retrieves the red, green, and blue values for a specified
121       /// pixel as floating points.
122       ///
123       /// This is for convenience and prototyping - for high-speed image
124       /// processing you'll need to work more directly with the image
125       /// buffer.
126       ///
127       /// Within CVImageGrey, this returns the intensity value on all
128       /// three channels (red, green, and blue).
129       ///
130       /// \param x - x position within the image of the pixel
131       /// \param y - y position within the image of the pixel
132       /// \param r - receives the red value of the pixel
133       /// \param g - receives the green value of the pixel
134       /// \param b - receives the blue value of the pixel
135       ///
136       /// \return CVRES result code.  CVRES_SUCCESS on success.
137       /// \sa SetPixel()
138       virtual CVRES GetPixel( int      x,
139                               int      y,
140                               float&   r,
141                               float&   g,
142                               float&   b) const;
143 
144       /// SetPixel() sets the red, green, and blue pixel values
145       /// for a pixel
146       ///
147       /// This is for convenience and prototyping - for high-speed image
148       /// processing you'll need to work more directly with the image
149       /// buffer.
150       ///
151       /// Within CVImageGrey(), this sets the pixel to:
152       ///    value = 0.299r + 0.587g + 0.114b
153       ///
154       /// Values are from the Y (Luminance) in YIQ conversion,
155       ///   Computer Graphics, Principles and Practice 2nd Ed.
156       ///   by Foley, van Dam, Feiner, Hughes.
157       ///
158       /// Intensity values above 255 will be truncated to 255. Values
159       /// below 0 will be set to 0.
160       ///
161       /// \param x - x position within the image of the pixel
162       /// \param y - y position within the image of the pixel
163       /// \param r - receives the red value of the pixel
164       /// \param g - receives the green value of the pixel
165       /// \param b - receives the blue value of the pixel
166       ///
167       /// \return CVRES result code.  CVRES_SUCCESS on success.
168       /// \sa GetPixel()
169       virtual CVRES SetPixel       (   int      x,
170                                        int      y,
171                                        float    r,
172                                        float    g,
173                                        float    b);
174 
175 
176    protected:
177 #ifdef WIN32
178       /// Win32-only function for setting image data from a bitmap.
179       /// WARNING: Currently only supports 24-bit uncompressed RGB bitmaps
180       ///
181       /// Bitmap header and data may be freed after call - we do a deep copy
182       /// of the data we care about.
183       ///
184       /// \param bmih - BITMAPINFOHEADER with format information
185       /// \param data - raw bitmap data matching bmih format.
186       ///
187       /// \return CVRES result code.
188       /// \sa ::CVRES_CORE_ENUM, ::CVRES_IMAGE_ENUM, CVImage::ReleaseImage()
189       virtual CVRES SetFromWin32Bmp(  const BITMAPINFOHEADER* bmih,
190                                       const unsigned char*    data);
191 #endif // WIN32
192 
193       //---------------------------------------------------------------------
194    protected:
195       /// Protected constructor - use CVImage::CreateImage() to construct
196       CVImageGrey();
197       /// Protected destructor - use CVImage::ReleaseImage() to destroy
198       virtual ~CVImageGrey();
199 };
200 
201 #endif // _CVImageGrey_H_
202 
203