Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

plbmpenc.cpp

Go to the documentation of this file.
00001 /*
00002 /--------------------------------------------------------------------
00003 |
00004 |      Windows bitmap file encoder. Encodes 1, 4, 8, 24 and 32 bpp
00005 |      bitmaps to a 1, 4, 8 or 24 bpp uncompressed BMP file
00006 |
00007 |      Copyright (c) 1996-1998 Ulrich von Zadow
00008 |
00009 \--------------------------------------------------------------------
00010 */
00011 #include "plstdpch.h"
00012 #include "plbmpenc.h"
00013 #include "plwindefs.h"
00014 #include "plexcept.h"
00015 
00016 
00018 // Class functions
00019 
00020 PLBmpEncoder::PLBmpEncoder
00021   () : PLPicEncoder()
00022     // Creates an encoder
00023 {
00024   // nothing to do (yet)
00025 }
00026 
00027 
00028 PLBmpEncoder::~PLBmpEncoder
00029     ()
00030 {
00031   // nothing to do (yet)
00032 }
00033 
00034 int PLBmpEncoder::GetLineMemNeeded (PLLONG width, PLWORD BitsPerPixel)
00035 {
00036   // Calculate memory per line.
00037   int LineMem = width * BitsPerPixel / 8;
00038 
00039   if (BitsPerPixel == 1 && width % 8)
00040     ++LineMem;
00041 
00042   // DWORD alignment
00043   return ((LineMem + 3) & ~3);
00044 }
00045 
00046 
00047 void PLBmpEncoder::DoEncode
00048     ( PLBmp * pBmp,
00049       PLDataSink * pSink
00050     )
00051 {
00052   int BPP = pBmp->GetBitsPerPixel();
00053 
00054   WINBITMAPFILEHEADER BFH;
00055   BFH.bfType = *((PLWORD*)"BM");
00056   BFH.bfReserved1 = 0;
00057   BFH.bfReserved2 = 0;
00058   BFH.bfOffBits = sizeof (WINBITMAPFILEHEADER) +
00059                   sizeof (WINBITMAPINFOHEADER);
00060   if (BPP <= 8)   // include palette
00061     BFH.bfOffBits += (1 << BPP) * sizeof(WINRGBQUAD);
00062 
00063   BFH.bfSize = BFH.bfOffBits;
00064   BFH.bfSize += pBmp->GetHeight()*GetLineMemNeeded(pBmp->GetWidth(), BPP);
00065 
00066   pSink->WriteNBytes( sizeof (WINBITMAPFILEHEADER), (PLBYTE *) &BFH);
00067 
00068   WINBITMAPINFOHEADER FileBMI;
00069   FileBMI.biSize = sizeof(WINBITMAPINFOHEADER);
00070   FileBMI.biWidth = pBmp->GetWidth ();
00071   FileBMI.biHeight = pBmp->GetHeight ();
00072   FileBMI.biPlanes = 1;
00073   FileBMI.biCompression = BI_RGB;
00074   FileBMI.biSizeImage = 0; // may be 0 for non-compressed files
00075   FileBMI.biXPelsPerMeter = (int)((float)pBmp->GetResolution().x * 39.37f+0.5);
00076   FileBMI.biYPelsPerMeter = (int)((float)pBmp->GetResolution().y * 39.37f+0.5);
00077   FileBMI.biClrUsed = 0;
00078   FileBMI.biClrImportant = 0;
00079 
00080   int x,y;
00081   if (BPP <= 8)
00082   {
00083     PLBYTE * pCurLine;
00084     FileBMI.biBitCount = BPP;  // not 32...
00085     pSink->WriteNBytes (sizeof (WINBITMAPINFOHEADER), (PLBYTE *) &FileBMI);
00086 
00087     // Write Palette
00088     PLPixel32 * pPal = pBmp->GetPalette();
00089     for (int i=0; i<(1<<BPP); i++)
00090     {
00091       pSink->WriteByte (pPal[i].GetB());
00092       pSink->WriteByte (pPal[i].GetG());
00093       pSink->WriteByte (pPal[i].GetR());
00094       pSink->WriteByte (pPal[i].GetA());
00095     }
00096 
00097     for (y=FileBMI.biHeight-1; y>=0; y--)
00098     {
00099       pCurLine = pBmp->GetLineArray()[y];
00100       pSink->WriteNBytes (pBmp->GetBytesPerLine(), pCurLine);
00101       int PadBytes = GetLineMemNeeded(pBmp->GetWidth(), BPP) 
00102         - pBmp->GetBytesPerLine();
00103 
00104       pSink->Skip(PadBytes);
00105     }
00106   }
00107   else
00108   {
00109     PLPixel32 * pCurLine;
00110     FileBMI.biBitCount = 24;  // not 32...
00111     pSink->WriteNBytes (sizeof (WINBITMAPINFOHEADER),(PLBYTE *) &FileBMI);
00112 
00113     int LinePadding = 4-((FileBMI.biWidth*3)&3);
00114     if (LinePadding == 4)
00115       LinePadding = 0;
00116     for (y=FileBMI.biHeight-1; y>=0; y--)
00117     {
00118       pCurLine = pBmp->GetLineArray32()[y];
00119       for (x=0; x<FileBMI.biWidth; x++)
00120       {
00121         pSink->WriteByte (pCurLine[x].GetB());
00122         pSink->WriteByte (pCurLine[x].GetG());        
00123         pSink->WriteByte (pCurLine[x].GetR());
00124       }
00125       pSink->WriteNBytes(LinePadding,(PLBYTE *) " ");
00126     }
00127   }
00128 
00129 }
00130 
00131 /*
00132 /--------------------------------------------------------------------
00133 |
00134 |      $Log: plbmpenc.cpp,v $
00135 |      Revision 1.1  2004/05/21 21:02:52  maxx
00136 |      Initial Version of vuVolume, moderatly changed to make it compile on my windows and linux machine.
00137 |
00138 |      Revision 1.1  2002/11/13 01:58:20  mspindle
00139 |      *** empty log message ***
00140 |
00141 |      Revision 1.6  2001/10/21 17:12:39  uzadow
00142 |      Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel.
00143 |
00144 |      Revision 1.5  2001/10/16 17:12:26  uzadow
00145 |      Added support for resolution information (Luca Piergentili)
00146 |
00147 |      Revision 1.4  2001/10/06 22:37:08  uzadow
00148 |      Linux compatibility.
00149 |
00150 |      Revision 1.3  2001/10/06 22:03:26  uzadow
00151 |      Added PL prefix to basic data types.
00152 |
00153 |      Revision 1.2  2001/10/06 20:44:45  uzadow
00154 |      Linux compatibility
00155 |
00156 |      Revision 1.1  2001/09/16 19:03:22  uzadow
00157 |      Added global name prefix PL, changed most filenames.
00158 |
00159 |      Revision 1.8  2001/02/04 14:31:52  uzadow
00160 |      Member initialization list cleanup (Erik Hoffmann).
00161 |
00162 |      Revision 1.7  2000/12/02 19:52:12  uzadow
00163 |      no message
00164 |
00165 |      Revision 1.6  2000/12/02 19:50:01  uzadow
00166 |      Added Logging.
00167 |
00168 |    31.10.2000 - José Miguel Buenaposada
00169 |                 Corrected the order for writing color channels
00170 |                 to work with other channels orders.
00171 |
00172 |    27.08.2000 - Martin Skinner
00173 |                 now uses PLDataSink instead of MFC CFile
00174 |                 moved to a new class PLBmpEncoder
00175 |
00176 |    11.01.2000 - Michael Salzlechner
00177 |                 moved code from PLWinBmp to CAfxBmp
00178 |
00179 |    21.01.99 - Added by M.Skinner
00180 |               Moved program logic from 
00181 |               PLWinBmp::SaveAsBmp(const char *) to here
00182 |
00183 \--------------------------------------------------------------------
00184 */

Generated on Wed Dec 15 21:20:30 2004 for vuVolume by  doxygen 1.3.9.1