Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
e280ae
#ifdef _MSC_VER
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiio_jpg_util.h"
Toshihiro Shimizu 890ddd
//#include "tiio.h"
Toshihiro Shimizu 890ddd
#include "tiio_jpg.h"
Toshihiro Shimizu 890ddd
#include "tproperty.h"
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Tiio::createJpg(std::vector<uchar> &buffer, const TRaster32P &ras,</uchar>
Shinya Kitaoka 120a6e
                     int quality) {
Shinya Kitaoka 120a6e
  // FILE *chan = tmpfile();
Shinya Kitaoka 120a6e
  TFilePath fname = TSystem::getUniqueFile();
Shinya Kitaoka 120a6e
  FILE *chan      = fopen(fname, "w+b");
Shinya Kitaoka 120a6e
  if (chan == 0) throw TException(L". Can not create " + fname.getWideString());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(chan);
Shinya Kitaoka 120a6e
  assert(ras);
Shinya Kitaoka 120a6e
  assert(0 <= quality && quality <= 100);
Shinya Kitaoka 120a6e
  fflush(chan);
Shinya Kitaoka 120a6e
  Tiio::Writer *writer = Tiio::makeJpgWriter();
Shinya Kitaoka 120a6e
  assert(writer);
Shinya Kitaoka 120a6e
  TPropertyGroup *pg = writer->getProperties();
Shinya Kitaoka 120a6e
  if (!pg) {
Shinya Kitaoka 120a6e
    writer->setProperties(new JpgWriterProperties());
Shinya Kitaoka 120a6e
    pg = writer->getProperties();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TProperty *qualityProp = pg->getProperty(JpgWriterProperties::QUALITY);
Shinya Kitaoka 120a6e
  assert(qualityProp);
Shinya Kitaoka 120a6e
  TIntProperty *ip = dynamic_cast<tintproperty *="">(qualityProp);</tintproperty>
Shinya Kitaoka 120a6e
  assert(ip);
Shinya Kitaoka 120a6e
  ip->setValue(quality);
Shinya Kitaoka 120a6e
  int lx = ras->getLx();
Shinya Kitaoka 120a6e
  int ly = ras->getLy();
Shinya Kitaoka 120a6e
  assert(lx > 0 && ly > 0);
Shinya Kitaoka 120a6e
  writer->open(chan, TImageInfo(lx, ly));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  ras->lock();
Shinya Kitaoka 120a6e
  for (int y = 0; y < ly; y++)
Shinya Kitaoka 120a6e
    writer->writeLine((char *)ras->getRawData(0, ly - 1 - y));
Shinya Kitaoka 120a6e
  ras->unlock();
Shinya Kitaoka 120a6e
  writer->flush();
Shinya Kitaoka 120a6e
  delete writer;
Shinya Kitaoka 120a6e
  fclose(chan);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // lo chiudo e lo riapro: altrimenti, gettava male  la filesize. boh.
Shinya Kitaoka 120a6e
  FILE *chan1 = fopen(fname, "rb");
Shinya Kitaoka 120a6e
  if (chan1 == 0)
Shinya Kitaoka 120a6e
    throw TException(L". Can not create " + fname.getWideString());
Shinya Kitaoka 120a6e
  int ret = fseek(chan1, 0, SEEK_END);
Shinya Kitaoka 120a6e
  assert(ret == 0);
Shinya Kitaoka 120a6e
  int fileSize = ftell(chan1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  buffer.resize(fileSize);
Shinya Kitaoka 120a6e
  ret = fseek(chan1, 0, SEEK_SET);
Shinya Kitaoka 120a6e
  assert(ret == 0);
Shinya Kitaoka 120a6e
  for (int i = 0; i < fileSize; i++) {
Shinya Kitaoka 120a6e
    int c = fgetc(chan1);
Shinya Kitaoka 120a6e
    assert(!feof(chan1));
Shinya Kitaoka 120a6e
    buffer[i] = c;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  fclose(chan1);
Shinya Kitaoka 120a6e
  TSystem::deleteFile(fname);
Toshihiro Shimizu 890ddd
}