image-base.h

Go to the documentation of this file.
00001 /*
00002  * image-base.h
00003  *
00004  * Copyright (C) 2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * This is the start of the dependency tree.  It defines a simple image object,
00032  * and the interface used by image loaders.
00033  */
00034 
00035 #ifndef WAVEPACKET_IMAGE_BASE_H__
00036 #define WAVEPACKET_IMAGE_BASE_H__
00037 
00038 // includes --------------------------------------------------------------------
00039 #include "common/common.h"
00040 #include "common/wave_ex.h"
00041 
00042 
00043 namespace nstream {
00044 class Manager;  // forward declaration
00045 class Stream;
00046 };
00047 
00048 
00049 
00050 namespace media {
00051 
00052 /// \ingroup wave_image
00053 /*@{*/
00054 
00055 ////////////////////////////////////////////////////////////////////////////////
00056 ///
00057 /// \defgroup image_base Base Image Library
00058 ///
00059 /// This library is used only by clients that are writing an image loader.
00060 /// If you just want to load images, use the \ref wave_image library instead.
00061 ///
00062 ////////////////////////////////////////////////////////////////////////////////
00063 /*@{*/
00064 
00065 
00066 /// refers to what sort of data you have per-pixel in the image
00067 enum eColorFormat {
00068         eColorFormat_Grayscale          = 1,    ///< grayscale, usually 8 bit
00069         eColorFormat_GrayAlpha          = 2,    ///< gray plus alpha, 16 bit?
00070 
00071         eColorFormat_RGB                = 5,    ///< RGB, usually 24 bit
00072         eColorFormat_RGBA               = 6,    ///< RGBA, usually 32 bit
00073 
00074         // keep this last!
00075         eColorFormat_Invalid            = 0
00076 };
00077 
00078 
00079 /// this is a basic image object.  Contains some image metadata as well as the
00080 ///     actual pixel data.  Use the color_format and bytes_per_pixel to
00081 ///     determine how to interpret the raw data.
00082 struct image_t {
00083         // constants
00084         enum eConstants {
00085                 eMaxString      = 8
00086         };
00087 
00088         // constructor, destructor
00089         image_t(void) throw() : data(NULL) { this->clear(); }
00090         ~image_t(void) throw() { this->clear(); }
00091 
00092         // manipulators, accessors
00093         void clear(void) throw() {
00094                         width = height = 0;
00095                         bit_depth = 0;
00096                         color_format = eColorFormat_Invalid;
00097                         bytes_per_pixel = 0;
00098                         imageType[0] = 0;
00099                         if (data) {
00100                                 delete[] data;
00101                                 data = NULL;
00102                         }
00103                 }
00104         void setImageType(IN const char * type) throw() {
00105                         strncpy(imageType, type, eMaxString);
00106                         imageType[eMaxString - 1] = 0;
00107                 }
00108         bool isValid(void) const throw() {
00109                         return (width > 0 &&
00110                                 height > 0 &&
00111                                 data &&
00112                                 (eColorFormat_Invalid != color_format) &&
00113                                 bit_depth > 0 &&
00114                                 bytes_per_pixel > 0);
00115                 }
00116         void dump(IN const char * txt) const throw();
00117         void allocate(void) {
00118                         ASSERT(!data, "already allocated?");
00119                         ASSERT(width > 0, "bad width: %d", width);
00120                         ASSERT(height > 0, "bad height: %d", height);
00121                         ASSERT(bytes_per_pixel > 0, "Bad bpp: %d", bytes_per_pixel);
00122 
00123                         long nBytes = width * height * bytes_per_pixel;
00124                         data = new byte_t[nBytes];
00125                         ASSERT_THROW(data, "out of memory");
00126                 }
00127 
00128         // data fields
00129         int32_t         width;
00130         int32_t         height;
00131         int32_t         bit_depth;
00132         eColorFormat    color_format;
00133         int32_t         bytes_per_pixel;
00134         char            imageType[eMaxString];
00135         byte_t *        data;
00136 };
00137 
00138 
00139 
00140 ////////////////////////////////////////////////////////////////////////////////
00141 //
00142 //      API for image loaders
00143 //
00144 //      If you want to support a new kind of image and make it available to the
00145 //      media::loadImage() call, these are the interfaces to work with.
00146 //
00147 ////////////////////////////////////////////////////////////////////////////////
00148 
00149 /// anyone wanting to support loading an image type must provide this interface.
00150 /// <b>Warning to implementers: keep your image loaders entirely stateless!</b>
00151 /// ImageLoader objects can be called on any thread, so they should be
00152 ///     threadsafe.  In practice that is easy so long as the loader is
00153 ///     stateless.  (stateless means no static or class member variables--the
00154 ///     only state is the image being loaded, or other state on the stack).
00155 class ImageLoader {
00156 public:
00157         // virtual constructor -------------------------------------------------
00158         virtual ~ImageLoader(void) throw();
00159 
00160         // media::ImageLoader class interface methods --------------------------
00161 
00162         /// tell us the name of yourself
00163         virtual const char * getLoaderName(void) const throw() = 0;
00164 
00165         /// image loader should return true if this is an image this loader can
00166         ///     load.
00167         virtual bool canLoadImage(IN nstream::Manager * mgr,
00168                                 IN const char * name) const = 0;
00169 
00170         /// here the loader actually loads
00171         virtual void load(IN nstream::Stream * stream,
00172                                 OUT image_t& image) const = 0;
00173 };
00174 
00175 
00176 
00177 };      // media namespace
00178 
00179 
00180 
00181 #endif  // WAVEPACKET_IMAGE_BASE_H__
00182