00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "plstdpch.h"
00012 #include "plbmpenc.h"
00013 #include "plwindefs.h"
00014 #include "plexcept.h"
00015
00016
00018
00019
00020 PLBmpEncoder::PLBmpEncoder
00021 () : PLPicEncoder()
00022
00023 {
00024
00025 }
00026
00027
00028 PLBmpEncoder::~PLBmpEncoder
00029 ()
00030 {
00031
00032 }
00033
00034 int PLBmpEncoder::GetLineMemNeeded (PLLONG width, PLWORD BitsPerPixel)
00035 {
00036
00037 int LineMem = width * BitsPerPixel / 8;
00038
00039 if (BitsPerPixel == 1 && width % 8)
00040 ++LineMem;
00041
00042
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)
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;
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;
00085 pSink->WriteNBytes (sizeof (WINBITMAPINFOHEADER), (PLBYTE *) &FileBMI);
00086
00087
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;
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
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184