www.pudn.com > Estereo.rar > imageio.cpp
/***************************************************************************
*
* Copyright 2004 by the Massachusetts Institute of Technology. All
* rights reserved.
*
* Developed by David Demirdjian
* at the Computer Sciences and Artificial Intelligence Laboratory,
* MIT, Cambridge, Massachusetts.
*
* Permission to use, copy, or modify this software and its documentation
* for educational and research purposes only and without fee is hereby
* granted, provided that this copyright notice and the original authors's
* names appear on all copies and supporting documentation. If individual
* files are separated from this distribution directory structure, this
* copyright notice must be included. For any other uses of this software,
* in original or modified form, including but not limited to distribution
* in whole or in part, specific prior permission must be obtained from
* MIT. These programs shall not be used, rewritten, or adapted as the
* basis of a commercial software or hardware product without first
* obtaining appropriate licenses from MIT. MIT. makes no representations
* about the suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*
**************************************************************************/
#include "imageio.h"
// save PGM image
int savePGM(char* pFilename, char* imData, int width, int height)
{
ofstream fileOutput(pFilename,ios::binary);
if (!fileOutput.is_open()) return 0;
// write data
fileOutput << "P5" << endl;
fileOutput << width << " " << height << endl << 255 << endl;
fileOutput.write(imData, width*height);
fileOutput.close();
return 1;
}
// load PPM image
int loadPPM(char* pFilename, char*& imData_orig, char*& imData,
int& iWidth, int& iHeight)
{
ifstream fileInput(pFilename,ios::binary);
char pChar, tmpLine[120];
int iNbChannels = 1;
int iNbGreyLevel, iPixelDepth;
if (!fileInput.is_open())
return 0;
fileInput >> pChar;
fileInput >> pChar;
switch(pChar)
{
case '5':
iNbChannels = 1;
iPixelDepth = 1;
break;
case '6':
iNbChannels = 3;
iPixelDepth = 1;
break;
case '3':
iNbChannels = 4;
iPixelDepth = 1;
break;
case '7':
iNbChannels = 1;
iPixelDepth = 4;
break;
case '9':
iNbChannels = 1;
iPixelDepth = 2;
break;
default:
return 0;
}
fileInput.getline(tmpLine,120);
fileInput.getline(tmpLine,120);
if (tmpLine[0]=='#')
fileInput >> iWidth >> iHeight >> iNbGreyLevel;
else {
iWidth = atoi(tmpLine);
iHeight = atoi(strchr(tmpLine,' ')+1);
fileInput >> iNbGreyLevel;
}
fileInput.getline(tmpLine,120);
// allocate (aligned) memory
int datasize = iWidth * iHeight * iPixelDepth * iNbChannels;
ALLOC_ALIGN_MEMORY(imData, imData_orig, char, datasize);
// read image content
fileInput.read(imData, datasize);
fileInput.close();
return 1;
}