321 |
321 |
strm.putDWord( value );
|
322 |
322 |
}
|
323 |
323 |
|
|
324 |
#ifdef HAVE_TIFF
|
324 |
325 |
|
325 |
|
bool TiffEncoder::write( const Mat& img, const vector<int>& )
|
|
326 |
#include "tiff.h"
|
|
327 |
#include "tiffio.h"
|
|
328 |
|
|
329 |
bool TiffEncoder::writeLibTiff( const Mat& img, const vector<int>& /*params*/)
|
326 |
330 |
{
|
327 |
331 |
int channels = img.channels();
|
328 |
332 |
int width = img.cols, height = img.rows;
|
329 |
333 |
int depth = img.depth();
|
330 |
334 |
|
|
335 |
int bitsPerChannel = -1;
|
|
336 |
switch (depth)
|
|
337 |
{
|
|
338 |
case CV_8U:
|
|
339 |
{
|
|
340 |
bitsPerChannel = 8;
|
|
341 |
break;
|
|
342 |
}
|
|
343 |
case CV_16U:
|
|
344 |
{
|
|
345 |
bitsPerChannel = 16;
|
|
346 |
break;
|
|
347 |
}
|
|
348 |
default:
|
|
349 |
{
|
|
350 |
return false;
|
|
351 |
}
|
|
352 |
}
|
|
353 |
|
|
354 |
// do NOT put "wb" as the mode, because the b means "big endian" mode, not "binary" mode.
|
|
355 |
// http://www.remotesensing.org/libtiff/man/TIFFOpen.3tiff.html
|
|
356 |
TIFF* pTiffHandle = TIFFOpen(m_filename.c_str(), "w");
|
|
357 |
if (!pTiffHandle)
|
|
358 |
{
|
|
359 |
return false;
|
|
360 |
}
|
|
361 |
|
|
362 |
// defaults for now, maybe base them on params in the future
|
|
363 |
int compression = COMPRESSION_LZW;
|
|
364 |
int predictor = PREDICTOR_HORIZONTAL;
|
|
365 |
|
|
366 |
float xresolution = 120.0;
|
|
367 |
float yresolution = 120.0;
|
|
368 |
int rowsperstrip = 100;
|
|
369 |
|
|
370 |
if ( !TIFFSetField(pTiffHandle, TIFFTAG_SUBFILETYPE, 0)
|
|
371 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_IMAGEWIDTH, width)
|
|
372 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_IMAGELENGTH, height)
|
|
373 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, bitsPerChannel)
|
|
374 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_COMPRESSION, compression)
|
|
375 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
|
|
376 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, channels)
|
|
377 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_XRESOLUTION, xresolution)
|
|
378 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_YRESOLUTION, yresolution)
|
|
379 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)
|
|
380 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH)
|
|
381 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_ROWSPERSTRIP, rowsperstrip)
|
|
382 |
|| !TIFFSetField(pTiffHandle, TIFFTAG_PREDICTOR, predictor)
|
|
383 |
)
|
|
384 |
{
|
|
385 |
TIFFClose(pTiffHandle);
|
|
386 |
return false;
|
|
387 |
}
|
|
388 |
|
|
389 |
// row buffer, because TIFFWriteScanline modifies the original data!
|
|
390 |
size_t scanlineSize = TIFFScanlineSize(pTiffHandle);
|
|
391 |
unsigned char* scanline = (unsigned char*) _TIFFmalloc(scanlineSize);
|
|
392 |
if (!scanline)
|
|
393 |
{
|
|
394 |
TIFFClose(pTiffHandle);
|
|
395 |
return false;
|
|
396 |
}
|
|
397 |
|
|
398 |
for (int y = 0; y < height; ++y)
|
|
399 |
{
|
|
400 |
memcpy(scanline, img.data + img.step * y, scanlineSize);
|
|
401 |
int writeResult = TIFFWriteScanline(pTiffHandle, scanline, y, 0);
|
|
402 |
if (writeResult != 1)
|
|
403 |
{
|
|
404 |
TIFFClose(pTiffHandle);
|
|
405 |
return false;
|
|
406 |
}
|
|
407 |
}
|
|
408 |
|
|
409 |
TIFFClose(pTiffHandle);
|
|
410 |
return true;
|
|
411 |
}
|
|
412 |
|
|
413 |
#endif
|
|
414 |
|
|
415 |
#ifdef HAVE_TIFF
|
|
416 |
bool TiffEncoder::write( const Mat& img, const vector<int>& params)
|
|
417 |
#else
|
|
418 |
bool TiffEncoder::write( const Mat& img, const vector<int>& /*params*/)
|
|
419 |
#endif
|
|
420 |
{
|
|
421 |
int channels = img.channels();
|
|
422 |
int width = img.cols, height = img.rows;
|
|
423 |
int depth = img.depth();
|
|
424 |
|
331 |
425 |
if (depth != CV_8U && depth != CV_16U)
|
332 |
426 |
return false;
|
333 |
427 |
|
... | ... | |
341 |
435 |
if( !strm.open(*m_buf) )
|
342 |
436 |
return false;
|
343 |
437 |
}
|
344 |
|
else if( !strm.open(m_filename) )
|
345 |
|
return false;
|
|
438 |
else
|
|
439 |
{
|
|
440 |
#ifdef HAVE_TIFF
|
|
441 |
return writeLibTiff(img, params);
|
|
442 |
#else
|
|
443 |
if( !strm.open(m_filename) )
|
|
444 |
return false;
|
|
445 |
#endif
|
|
446 |
}
|
346 |
447 |
|
347 |
448 |
int rowsPerStrip = (1 << 13)/fileStep;
|
348 |
449 |
|