BMPWriter.cpp

Go to the documentation of this file.
00001 #include "MercuryFiles.h"
00002 #include "BMPWriter.h"
00003 
00004 void WriteBMP(const MString& path, const RawImageData& image)
00005 {
00006     BMPHeader header;
00007     BMPInfoHeader info;
00008     char buffer[3];
00009 
00010     info.imagesize = image.attrs.m_height * image.attrs.m_width * 3;
00011     unsigned int realSize = image.attrs.m_height * image.attrs.m_width * ColorBytesToSize(image.attrs.m_ColorByteType);
00012 
00013     header.offset = 54;
00014     memcpy(header.ID, "BM", 2);
00015     header.fileSize = 54 + info.imagesize;
00016     header.reserved = 0;
00017 
00018     info.bbp = 24; //RGB
00019     info.compression = 0;
00020     info.headerSize = 40;
00021     info.height = image.attrs.m_height;
00022     info.width = image.attrs.m_width;
00023     info.importantColor = 0;
00024     info.numColor = 0;
00025     info.planes = 1;
00026     info.resx = (int)(image.attrs.m_dpi_x * 39.3700787);
00027     info.resy = (int)(image.attrs.m_dpi_y * 39.3700787);
00028 
00029     MercuryFile* f = FILEMAN.Open(path, MFP_WRITE_ONLY);
00030 
00031     //Manually write all this stuff because of padding
00032     f->Write(header.ID, 2);
00033     f->Write(&header.fileSize, 4);
00034     f->Write(&header.reserved, 4);
00035     f->Write(&header.offset, 4);
00036 //  f->Write(&header, 14);
00037     f->Write(&info, 40);
00038 
00039     //RGB -> BGR
00040     if (image.attrs.m_ColorByteType == RGB)
00041         for (unsigned int x = 0; x < realSize; x += 3)
00042         {
00043             buffer[2] = image.data[x];
00044             buffer[1] = image.data[x+1];
00045             buffer[0] = image.data[x+2];
00046             f->Write(buffer, 3);
00047         }
00048     else if (image.attrs.m_ColorByteType == RGBA)
00049         for (unsigned int x = 0; x < realSize; x += 4)
00050         {
00051             buffer[2] = image.data[x];
00052             buffer[1] = image.data[x+1];
00053             buffer[0] = image.data[x+2];
00054             f->Write(buffer, 3);
00055         }
00056 
00057     SAFE_DELETE(f);
00058 }
00059 
00060 /* 
00061  * Copyright (c) 2006 Joshua Allen
00062  * All rights reserved.
00063  *
00064  * Redistribution and use in source and binary forms, with or
00065  * without modification, are permitted provided that the following
00066  * conditions are met:
00067  *  -   Redistributions of source code must retain the above
00068  *      copyright notice, this list of conditions and the following disclaimer.
00069  *  -   Redistributions in binary form must reproduce the above copyright
00070  *      notice, this list of conditions and the following disclaimer in
00071  *      the documentation and/or other materials provided with the distribution.
00072  *  -   Neither the name of the Mercury Engine nor the names of its
00073  *      contributors may be used to endorse or promote products derived from
00074  *      this software without specific prior written permission.
00075  *
00076  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00077  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00078  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00079  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00080  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00081  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00082  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00083  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00084  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00085  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00086  */

Hosted by SourceForge.net Logo