Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members

plbitmap.h

00001 /*
00002 /--------------------------------------------------------------------
00003 |
00004 |      $Id: plbitmap.h,v 1.11 2003/11/05 15:17:23 artcom Exp $
00005 |
00006 |      Copyright (c) 1996-2002 Ulrich von Zadow
00007 |
00008 \--------------------------------------------------------------------
00009 */
00010 
00011 #ifndef INCL_PLBITMAP
00012 #define INCL_PLBITMAP
00013 
00014 #include "plbmpinfo.h"
00015 #include "plpoint.h"
00016 #include "pldebug.h"
00017 #include "plpixel32.h"
00018 #include "plpixel24.h"
00019 
00020 class PLFilter;
00021 
00022 //! Device- and OS-independent bitmap class. Manipulates uncompressed
00023 //! bitmaps of all color depths.
00024 //!
00025 //! This class is an abstract base class. It exists to define a
00026 //! format-independent interface for bitmap manipulation and to
00027 //! provide common routines. Derived classes must support at least
00028 //! the color depths 1, 8 and 32 bpp. PLBmp defines a public interface
00029 //! for general use and a protected interface for use by derived
00030 //! classes.
00031 //!
00032 //! For 32 bpp, alpha channel information is stored in one byte
00033 //! (PL_RGBA_ALPHA) of each 4-byte pixel. To allow for optimizations
00034 //! when no alpha channel is present, a flag is set whenever the
00035 //! alpha information is valid. The complete alpha channel of a
00036 //! bitmap can be replaced by a different one by calling
00037 //! SetAlphaChannel(). A 0 in an alpha channel entry is completely
00038 //! transparent; a 255 is completely opaque.
00039 class PLBmp : public PLBmpInfo
00040 {
00041 
00042 public:
00043 
00044   //! Empty constructor. Constructors in derived classes create a
00045   //! small empty bitmap to ensure that the object is always in a
00046   //! sane state.
00047   PLBmp
00048     ();
00049 
00050   //! Empty destructor.
00051   virtual ~PLBmp
00052     ();
00053 
00054   //! Assignment operator. Note that assignment between different derived
00055   //! classes is possible and results in a format conversion.
00056   PLBmp &operator=
00057     ( PLBmp const &Orig
00058     );
00059 
00060   //! Test for equality. This function actually tests every pixel, so
00061   //! it's not fast. It's meant mainly for use in asserts and such.
00062   bool const operator ==
00063     ( PLBmp const &Other
00064     );
00065 
00066 #ifdef _DEBUG
00067   virtual void AssertValid
00068     () const;    // Tests internal object state
00069 #endif
00070 
00071   // PLBmp creation
00072 
00073   //! Creates a new empty bitmap. Memory for the bits is allocated
00074   //! but not initialized. Previous contents of the bitmap object are
00075   //! discarded. If bAlphaChannel is true, the bitmap is assumed to
00076   //! contain a valid alpha channel.
00077   virtual void Create
00078     ( PLLONG Width,
00079       PLLONG Height,
00080       PLWORD BitsPerPixel,
00081       bool bAlphaChannel,
00082       bool bIsGreyscale, 
00083       PLBYTE * pBits = 0,
00084       int Stride = 0, 
00085       const PLPoint& Resolution = PLPoint (0,0)
00086     );
00087 
00088   //! Creates a new empty bitmap. Info contains the metadata (width, height, 
00089   //! etc.) to be used in creation.
00090   virtual void Create
00091     ( const PLBmpInfo& Info 
00092     );
00093 
00094   //! Creates a copy of rSrPLBmp, converting color depth if nessesary.
00095   //! Supports 1, 8, 24 and 32 BPP. Alpha channel information is preserved.
00096   void CreateCopy
00097     ( const PLBmp & rSrPLBmp,
00098       int BPPWanted = 0
00099     );
00100 
00101   //! Creates a copy of rSrPLBmp, applying rFilter on the way. Depending
00102   //! on the filter called, this is often much faster than CreateCopy()
00103   //! followed by ApplyFilter().
00104   void CreateFilteredCopy (PLBmp & rSrPLBmp, const PLFilter & rFilter);
00105 
00106   // PLBmp manipulation
00107 
00108   //! Sets quality of conversion to 8 bpp by CreateCopy(). Valid parameters are
00109   //! defined in FilterQuantize.h.
00110   void SetQuantizationMode
00111     ( int DitherType,
00112       int DitherPaletteType
00113     );
00114 
00115   //! Fills the color table with a grayscale palette. This function
00116   //! is only useable for bitmaps containing a color table. Index 0
00117   //! contains black (0) and the last index contains white (255). The
00118   //! alpha channel is set to opaque (255) for every palette entry.
00119   void SetGrayPalette
00120     ();
00121 
00122   //! Sets the color table to pPal. The contents or pPal are copied.
00123   void SetPalette
00124     ( PLPixel32 * pPal
00125     );
00126 
00127   //! Sets one entry in the color table. The function may only be
00128   //! called if there is a color table stored with the bitmap. The
00129   //! color table entry is set to the red, green, blue, and alpha
00130   //! values specified.
00131   void SetPaletteEntry
00132     ( PLBYTE Entry,
00133       PLBYTE r,
00134       PLBYTE g,
00135       PLBYTE b,
00136       PLBYTE a
00137     );
00138 
00139   //! Sets one entry in the color table. The function may only be
00140   //! called if there is a color table stored with the bitmap. The
00141   //! color table entry is set to the red, green, blue, and alpha
00142   //! values specified.
00143   void SetPaletteEntry
00144     ( PLBYTE Entry,
00145       PLPixel32 Value
00146     );
00147 
00148   //! Replaces the alpha channel of the bitmap with a new one. This
00149   //! only works for bitmaps with 32 bpp. pAlphaBmp must point to an
00150   //! 8 bpp bitmap with the same dimensions as the object. The alpha
00151   //! channel information is physically copied into the bitmap.
00152   void SetAlphaChannel
00153     ( PLBmp * pAlphaBmp
00154     );
00155 
00156   //! Applies a filter to the bitmap.
00157   void ApplyFilter
00158     ( const PLFilter& Filter
00159     );
00160 
00161   //! Slow but simple function to set a single pixel. 32 bpp only.
00162   void SetPixel
00163     ( int x,
00164       int y,
00165       PLPixel32 pixel
00166     );
00167 
00168   //! Slow but simple function to get a single pixel. 32 bpp only.
00169   PLPixel32 GetPixel
00170     ( int x,
00171       int y
00172     ) const;
00173 
00174   //! Find the nearest color to cr in the palette used by this bitmap
00175   //! Only works for 8 bpp bitmaps.
00176   PLBYTE FindNearestColor
00177     ( PLPixel32 cr
00178     );
00179 
00180   // PLBmp information.
00181   //! Returns memory used by a bitmap
00182   virtual long GetMemUsed
00183     () = 0;
00184 
00185   //! Returns number of colors that can be stored by a bitmap with this
00186   //! this color depth.
00187   int GetNumColors
00188     ();
00189 
00190   //! Sets whether a bitmap stores an alpha channel. Works for 8 and 32 bpp 
00191   //! bitmaps. In either case, if b is true and the bitmap did not have an 
00192   //! alpha channel before the call, the complete alpha channel is set to
00193   //! opaque by the call.
00194   void SetHasAlpha
00195     (bool b
00196     );
00197 
00198   //! Returns number of bytes used per line.
00199   virtual long GetBytesPerLine
00200     () = 0;
00201 
00202   // PLBmp direct manipulation
00203 
00204   //! Returns the address of the color table of the bitmap or NULL if
00205   //! no color table exists. The color table is stored as an array of
00206   //! consecutive PLPixel32 objects.
00207   PLPixel32 * GetPalette
00208     () const;
00209 
00210   //! Returns pointer to an array containing the starting addresses of
00211   //! the bitmap lines. This array should be used whenever the bitmap
00212   //! bits need to be manipulated directly.
00213   PLBYTE ** GetLineArray
00214     () const;
00215 
00216   //! Returns pointer to an array containing the starting addresses of
00217   //! the bitmap lines. This array should be used whenever the bitmap
00218   //! bits need to be manipulated directly.
00219   PLPixel32 ** GetLineArray32
00220     () const;
00221 
00222   //! Returns pointer to an array containing the starting addresses of
00223   //! the bitmap lines. This array should be used whenever the bitmap
00224   //! bits need to be manipulated directly.
00225   PLPixel24 ** GetLineArray24
00226     () const;
00227 
00228   //! Locks bitmap. Currently, this is only usedby PLDDrawBmp - other derived 
00229   //! classes always behave as if the bitmap were locked.
00230   //! GetLineArray() and other direct-access methods should
00231   //! only be called if the bitmap is locked. Lock and Unlock keep a lock
00232   //! count. In most cases (currently: all but PLDDrawBmp), the lock count
00233   //! will always be >= 1.
00234   virtual void Lock
00235     ( bool bReadable,
00236       bool bWriteable
00237     );
00238 
00239   //! Unlocks the Bitmap surface. (See Lock for specifics.)
00240   virtual void Unlock
00241     ();
00242 
00243   //! Returns whether a bitmap surface is locked. (See Lock for specifics.)
00244   bool IsLocked
00245     () const;
00246 
00247   //! Returns true if Bmp and this are almost equal. The comparison is done by 
00248   //! comparing the pixels in the bitmaps component-wise. If all components are 
00249   //! closer than epsilon, the bitmaps are considered almost equal.
00250   bool AlmostEqual
00251     ( const PLBmp& Bmp,
00252       int epsilon
00253     ) const;
00254 
00255   //! Sets the bitmap resolution in pixels per inch.
00256   void SetResolution (const PLPoint& Resolution);
00257 
00258 protected:
00259 
00260 
00261   //! Create a new bitmap with uninitialized bits. (Assume no memory
00262   //! is allocated yet.)
00263   virtual void internalCreate
00264     ( PLLONG Width,
00265       PLLONG Height,
00266       PLWORD BitsPerPixel,
00267       bool bAlphaChannel,
00268       bool bIsGreyscale
00269     ) = 0;
00270 
00271   //! Delete memory allocated by member variables.
00272   virtual void freeMembers
00273     () = 0;
00274 
00275   //! Initialize internal table of line addresses.
00276   virtual void initLineArray
00277     () = 0;
00278 
00279   //! Creates a new PLBmp as copy of rSrPLBmp. Assumes there is no memory
00280   //! allocated yet.
00281   void internalCopy
00282     ( const PLBmp & rSrPLBmp
00283     );
00284 
00285   //! Can be called from internalCreate() to initialize object state.
00286   void initLocals
00287     ( PLLONG Width,
00288       PLLONG Height,
00289       PLWORD BitsPerPixel,
00290       bool bAlphaChannel,
00291       bool bIsGreyscale    
00292     );
00293 
00294   void create8BPPCopy
00295     ( const PLBmp & rSrPLBmp
00296     );
00297 
00298   void create1BPPCopy
00299     ( const PLBmp & rSrPLBmp
00300     );
00301 
00302   // Member variables
00303   PLPixel32 * m_pClrTab;      // Pointer to the color table.
00304   PLBYTE    ** m_pLineArray;   // Table of the starting addresses of
00305                              // the lines.
00306   int        m_LockCount;    // Number of times the bitmap was locked.
00307                              // Default is m_LockCount always >= 1, so
00308                              // access to bits is always possible.
00309   int m_DitherType;
00310   int m_DitherPaletteType;
00311 };
00312 
00313 inline PLBmp & PLBmp::operator=
00314     ( PLBmp const &Orig
00315     )
00316 {
00317   if (this != &Orig)
00318     CreateCopy(Orig);
00319   return *this;
00320 }
00321 
00322 inline void PLBmp::SetPaletteEntry
00323     ( PLBYTE Entry,
00324       PLBYTE r,
00325       PLBYTE g,
00326       PLBYTE b,
00327       PLBYTE a
00328     )
00329 {
00330   m_pClrTab[Entry].Set (r, g, b, a);
00331 }
00332 
00333 inline void PLBmp::SetPaletteEntry
00334     ( PLBYTE Entry,
00335       PLPixel32 Value
00336     )
00337 {
00338   m_pClrTab[Entry] = Value;
00339 }
00340 
00341 
00342 inline PLPixel32 PLBmp::GetPixel (int x, int y) const
00343 {
00344   PLASSERT (GetBitsPerPixel() == 32);
00345   return *((PLPixel32 *)GetLineArray()[y]+x);
00346 }
00347 
00348 inline void PLBmp::SetPixel (int x, int y, PLPixel32 pixel)
00349 {
00350   PLASSERT (GetBitsPerPixel() == 32);
00351   *(GetLineArray32()[y]+x) = pixel;
00352 }
00353 
00354 inline void PLBmp::SetResolution(const PLPoint& Resolution)
00355 {
00356   PLASSERT_VALID(this);
00357   m_Resolution = Resolution;
00358 }
00359 
00360 inline int PLBmp::GetNumColors
00361     ()
00362 {
00363   PLASSERT_VALID (this);
00364 
00365   if (m_bpp == 32)
00366     return 1 << 24;
00367    else
00368     return 1 << m_bpp;
00369 }
00370 
00371 
00372 // PLBmp direct manipulation
00373 
00374 inline PLBYTE ** PLBmp::GetLineArray
00375     () const
00376 {
00377   PLASSERT (m_pLineArray);
00378   return m_pLineArray;
00379 }
00380 
00381 inline PLPixel32 ** PLBmp::GetLineArray32
00382     () const
00383 {
00384   PLASSERT (m_pLineArray);
00385   PLASSERT (m_bpp == 32);
00386   return (PLPixel32**)m_pLineArray;
00387 }
00388 
00389 inline PLPixel24 ** PLBmp::GetLineArray24
00390     () const
00391 {
00392   PLASSERT (m_pLineArray);
00393   PLASSERT (m_bpp == 24);
00394   return (PLPixel24**)m_pLineArray;
00395 }
00396 
00397 inline PLPixel32 * PLBmp::GetPalette
00398     () const
00399     // Returns adress of the color table of the bitmap or NULL if no
00400     // color table exists.
00401 {
00402   PLASSERT_VALID (this);
00403 
00404   return m_pClrTab;
00405 }
00406 
00407 inline bool PLBmp::IsLocked
00408     () const
00409 {
00410   return (m_LockCount >= 1);
00411 }
00412 
00413 
00414 #endif
00415 /*
00416 /--------------------------------------------------------------------
00417 |
00418 |      $Log: plbitmap.h,v $
00419 |      Revision 1.11  2003/11/05 15:17:23  artcom
00420 |      Added ability to specify initial data in PLBitmap::Create()
00421 |
00422 |      Revision 1.10  2003/07/29 21:27:41  uzadow
00423 |      Fixed PLDirectFBBmp::GetBytesPerLine(), im2* Makefiles
00424 |
00425 |      Revision 1.9  2003/02/15 21:26:58  uzadow
00426 |      Added win32 version of url data source.
00427 |
00428 |      Revision 1.8  2002/08/04 20:08:01  uzadow
00429 |      Added PLBmpInfo class, ability to extract metainformation from images without loading the whole image and proper greyscale support.
00430 |
00431 |      Revision 1.7  2002/03/31 13:36:41  uzadow
00432 |      Updated copyright.
00433 |
00434 |      Revision 1.6  2001/10/21 17:12:39  uzadow
00435 |      Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel.
00436 |
00437 |      Revision 1.5  2001/10/16 17:12:26  uzadow
00438 |      Added support for resolution information (Luca Piergentili)
00439 |
00440 |      Revision 1.4  2001/10/06 22:37:08  uzadow
00441 |      Linux compatibility.
00442 |
00443 |      Revision 1.3  2001/10/06 22:03:26  uzadow
00444 |      Added PL prefix to basic data types.
00445 |
00446 |      Revision 1.2  2001/09/28 19:50:56  uzadow
00447 |      Added some 24 bpp stuff & other minor features.
00448 |
00449 |      Revision 1.1  2001/09/16 19:03:22  uzadow
00450 |      Added global name prefix PL, changed most filenames.
00451 |
00452 |      Revision 1.26  2001/09/15 14:30:20  uzadow
00453 |      Fixed PLPixel32 initialization bug.
00454 |
00455 |      Revision 1.25  2001/09/13 20:47:36  uzadow
00456 |      Removed commented-out lines.
00457 |
00458 |      Revision 1.24  2001/01/15 15:05:31  uzadow
00459 |      Added PLBmp::ApplyFilter() and PLBmp::CreateFilteredCopy()
00460 |
00461 |      Revision 1.23  2000/12/18 22:42:52  uzadow
00462 |      Replaced RGBAPIXEL with PLPixel32.
00463 |
00464 |      Revision 1.22  2000/11/21 20:18:03  uzadow
00465 |      Added operator ==
00466 |
00467 |      Revision 1.21  2000/11/07 15:40:46  jmbuena
00468 |      Changes related to paintlibdefs.h and pixeldefs.h
00469 |
00470 |      Revision 1.20  2000/11/02 21:28:47  uzadow
00471 |      Fixed copy constructors.
00472 |
00473 |      Revision 1.19  2000/10/24 16:46:34  uzadow
00474 |      Fixed build problems
00475 |
00476 |      Revision 1.18  2000/10/23 17:45:03  jmbuena
00477 |      Linux compatibility changes
00478 |
00479 |      Revision 1.17  2000/09/26 14:28:47  Administrator
00480 |      Added Threshold filter
00481 |
00482 |      Revision 1.16  2000/09/26 12:14:51  Administrator
00483 |      Refactored quantization.
00484 |
00485 |      Revision 1.15  2000/08/13 12:11:43  Administrator
00486 |      Added experimental DirectDraw-Support
00487 |
00488 |      Revision 1.14  2000/07/11 17:11:00  Ulrich von Zadow
00489 |      Added support for RGBA pixel ordering (Jose Miguel Buenaposada Biencinto).
00490 |
00491 |      Revision 1.13  2000/03/31 12:20:05  Ulrich von Zadow
00492 |      Video invert filter (beta)
00493 |
00494 |      Revision 1.12  2000/03/31 11:53:30  Ulrich von Zadow
00495 |      Added quantization support.
00496 |
00497 |      Revision 1.11  2000/01/16 20:43:12  anonymous
00498 |      Removed MFC dependencies
00499 |
00500 |      Revision 1.10  1999/12/10 01:27:26  Ulrich von Zadow
00501 |      Added assignment operator and copy constructor to
00502 |      bitmap classes.
00503 |
00504 |      Revision 1.9  1999/12/09 16:35:22  Ulrich von Zadow
00505 |      no message
00506 |
00507 |      Revision 1.8  1999/12/08 15:39:45  Ulrich von Zadow
00508 |      Unix compatibility changes
00509 |
00510 |      Revision 1.7  1999/12/02 17:07:34  Ulrich von Zadow
00511 |      Changes by bdelmee.
00512 |
00513 |      Revision 1.6  1999/10/22 21:25:51  Ulrich von Zadow
00514 |      Removed buggy octree quantization
00515 |
00516 \--------------------------------------------------------------------
00517 */

Generated on Sun Jun 6 13:42:21 2004 for paintlib by doxygen 1.3.2