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