Aurelon Open API 8.1.1
Loading...
Searching...
No Matches
AOI_TIFFWriter.cpp
1#include "./AOI_TIFFWriter.h"
2#include <ACPL/SwapStream.h>
3#include <TIFF Library/libtiff/tiffiop.h>
4
5using namespace aur;
6using namespace aur::ACPL;
7
8extern Stream* gFile;
9
10static aur::ExceptionCode WriteTiff( TIFF*& tif, uint8_t compression, uint16_t /* platform */,
11 AOI_ColorSpaceEnum colorSpace, uint16_t bitsPerPixel, uint32_t width, uint32_t height,
12 float dpiX, float dpiY, const void* imageData )
13{
14 int err = 0;
15
16 if( tif )
17 {
18 uint32_t nrOfPlanes = (uint32_t)AOI_ColorSpaceComponents( colorSpace );
19 //
20 // Setup tif
21 //
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);
33
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 );
41 else
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 );
47 //
48 // Process data
49 //
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 )
53 {
54 if( ( err = TIFFWriteScanline( tif, data, row, 0 ) ) < 0 )
55 break;
56 data += rowBytes;
57 }
58 //
59 // Clean up
60 //
61 TIFFClose( tif );
62 }
63 return err;
64}
65
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 )
93{
94 TIFF* tif = TIFFOpenW( path.GetPOSIXPath(), "w" );
95
96 return WriteTiff( tif, compression, platform, colorSpace, bitsPerPixel, width, height, dpiX, dpiY, imageData );
97}
98
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 )
102{
103 TIFF* tif = TIFFOpenW( ACPL::UString( path ), "w" );
104
105 return WriteTiff( tif, compression, platform, colorSpace, bitsPerPixel, width, height, dpiX, dpiY, imageData );
106}
107
120AOI_TIFFWriter::AOI_TIFFWriter()
121{
122}
123
124AOI_TIFFWriter::~AOI_TIFFWriter()
125{
126 TIFFClose( mTif );
127}
128
129::ExceptionCode AOI_TIFFWriter::Init( const aur::ACPL::FileSpec& path, uint8_t compression, uint16_t /* platform */,
130 AOI_ColorSpaceEnum colorSpace, uint16_t bitsPerPixel, uint32_t width, uint32_t height,
131 float dpiX, float dpiY )
132{
133 mTif = TIFFOpenW( path.GetPOSIXPath(), "w" );
134 if( !mTif )
135 return -1;
136
137 uint32_t nrOfPlanes = (uint32_t)AOI_ColorSpaceComponents( colorSpace );
138 //
139 // Setup tif
140 //
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);
152
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 );
160 else
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 );
166 mWidth = width;
167 mBitsPerPixel = bitsPerPixel;
168 mNrOfPlanes = nrOfPlanes;
169 mHeight = height;
170 mRow = 0;
171 return 0;
172}
173
174::ExceptionCode AOI_TIFFWriter::WriteData( uint32_t lines, const void* imageData )
175{
176 //
177 // Process data
178 //
179 ::ExceptionCode err;
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 )
183 {
184 if( ( err = TIFFWriteScanline( mTif, data, mRow, 0 ) ) < 0 )
185 break;
186 data += rowBytes;
187 ++mRow;
188 }
189 return err < 0;
190}
191
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.