The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

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

imageio.cpp

Go to the documentation of this file.
00001 /*
00002  * imageio.cpp
00003  *
00004  * Copyright (c) 2003 Machine Perception Laboratory
00005  * University of California San Diego.
00006  * Please read the disclaimer and notes about redistribution
00007  * at the end of this file.
00008  *
00009  * Authors: Josh Susskind, Bret Fortenberry
00010  */
00011 
00012 #ifdef WIN32
00013 #include <windows.h>
00014 #endif
00015 #include "imageio.h"
00016 #include <fstream>
00017 
00018 #include "../common.h"
00019 
00020 using namespace std;
00021 
00022 // ================================================================
00023 
00024 // This is the implementation function that writes the captured video
00025 // data onto a bitmap on the user's disk.
00026 //
00027 int MPImageIO::WriteBitmap(unsigned char * pBuffer, long lBufferSize, int lWidth, int lHeight, char *m_szSnappedName, int bitcount)
00028 {
00029         int extrabytes = (4 - lWidth % 4) % 4;
00030         const int size = ((lWidth+extrabytes)*lHeight)*(bitcount/8);
00031         unsigned char * tempbuffer;
00032         int pos = 0;
00033 
00034         /*if width is not multple of four then extra bytes need to be added*/
00035         if(extrabytes){
00036                 int pos = 0;
00037                 tempbuffer = new BYTE[size];
00038                 for(int y=0;y<lHeight;y++){
00039                         for(int x =0; x< lWidth;x++)
00040                         {
00041                                 tempbuffer[pos] = pBuffer[x+y*lWidth];
00042                                 pos++;
00043                         }
00044                         for(x = 0; x < extrabytes*(bitcount/8); x++)
00045                         {
00046                                 tempbuffer[pos] = 0;
00047                                 pos++;
00048                         }
00049                 }
00050                 lBufferSize = pos;
00051         }
00052 
00053         BITMAPFILEHEADER bfh;
00054         BITMAPINFOHEADER bih;
00055         ofstream os(m_szSnappedName, ios::binary);
00056         memset( &bfh, 0, sizeof( bfh ) );
00057         bfh.bfType = 'MB';
00058         bfh.bfSize = sizeof( bfh ) + lBufferSize + sizeof( BITMAPINFOHEADER );
00059         if(bitcount == 24)
00060                 bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );
00061         else if(bitcount == 8)
00062                 bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER ) + 1024;
00063         //bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );
00064         os.write((char*)&bfh, sizeof(bfh)); 
00065         //os.flush();
00066         memset( &bih, 0, sizeof( bih ) );
00067         bih.biSize = sizeof( bih );
00068         bih.biWidth = lWidth;
00069         bih.biHeight = lHeight;
00070         bih.biPlanes = 1;
00071         bih.biBitCount = bitcount;
00072         os.write((char*)&bih, sizeof(bih));
00073         //os.flush();
00074         /*adds header buffer needed for 8 bit*/
00075         if(bitcount == 8)
00076         {
00077                 unsigned char update = 0;
00078                 unsigned char buffArray[1024];
00079                 int i = 0;
00080                 while(i < 1024)
00081                 {
00082                         for(int n = 0; n < 3; n++,i++)
00083                                 buffArray[i] = update;
00084                         update++;
00085                         buffArray[i] = 0;
00086                         i++;
00087                 }
00088                 os.write((char*)buffArray, 1024);
00089         }
00090         /*adds extrabyte buffer for every row*/
00091         if(extrabytes){
00092                 os.write((char*)tempbuffer, sizeof(char)*lBufferSize);
00093                 delete [] tempbuffer;
00094         }
00095         else
00096                 os.write((char*)pBuffer, sizeof(char)*lBufferSize);
00097         //os.flush();
00098         os.close();
00099         return 0;
00100 }
00101 
00102 // ================================================================
00103 
00104 int MPImageIO::WriteBitmap(float * float_pBuffer, long lBufferSize, int lWidth, int lHeight, char *m_szSnappedName, int bitcount, bool flipped)
00105 {
00106         int extrabytes = (4 - lWidth % 4) % 4;
00107         const int size = ((lWidth+extrabytes)*lHeight)*(bitcount/8);
00108         const int imSize = (lWidth*lHeight)*(bitcount/8);
00109         BYTE * tempbuffer;
00110         BYTE * pBuffer = new BYTE[imSize];
00111         BYTE *temp;
00112         float *ftemp;
00113         int pos = 0;
00114 
00115         if(flipped) temp = &pBuffer[0];
00116         else temp = &pBuffer[imSize-1];
00117         ftemp = float_pBuffer;
00118         // switch from float to unsigned char
00119         for(int height = 0; height < lHeight; height++) { 
00120                 int y = height;
00121                 if(!flipped) y = lHeight-y;
00122                 for (int width = 0; width < lWidth; width++) {
00123                         for(int bitSize = 0; bitSize < bitcount/8; bitSize++){
00124                                 if (flipped) *temp++ = static_cast<unsigned char>(*ftemp++);
00125                                 else *temp-- = static_cast<unsigned char>(*ftemp++);
00126                         }
00127                 }
00128         }
00129 
00130         /*if width is not multple of four then extra bytes need to be added*/
00131         if(extrabytes){
00132                 int pos = 0;
00133                 tempbuffer = new BYTE[size];
00134                 for(int y=0;y<lHeight;y++){
00135                         for(int x =0; x< lWidth;x++)
00136                         {
00137                                 tempbuffer[pos] = pBuffer[x+y*lWidth];
00138                                 pos++;
00139                         }
00140                         for(x = 0; x < extrabytes*(bitcount/8); x++)
00141                         {
00142                                 tempbuffer[pos] = 0;
00143                                 pos++;
00144                         }
00145                 }
00146                 lBufferSize = pos;
00147         }
00148 
00149         BITMAPFILEHEADER bfh;
00150         BITMAPINFOHEADER bih;
00151         ofstream os(m_szSnappedName, ios::binary);
00152         memset( &bfh, 0, sizeof( bfh ) );
00153         bfh.bfType = 'MB';
00154         bfh.bfSize = sizeof( bfh ) + lBufferSize + sizeof( BITMAPINFOHEADER );
00155         if(bitcount == 24)
00156                 bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );
00157         else if(bitcount == 8)
00158                 bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER ) + 1024;
00159         //bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );
00160         os.write((char*)&bfh, sizeof(bfh)); 
00161         //os.flush();
00162         memset( &bih, 0, sizeof( bih ) );
00163         bih.biSize = sizeof( bih );
00164         bih.biWidth = lWidth;
00165         bih.biHeight = lHeight;
00166         bih.biPlanes = 1;
00167         bih.biBitCount = bitcount;
00168         os.write((char*)&bih, sizeof(bih));
00169         //os.flush();
00170         /*adds header buffer needed for 8 bit*/
00171         if(bitcount == 8)
00172         {
00173                 unsigned char update = 0;
00174                 unsigned char buffArray[1024];
00175                 int i = 0;
00176                 while(i < 1024)
00177                 {
00178                         for(int n = 0; n < 3; n++,i++)
00179                                 buffArray[i] = update;
00180                         update++;
00181                         buffArray[i] = 0;
00182                         i++;
00183                 }
00184                 os.write((char*)buffArray, 1024);
00185         }
00186         /*adds extrabyte buffer for every row*/
00187         if(extrabytes){
00188                 os.write((char*)tempbuffer, sizeof(char)*lBufferSize);
00189                 delete [] tempbuffer;
00190         }
00191         else
00192                 os.write((char*)pBuffer, sizeof(char)*lBufferSize);
00193         //os.flush();
00194         os.close();
00195         return 0;
00196 }
00197 
00198 // ================================================================
00199 
00200 BITMAPINFOHEADER MPImageIO::LoadBitmapHeader(char* inFile)
00201 {
00202         BITMAPINFOHEADER bmih;
00203         ifstream is(inFile, ios::binary);
00204         is.seekg(sizeof(BITMAPFILEHEADER), ios_base::beg );
00205         is.read((char*) &bmih, sizeof( bmih ));
00206         is.close();
00207         return(bmih);
00208 }
00209 
00210 // ================================================================
00211 
00212 void MPImageIO::LoadBitmapPixels(BYTE* bitPixels, char* inFile, BITMAPINFOHEADER &bmih)
00213 {
00214         BITMAPFILEHEADER bmfh;
00215         int remainder;
00216         unsigned char * tempPixArray;
00217 
00218         ifstream is(inFile, ios::binary);
00219         is.read((char*) &bmfh, sizeof( bmfh ));
00220         is.seekg(sizeof(bmih), ios_base::cur);
00221 
00222         remainder = (4 - bmih.biWidth % 4) % 4;
00223         int size = bmfh.bfSize-bmfh.bfOffBits;
00224         int temp = bmfh.bfOffBits - 54;
00225         tempPixArray = new BYTE[size];
00226         
00227         if (bmih.biBitCount == 24)
00228         {
00229                 is.read((char*)bitPixels, size);
00230         }
00231 
00232         if (bmih.biBitCount == 8)
00233         {
00234                 is.read((char*)tempPixArray, temp);
00235                 is.read((char*)tempPixArray, size);
00236                 
00237         }
00238         
00239         //flip image
00240         int pos =0;
00241         if(bmih.biBitCount == 8){
00242                 for(int y = bmih.biHeight - 1; y >= 0; y--){
00243                         for(int x = 0; x < bmih.biWidth; x++){
00244                                 //is only functional if rgb(24 bit)
00245                                 for(int p = 0; p < bmih.biBitCount/8; p++){
00246                                         bitPixels[pos] = tempPixArray[x+y*bmih.biWidth+p];
00247                                         pos++;
00248                                 }
00249                         }
00250                 }
00251         } 
00252         
00253         delete [] tempPixArray;
00254 
00255 }
00256 
00257 // ================================================================
00258 
00259 BITMAPINFOHEADER MPImageIO::LoadBmp(unsigned char * bitPixels, char* inFile)
00260 {
00261         BITMAPINFOHEADER bmih = LoadBitmapHeader(inFile);
00262         LoadBitmapPixels(bitPixels, inFile, bmih);
00263         return (bmih);
00264 }
00265 
00266 // ================================================================
00267 
00268 /*
00269  * 
00270  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00271  * 
00272  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00273  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00274  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00275  * 
00276  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00277  * 
00278  */
00279 
00280 

Generated on Mon Nov 8 17:07:36 2004 for MPT by  doxygen 1.3.9.1