1#include "./AOI_TIFFWriter.h"
2#include <ACPL/SwapStream.h>
3#include <TIFF Library/libtiff/tiffiop.h>
6using namespace aur::ACPL;
10static aur::ExceptionCode WriteTiff( TIFF*& tif, uint8_t compression, uint16_t ,
11 AOI_ColorSpaceEnum colorSpace, uint16_t bitsPerPixel, uint32_t width, uint32_t height,
12 float dpiX,
float dpiY,
const void* imageData )
22 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width );
23 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height );
24 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
25 TIFFSetField(tif, TIFFTAG_XRESOLUTION, dpiX );
26 TIFFSetField(tif, TIFFTAG_YRESOLUTION, dpiY );
27 if( colorSpace == AOI_GraySpace )
28 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, colorSpace == AOI_GraySpace && bitsPerPixel == 8 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_MINISWHITE );
29 else if( colorSpace == AOI_RGBSpace )
30 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
31 else if( colorSpace == AOI_CMYKSpace )
32 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
34 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
35 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, nrOfPlanes );
36 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitsPerPixel );
37 if( compression == TIFF_Comp_CCITT )
38 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4 );
39 else if( compression == TIFF_Comp_LZW )
40 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW );
42 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE );
43 uint32_t rowsperstrip = TIFFDefaultStripSize( tif, 128 );
44 if( rowsperstrip > height )
45 rowsperstrip = height;
46 TIFFSetField( tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip );
50 uint8_t* data = (uint8_t*)imageData;
51 uint32_t rowBytes = ( width * bitsPerPixel * nrOfPlanes + 7 ) / 8;
52 for( uint32_t row = 0; row != height; ++row )
54 if( ( err = TIFFWriteScanline( tif, data, row, 0 ) ) < 0 )
90::ExceptionCode
AOI_WriteTIFF(
const aur::ACPL::FileSpec& path, uint8_t compression, uint16_t platform,
91 AOI_ColorSpaceEnum colorSpace, uint16_t bitsPerPixel, uint32_t width, uint32_t height,
92 float dpiX,
float dpiY,
const void* imageData )
94 TIFF* tif = TIFFOpenW( path.GetPOSIXPath(),
"w" );
96 return WriteTiff( tif, compression, platform, colorSpace, bitsPerPixel, width, height, dpiX, dpiY, imageData );
99ExceptionCode
AOI_WriteTIFF(
const char16_t* path, uint8_t compression, uint16_t platform,
100 AOI_ColorSpaceEnum colorSpace, uint16_t bitsPerPixel, uint32_t width, uint32_t height,
101 float dpiX,
float dpiY,
const void* imageData )
103 TIFF* tif = TIFFOpenW( ACPL::UString( path ),
"w" );
105 return WriteTiff( tif, compression, platform, colorSpace, bitsPerPixel, width, height, dpiX, dpiY, imageData );
120AOI_TIFFWriter::AOI_TIFFWriter()
124AOI_TIFFWriter::~AOI_TIFFWriter()
129::ExceptionCode AOI_TIFFWriter::Init(
const aur::ACPL::FileSpec& path, uint8_t compression, uint16_t ,
130 AOI_ColorSpaceEnum colorSpace, uint16_t bitsPerPixel, uint32_t width, uint32_t height,
131 float dpiX,
float dpiY )
133 mTif = TIFFOpenW( path.GetPOSIXPath(),
"w" );
141 TIFFSetField(mTif, TIFFTAG_IMAGEWIDTH, width );
142 TIFFSetField(mTif, TIFFTAG_IMAGELENGTH, height );
143 TIFFSetField(mTif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
144 TIFFSetField(mTif, TIFFTAG_XRESOLUTION, dpiX );
145 TIFFSetField(mTif, TIFFTAG_YRESOLUTION, dpiY );
146 if( colorSpace == AOI_GraySpace )
147 TIFFSetField(mTif, TIFFTAG_PHOTOMETRIC, colorSpace == AOI_GraySpace && bitsPerPixel == 8 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_MINISWHITE );
148 else if( colorSpace == AOI_RGBSpace )
149 TIFFSetField(mTif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
150 else if( colorSpace == AOI_CMYKSpace )
151 TIFFSetField(mTif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
153 TIFFSetField(mTif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
154 TIFFSetField(mTif, TIFFTAG_SAMPLESPERPIXEL, nrOfPlanes );
155 TIFFSetField(mTif, TIFFTAG_BITSPERSAMPLE, bitsPerPixel );
156 if( compression == TIFF_Comp_CCITT )
157 TIFFSetField(mTif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4 );
158 else if( compression == TIFF_Comp_LZW )
159 TIFFSetField(mTif, TIFFTAG_COMPRESSION, COMPRESSION_LZW );
161 TIFFSetField(mTif, TIFFTAG_COMPRESSION, COMPRESSION_NONE );
162 uint32_t rowsperstrip = TIFFDefaultStripSize( mTif, 128 );
163 if( rowsperstrip > height )
164 rowsperstrip = height;
165 TIFFSetField( mTif, TIFFTAG_ROWSPERSTRIP, rowsperstrip );
167 mBitsPerPixel = bitsPerPixel;
168 mNrOfPlanes = nrOfPlanes;
174::ExceptionCode AOI_TIFFWriter::WriteData( uint32_t lines,
const void* imageData )
180 uint8_t* data = (uint8_t*)imageData;
181 uint32_t rowBytes = ( mWidth * mBitsPerPixel * mNrOfPlanes + 7 ) / 8;
182 for( uint32_t l = 0; l != lines; ++l )
184 if( ( err = TIFFWriteScanline( mTif, data, mRow, 0 ) ) < 0 )
int32_t AOIAPI AOI_ColorSpaceComponents(AOI_ColorSpaceEnum space)
::ExceptionCode AOI_WriteTIFF(const aur::ACPL::FileSpec &path, uint8_t compression, uint16_t platform, AOI_ColorSpaceEnum colorSpace, uint16_t bitsPerPixel, uint32_t width, uint32_t height, float dpiX, float dpiY, const void *imageData)
TIFF writer.