mirror of http://192.168.1.51:8099/lmh188/twain3.0
109 lines
3.1 KiB
C++
109 lines
3.1 KiB
C++
#include "stdafx.h"
|
||
#include "G4Tiff.h"
|
||
#include <io.h>
|
||
|
||
G4Tiff::G4Tiff(cv::Mat & mat, int threshold)
|
||
{
|
||
m_tmppath = cv::tempfile(".tif");
|
||
if (mat.channels() != 1)
|
||
throw std::runtime_error("mat channel error");
|
||
TIFF* pTiffHandle = TIFFOpen(m_tmppath.c_str(), "w");
|
||
if (!pTiffHandle)
|
||
{
|
||
printf("can't open TIFF descriptor\n");
|
||
}
|
||
int width = mat.cols;
|
||
int height = mat.rows;
|
||
try
|
||
{
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_IMAGEWIDTH, width), "width");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_IMAGELENGTH, height), "length");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, 1), "bits per sample");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, 1), "samples per pixel");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_ROWSPERSTRIP, height), "rows per strip");
|
||
|
||
//except(TIFFSetField(pTiffHandle, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4), "compression");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_COMPRESSION, COMPRESSION_CCITT_T6), "compression");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE), "photometric");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB), "photometric");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG), "planar config");
|
||
|
||
// not necessary
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_XRESOLUTION, 200.0), "res x");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_YRESOLUTION, 200.0), "res y");
|
||
except(TIFFSetField(pTiffHandle, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH), "res unit");
|
||
|
||
std::vector<uchar> _buffer(width / 8 + 8, 0);
|
||
//std::vector<uchar> _buffer(width / 8 + 1, 0);
|
||
uchar* buffer = &_buffer[0];
|
||
//int bytes = int(width / 8.0 + 0.5);
|
||
for (int y = 0; y < height; ++y)
|
||
{
|
||
uint8_t* src_row = mat.ptr(y);
|
||
for (int x = 0; x < width; ++x, ++src_row)
|
||
{
|
||
uint8_t eight_pixels = buffer[x / 8];
|
||
eight_pixels = eight_pixels << 1;
|
||
if (*src_row < threshold)
|
||
eight_pixels = eight_pixels | 1; //
|
||
buffer[x / 8] = eight_pixels;
|
||
}
|
||
except(TIFFWriteScanline(pTiffHandle, buffer, y, 0) != -1, "write scanline");
|
||
}
|
||
|
||
}
|
||
catch (const std::runtime_error& e)
|
||
{
|
||
printf("TIFF writing: %s\n", e.what());
|
||
TIFFClose(pTiffHandle);
|
||
}
|
||
|
||
TIFFClose(pTiffHandle);
|
||
}
|
||
|
||
G4Tiff::~G4Tiff()
|
||
{
|
||
if (_access(m_tmppath.c_str(), 0) == 0)
|
||
{
|
||
if (!std::remove(m_tmppath.c_str())) {}//ɾ<><C9BE><EFBFBD>ɹ<EFBFBD>
|
||
else
|
||
throw std::runtime_error("File is not exist");
|
||
}
|
||
}
|
||
|
||
void G4Tiff::GetCompressedData(std::vector<uchar>& cmpeddata)
|
||
{
|
||
if (m_tmppath.empty())
|
||
throw std::runtime_error("file is not exist");
|
||
FILE* file = fopen(m_tmppath.c_str(), "rb");
|
||
if (file)
|
||
{
|
||
|
||
int filesize = _lseek(_fileno(file), 0, SEEK_END); // seek to EOF
|
||
fseek(file, 0, SEEK_SET);
|
||
std::vector<uchar> ifh(8, 0);
|
||
fread(ifh.data(), 1, ifh.size(), file);
|
||
int size = (int)(*(int*)(ifh.data() + 4));
|
||
fseek(file, 8, SEEK_SET);
|
||
if (filesize)
|
||
{
|
||
cmpeddata.resize(size);
|
||
fread(cmpeddata.data(), 1, size, file);
|
||
}
|
||
|
||
|
||
}
|
||
fclose(file);
|
||
|
||
|
||
//TIFF* pTiffHandle = TIFFOpen(m_tmppath.c_str(), "w");
|
||
//TIFFGetField()
|
||
//TIFFClose(pTiffHandle);
|
||
}
|
||
|
||
void G4Tiff::except(bool condition, const std::string & message)
|
||
{
|
||
if (!condition)
|
||
throw std::runtime_error("Error " + message);
|
||
}
|