Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
#include "tfilepath.h"
Toshihiro Shimizu 890ddd
#include "tiio_bmp.h"
Toshihiro Shimizu 890ddd
#include "filebmp.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
string buildBMPExceptionString(int retCode)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (retCode) {
Toshihiro Shimizu 890ddd
	case UNSUPPORTED_BMP_FORMAT:
Toshihiro Shimizu 890ddd
		return " : unsupported bmp format";
Toshihiro Shimizu 890ddd
	case OUT_OF_MEMORY:
Toshihiro Shimizu 890ddd
		return " : out of memory";
Toshihiro Shimizu 890ddd
	case UNEXPECTED_EOF:
Toshihiro Shimizu 890ddd
		return " : unexptected EOF";
Toshihiro Shimizu 890ddd
	case CANT_OPEN_FILE:
Toshihiro Shimizu 890ddd
		return " : can't open file";
Toshihiro Shimizu 890ddd
	case WRITE_ERROR:
Toshihiro Shimizu 890ddd
		return " : writing error";
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		return " : I/O error";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageP TImageReaderBmp::load()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TImageP image;
Toshihiro Shimizu 890ddd
	void *buff;
Toshihiro Shimizu 890ddd
	int retCode = readbmp(getFilePath().getWideString().c_str(), &m_lx, &m_ly, &buff);
Toshihiro Shimizu 890ddd
	if (retCode != OK) {
Toshihiro Shimizu 890ddd
		throw TImageException(getFilePath(), buildBMPExceptionString(retCode));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P raster;
Toshihiro Shimizu 890ddd
	raster.create(m_lx, m_ly);
Toshihiro Shimizu 890ddd
	raster->lock();
Toshihiro Shimizu 890ddd
	memcpy(raster->pixels(), buff, m_lx * m_ly * sizeof(TPixel32));
Toshihiro Shimizu 890ddd
	raster->unlock();
Toshihiro Shimizu 890ddd
	TRasterImageP rasImage(raster);
Toshihiro Shimizu 890ddd
	// image->setRaster(raster);
Toshihiro Shimizu 890ddd
	delete[] buff;
Toshihiro Shimizu 890ddd
	return TImageP(rasImage);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void TImageReaderBmp::load(const TRasterP &ras, const TPoint &pos,
Toshihiro Shimizu 890ddd
                              int shrinkX, int shrinkY)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void *buff;
Toshihiro Shimizu 890ddd
int x0 = pos.x;
Toshihiro Shimizu 890ddd
int y0 = pos.y;
Toshihiro Shimizu 890ddd
int x1 = x0 + ras->getLx() * shrinkX - 1;
Toshihiro Shimizu 890ddd
int y1 = y0 + ras->getLy() * shrinkY - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int retCode = readbmpregion(m_path.getFullPath().c_str(), &buff, x0, y0, x1, y1, shrinkX);
Toshihiro Shimizu 890ddd
if (retCode!= OK) 
Toshihiro Shimizu 890ddd
  throw TImageException(getFilePath().getFullPath(), buildBMPExceptionString(retCode));
Toshihiro Shimizu 890ddd
memcpy(ras->getRawData(), buff, ras->getLx() * ras->getLy() * 4);
Toshihiro Shimizu 890ddd
delete [] buff;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TDimension TImageReaderBmp::getSize() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TDimension d(0, 0);
Toshihiro Shimizu 890ddd
	int retCode = readbmp_size(getFilePath().getWideString().c_str(), &(d.lx), &(d.ly));
Toshihiro Shimizu 890ddd
	if (retCode != OK)
Toshihiro Shimizu 890ddd
		throw TImageException(getFilePath(), buildBMPExceptionString(retCode));
Toshihiro Shimizu 890ddd
	return d;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRect TImageReaderBmp::getBBox() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRect r;
Toshihiro Shimizu 890ddd
	int retCode = readbmp_bbox(getFilePath().getWideString().c_str(), &(r.x0), &(r.y0), &(r.x1), &(r.y1));
Toshihiro Shimizu 890ddd
	if (retCode != OK)
Toshihiro Shimizu 890ddd
		throw TImageException(getFilePath(), buildBMPExceptionString(retCode));
Toshihiro Shimizu 890ddd
	return r;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TImageWriterBmp::save(const TImageP &image)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRasterImageP rasImage(image);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P ras32;
Toshihiro Shimizu 890ddd
	ras32 = rasImage->getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterGR8P ras8;
Toshihiro Shimizu 890ddd
	ras8 = rasImage->getRaster();
Toshihiro Shimizu 890ddd
	void *buff = 0;
Toshihiro Shimizu 890ddd
	int pixSize;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!ras32 && !ras8))
Toshihiro Shimizu 890ddd
    throw TImageException(m_path, buildBMPExceptionString(UNSUPPORTED_BMP_FORMAT));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rasImage->getRaster()->lock();
Toshihiro Shimizu 890ddd
	if (ras32) {
Toshihiro Shimizu 890ddd
		buff = ras32->getRawData();
Toshihiro Shimizu 890ddd
		m_lx = ras32->getLx();
Toshihiro Shimizu 890ddd
		m_ly = ras32->getLy();
Toshihiro Shimizu 890ddd
		pixSize = 32;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		buff = ras8->getRawData();
Toshihiro Shimizu 890ddd
		m_lx = ras8->getLx();
Toshihiro Shimizu 890ddd
		m_ly = ras8->getLy();
Toshihiro Shimizu 890ddd
		pixSize = 8;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int retCode = writebmp(getFilePath().getWideString().c_str(), m_lx, m_ly, buff, pixSize);
Toshihiro Shimizu 890ddd
	rasImage->getRaster()->unlock();
Toshihiro Shimizu 890ddd
	if (retCode != OK) {
Toshihiro Shimizu 890ddd
		throw TImageException(m_path, buildBMPExceptionString(retCode));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}