Liyou Zhou / Mbed 2 deprecated light_painting_strip

Dependencies:   NeoStrip PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BitmapFile.cpp Source File

BitmapFile.cpp

00001 #include "BitmapFile.h"
00002 
00003 
00004 BitmapFile::BitmapFile(char* fname) : m_pFile(NULL)
00005 {
00006     m_fileName = fname;
00007     Initialize();
00008 }
00009 
00010 BitmapFile::~BitmapFile()
00011 {
00012     delete[] m_fileName;
00013 }
00014 
00015 bool BitmapFile::Initialize()
00016 {
00017     bool success = true;
00018     open();
00019     fread(&BMPHeader,sizeof(BMPHeader),1,m_pFile);
00020     success = (BMPHeader.b == 'B' && BMPHeader.m == 'M');
00021     
00022     fread(&m_headerlength,sizeof(m_headerlength),1,m_pFile);
00023     fread(&DIBHeader,m_headerlength,1,m_pFile);
00024     
00025     /*Debugging code*/
00026     
00027     Serial pc2(USBTX,USBRX);
00028     
00029     pc2.printf("\n\rFile = %s", m_fileName);
00030     
00031     pc2.printf("\n\rBMPHeader - Size = %d:\n\r",sizeof(BMPHeader));
00032     pc2.printf("\tbm:\t\t%c%c\n\r",BMPHeader.b,BMPHeader.m);
00033     pc2.printf("\tfilesize:\t%d\n\r",BMPHeader.filesize);
00034     pc2.printf("\treserved:\t%d,%d\n\r",BMPHeader.reserved1,BMPHeader.reserved2);
00035     pc2.printf("\toffset:\t\t%d\n\r",BMPHeader.offset);
00036     
00037     pc2.printf("\n\rDIBHeader - Size = %d:\n\r",sizeof(DIBHeader));
00038     //pc2.printf("\theaderLength:\t%d\n\r",DIBHeader.headerLength);
00039     pc2.printf("\theight:\t\t\t%d\n\r",DIBHeader.height);
00040     pc2.printf("\twidth:\t\t%d\n\r",DIBHeader.width);
00041     pc2.printf("\tcplanes:\t\t%d\n\r",DIBHeader.cplanes);
00042     pc2.printf("\tcolordepth:\t\t%d\n\r",DIBHeader.colordepth);
00043     pc2.printf("\tcompression:\t%d\n\r",DIBHeader.compression);
00044     pc2.printf("\tdatasize:\t\t%d\n\r",DIBHeader.datasize);
00045     pc2.printf("\tvres:\t\t%d\n\r",DIBHeader.vres);
00046     pc2.printf("\thres:\t\t%d\n\r",DIBHeader.hres);
00047     
00048     
00049     
00050     m_rowsize = 4*((getColorDepth()*getWidth()+31)/32);
00051     
00052     close();
00053     return success;
00054 }
00055 
00056 void BitmapFile::open()
00057 {
00058     if(m_pFile==NULL)
00059     {
00060         m_pFile = fopen(m_fileName, "r");
00061     }
00062 }
00063 
00064 void BitmapFile::close()
00065 {
00066     if(m_pFile!=NULL)
00067     {
00068         fclose(m_pFile);
00069         m_pFile = NULL;
00070     }
00071 }
00072 
00073 /**********************************************************/
00074 /*BMP Header Gets                                         */
00075 /**********************************************************/
00076 
00077 int BitmapFile::getFileSize()
00078 {
00079     return BMPHeader.filesize;
00080 }
00081 
00082 int BitmapFile::getReserved1()
00083 {
00084     return BMPHeader.reserved1;
00085 }
00086 
00087 int BitmapFile::getReserved2()
00088 {
00089     return BMPHeader.reserved2;
00090 }
00091 
00092 int BitmapFile::getOffset()
00093 {
00094     return BMPHeader.offset;
00095 }
00096 
00097 /**********************************************************/
00098 /*DIB Header Gets                                         */
00099 /**********************************************************/
00100 
00101 int BitmapFile::getHeaderType()
00102 {
00103     return m_headerlength;
00104 }
00105 
00106 int BitmapFile::getHeight()
00107 {
00108     return DIBHeader.height;
00109 }
00110 
00111 int BitmapFile::getWidth()
00112 {
00113     return DIBHeader.width;
00114 }
00115 
00116 int BitmapFile::getCPlanes()
00117 {
00118     return DIBHeader.cplanes;
00119 }
00120 
00121 int BitmapFile::getColorDepth()
00122 {
00123     return DIBHeader.colordepth;
00124 }
00125 
00126 int BitmapFile::getCompression()
00127 {
00128     return DIBHeader.compression;
00129 }
00130 
00131 int BitmapFile::getDataSize()
00132 {
00133     return DIBHeader.datasize;
00134 }
00135 
00136 int BitmapFile::getHRes()
00137 {
00138     return DIBHeader.hres;
00139 }
00140 
00141 int BitmapFile::getVRes()
00142 {
00143     return DIBHeader.vres;
00144 }
00145 
00146 int BitmapFile::getNumPaletteColors()
00147 {
00148     return DIBHeader.numpalettecolors;
00149 }
00150 
00151 int BitmapFile::getImportantColors()
00152 {
00153     return DIBHeader.importantcolors;
00154 }
00155 
00156 /**********************************************************/
00157 /*Data Gets                                               */
00158 /**********************************************************/
00159 
00160 int BitmapFile::getRowSize()
00161 {
00162     return m_rowsize;
00163 }
00164 
00165 int BitmapFile::getPixel(int row, int col, bool closefile)
00166 {
00167     int color = -1;
00168     if(row>=0 && row < getHeight() && col>=0 && col< getWidth())
00169     {
00170         if(getColorDepth() == 24)
00171         {
00172             open();
00173             color = 0;  //make sure the last byte is 00
00174             
00175             int index = getOffset();
00176             index += col*3;
00177             index += row*4*ceil(getWidth()*3/4.0);
00178             fseek(m_pFile, index, SEEK_SET);
00179             
00180             fread (&color, 3,1,m_pFile);
00181             
00182             if(closefile)
00183             {
00184                 close();
00185             }
00186         }
00187     }
00188     return color;
00189 }
00190 
00191 int *BitmapFile::getRow(int row, bool closefile)
00192 {
00193     open();
00194     int *colors = new int[getWidth()];
00195     int index = getOffset() + m_rowsize*row;
00196     fseek(m_pFile, index, SEEK_SET);
00197     if(getColorDepth() == 24)
00198     {
00199         for(int i=0; i<getWidth(); i++)
00200         {
00201             fread(&colors[i],3,1,m_pFile);
00202         }
00203     }
00204     else if(getColorDepth() == 1)
00205     {
00206         char *temp = new char[m_rowsize];
00207         for(int i=0; i<m_rowsize; i++)
00208         {
00209             fread(&temp[i],sizeof(char),1,m_pFile);
00210         }
00211         for(int i=0; i<getWidth(); i++)
00212         {
00213             int byte = i / 8;
00214             int bit = i % 8;    
00215             colors[i] = ((temp[byte] << bit) & 0x80) ? 0xFFFFFF : 0x000000;
00216         }
00217         delete [] temp;
00218     }
00219     if(closefile)
00220     {
00221         close();
00222     }
00223     return colors;
00224 }
00225 
00226 int *BitmapFile::getRowBW(int row, bool closefile)
00227 {
00228     open();
00229     int *colors = new int[getWidth()];
00230     int index = getOffset() + m_rowsize*row;
00231     fseek(m_pFile, index, SEEK_SET);
00232     if(getColorDepth() == 24)
00233     {
00234         for(int i=0; i<getWidth(); i++)
00235         {
00236             char temp[3];
00237             fread(temp,sizeof(char),3,m_pFile);
00238             int average = (temp[0]+temp[1]+temp[2])/3;
00239             colors[i] = average>128 ? 0xFFFFFF : 0x000000;
00240         }
00241     }
00242     else if(getColorDepth() == 1)
00243     {
00244         delete [] colors;
00245         colors = getRow(row, closefile);
00246     }
00247     if(closefile)
00248     {
00249         close();
00250     }
00251     return colors;
00252 }
00253 
00254 char *BitmapFile::getRowBitstream(int row, bool closefile)
00255 {
00256     open();
00257     int bitsperrow = (getWidth()+7)/8;
00258     char *data = new char[bitsperrow];
00259     for(int i = 0; i<bitsperrow; i++)
00260     {
00261         data[i] = 0;
00262     }
00263     int index = getOffset() + m_rowsize*row;
00264     fseek(m_pFile, index, SEEK_SET);
00265     
00266     if(getColorDepth() == 24)
00267     {
00268         for(int i=0; i<getWidth(); i++)
00269         {
00270             char temp[3];
00271             fread(temp,sizeof(char),3,m_pFile);
00272             int average = (temp[0]+temp[1]+temp[2])/3;
00273             int val = average<128?0:1;
00274             data[i/8] |= (val*0x80) >> (i%8);
00275         }
00276     }
00277     else if(getColorDepth() == 1)
00278     {
00279         fread(data,sizeof(char),bitsperrow,m_pFile);
00280     }
00281     
00282     if(closefile)
00283     {
00284         close();
00285     }
00286     return data;
00287 }