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