| 1 |
/* |
| 2 |
Copyright (c) 2010, Michael Kazhdan |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#ifndef IMAGE_INCLUDED |
| 30 |
#define IMAGE_INCLUDED |
| 31 |
|
| 32 |
#define SUPPORT_TILES |
| 33 |
|
| 34 |
#include <string.h> |
| 35 |
#include "MyMiscellany.h" |
| 36 |
|
| 37 |
namespace PoissonRecon |
| 38 |
{ |
| 39 |
|
| 40 |
struct ImageReader |
| 41 |
{ |
| 42 |
virtual unsigned int nextRow( unsigned char* row ) = 0; |
| 43 |
static unsigned char* Read( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels ) |
| 44 |
{ |
| 45 |
ImageReader* reader = Get( fileName ); |
| 46 |
width = reader->width() , height = reader->height() , channels = reader->channels(); |
| 47 |
unsigned char* pixels = new unsigned char[ width*height*channels ]; |
| 48 |
for( unsigned int j=0 ; j<height ; j++ ) reader->nextRow( pixels + j*width*channels ); |
| 49 |
delete reader; |
| 50 |
return pixels; |
| 51 |
} |
| 52 |
static unsigned char* ReadColor( const char* fileName , unsigned int& width , unsigned int& height ) |
| 53 |
{ |
| 54 |
unsigned int channels; |
| 55 |
ImageReader* reader = Get( fileName ); |
| 56 |
width = reader->width() , height = reader->height() , channels = reader->channels(); |
| 57 |
if( channels!=1 && channels!=3 ) MK_THROW( "Requres one- or three-channel input" ); |
| 58 |
unsigned char* pixels = new unsigned char[ width*height*3 ]; |
| 59 |
unsigned char* pixelRow = new unsigned char[ width*channels]; |
| 60 |
for( unsigned int j=0 ; j<height ; j++ ) |
| 61 |
{ |
| 62 |
reader->nextRow( pixelRow ); |
| 63 |
if ( channels==3 ) memcpy( pixels+j*width*3 , pixelRow , sizeof(unsigned char)*width*3 ); |
| 64 |
else if( channels==1 ) for( unsigned int i=0 ; i<width ; i++ ) for( unsigned int c=0 ; c<3 ; c++ ) pixels[j*width*3+i*3+c] = pixelRow[i]; |
| 65 |
} |
| 66 |
delete[] pixelRow; |
| 67 |
delete reader; |
| 68 |
return pixels; |
| 69 |
} |
| 70 |
|
| 71 |
static bool ValidExtension( const char *ext ); |
| 72 |
static ImageReader* Get( const char* fileName ); |
| 73 |
static void GetInfo( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels ); |
| 74 |
virtual ~ImageReader( void ){ } |
| 75 |
unsigned int width( void ) const { return _width; } |
| 76 |
unsigned int height( void ) const { return _height; } |
| 77 |
unsigned int channels( void ) const { return _channels; } |
| 78 |
protected: |
| 79 |
unsigned int _width , _height , _channels; |
| 80 |
}; |
| 81 |
struct ImageWriterParams |
| 82 |
{ |
| 83 |
static const char* DefaultTileExtension; |
| 84 |
unsigned int quality; |
| 85 |
#ifdef SUPPORT_TILES |
| 86 |
const char* tileExtension; |
| 87 |
unsigned int tileWidth , tileHeight; |
| 88 |
ImageWriterParams* tileParams; |
| 89 |
ImageWriterParams( void ) : quality( 100 ) , tileExtension( DefaultTileExtension ) , tileWidth( 4096 ) , tileHeight( 4096 ) , tileParams( NULL ){}; |
| 90 |
#else // !SUPPORT_TILES |
| 91 |
ImageWriterParams( void ) : quality( 100 ){}; |
| 92 |
#endif // SUPPORT_TILES |
| 93 |
}; |
| 94 |
const char* ImageWriterParams::DefaultTileExtension = "jpg"; |
| 95 |
struct ImageWriter |
| 96 |
{ |
| 97 |
virtual unsigned int nextRow( const unsigned char* row ) = 0; |
| 98 |
virtual unsigned int nextRows( const unsigned char* rows , unsigned int rowNum ){ unsigned int row ; for( unsigned int r=0 ; r<rowNum ; r++ ) row = nextRow( rows + _width * _channels * r ) ; return row; } |
| 99 |
static void Write( const char* fileName , const unsigned char* pixels , unsigned int width , unsigned int height , unsigned int channels , ImageWriterParams params=ImageWriterParams() ) |
| 100 |
{ |
| 101 |
ImageWriter* writer = Get( fileName , width , height , channels , params ); |
| 102 |
for( unsigned int j=0 ; j<height ; j++ ) writer->nextRow( pixels + j*width*channels ); |
| 103 |
delete writer; |
| 104 |
} |
| 105 |
|
| 106 |
static bool ValidExtension( const char *ext ); |
| 107 |
static ImageWriter* Get( const char* fileName , unsigned int width , unsigned int height , unsigned int channels , ImageWriterParams params=ImageWriterParams() ); |
| 108 |
virtual ~ImageWriter( void ){ } |
| 109 |
unsigned int width( void ) const { return _width; } |
| 110 |
unsigned int height( void ) const { return _height; } |
| 111 |
unsigned int channels( void ) const { return _channels; } |
| 112 |
protected: |
| 113 |
unsigned int _width , _height , _channels; |
| 114 |
}; |
| 115 |
|
| 116 |
#ifdef SUPPORT_TILES |
| 117 |
struct TiledImageReader : public ImageReader |
| 118 |
{ |
| 119 |
unsigned int nextRow( unsigned char* row ); |
| 120 |
TiledImageReader( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels ); |
| 121 |
~TiledImageReader( void ); |
| 122 |
static bool GetInfo( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels ); |
| 123 |
protected: |
| 124 |
ImageReader** _tileReaders; |
| 125 |
char** _tileNames; |
| 126 |
unsigned int _tileRows , _tileColumns , _currentPixelRow , _currentTileRow , *_tileWidths , *_tileHeights; |
| 127 |
}; |
| 128 |
struct TiledImageWriter : public ImageWriter |
| 129 |
{ |
| 130 |
unsigned int nextRow( const unsigned char* row ); |
| 131 |
TiledImageWriter( const char* fileName , unsigned int width , unsigned int height , unsigned int channels , ImageWriterParams params ); |
| 132 |
~TiledImageWriter( void ); |
| 133 |
protected: |
| 134 |
ImageWriter** _tileWriters; |
| 135 |
char** _tileNames; |
| 136 |
unsigned int _tileWidth , _tileHeight , _tileRows , _tileColumns , _currentPixelRow; |
| 137 |
ImageWriterParams _params; |
| 138 |
}; |
| 139 |
#endif // SUPPORT_TILES |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
// [WARNING] Need to include "png.h" before "jpeg.h" so that "setjmp.h" is not already included (?) |
| 144 |
#include "PNG.h" |
| 145 |
#include "JPEG.h" |
| 146 |
|
| 147 |
namespace PoissonRecon |
| 148 |
{ |
| 149 |
struct FileNameParser |
| 150 |
{ |
| 151 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 152 |
static const char Separator = (char)'\\'; |
| 153 |
#else // !_WIN |
| 154 |
static const char Separator = (char)'/'; |
| 155 |
#endif // _WIN |
| 156 |
static inline char* Extension ( const char* fileName ){ return __Split( fileName , '.' , false , false ); } |
| 157 |
static inline char* Header ( const char* fileName ){ return __Split( fileName , '.' , true , false ); } |
| 158 |
static inline char* Local ( const char* fileName ){ return __Split( fileName , Separator , false , false ); } |
| 159 |
static inline char* Dir ( const char* fileName ){ return __Split( fileName , Separator , true , false ); } |
| 160 |
static inline char* LocalHeader( const char* fileName ) |
| 161 |
{ |
| 162 |
char* localFileName = Local( fileName ); |
| 163 |
if( !localFileName ) MK_THROW( "Couldn't get local file name: " , fileName ); |
| 164 |
char* localFileHeader = Header( localFileName ); |
| 165 |
delete[] localFileName; |
| 166 |
return localFileHeader; |
| 167 |
} |
| 168 |
|
| 169 |
protected: |
| 170 |
static inline char* __Split( const char* fileName , char splitChar , bool front , bool first ) |
| 171 |
{ |
| 172 |
int position; |
| 173 |
char* out; |
| 174 |
if( first ){ for( position=0 ; position<strlen(fileName) ; position++ ) if( fileName[position]==splitChar ) break; } |
| 175 |
else { for( position=(int)strlen(fileName)-1 ; position>=0 ; position-- ) if( fileName[position]==splitChar ) break; } |
| 176 |
|
| 177 |
if( front ) |
| 178 |
{ |
| 179 |
if( position==-1 ) out = NULL; |
| 180 |
else |
| 181 |
{ |
| 182 |
out = new char[ strlen(fileName)+1 ]; |
| 183 |
strcpy( out , fileName ); |
| 184 |
out[ position ] = 0; |
| 185 |
} |
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
if( position==strlen(fileName) ) out = NULL; |
| 190 |
else |
| 191 |
{ |
| 192 |
out = new char[ strlen(fileName)-position ]; |
| 193 |
strcpy( out , fileName+position+1 ); |
| 194 |
} |
| 195 |
} |
| 196 |
return out; |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
inline bool ImageReader::ValidExtension( const char *ext ) |
| 201 |
{ |
| 202 |
#ifdef WIN32 |
| 203 |
if ( !_stricmp( ext , "jpeg" ) || !_stricmp( ext , "jpg" ) ) return true; |
| 204 |
else if( !_stricmp( ext , "png" ) ) return true; |
| 205 |
else if( !_stricmp( ext , "iGrid" ) ) return true; |
| 206 |
#else // !WIN32 |
| 207 |
if( !strcasecmp( ext , "jpeg" ) || !strcasecmp( ext , "jpg" ) ) return true; |
| 208 |
else if( !strcasecmp( ext , "png" ) ) return true; |
| 209 |
else if( !strcasecmp( ext , "iGrid" ) ) return true; |
| 210 |
#endif // WIN32 |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
inline ImageReader* ImageReader::Get( const char* fileName ) |
| 215 |
{ |
| 216 |
unsigned int width , height , channels; |
| 217 |
ImageReader* reader = NULL; |
| 218 |
char* ext = FileNameParser::Extension( fileName ); |
| 219 |
#ifdef WIN32 |
| 220 |
if ( !_stricmp( ext , "jpeg" ) || !_stricmp( ext , "jpg" ) ) reader = new JPEGReader( fileName , width , height , channels ); |
| 221 |
else if( !_stricmp( ext , "png" ) ) reader = new PNGReader( fileName , width , height , channels ); |
| 222 |
else if( !_stricmp( ext , "iGrid" ) ) reader = new TiledImageReader( fileName , width , height , channels ); |
| 223 |
#else // !WIN32 |
| 224 |
if( !strcasecmp( ext , "jpeg" ) || !strcasecmp( ext , "jpg" ) ) reader = new JPEGReader( fileName , width , height , channels ); |
| 225 |
else if( !strcasecmp( ext , "png" ) ) reader = new PNGReader( fileName , width , height , channels ); |
| 226 |
else if( !strcasecmp( ext , "iGrid" ) ) reader = new TiledImageReader( fileName , width , height , channels ); |
| 227 |
#endif // WIN32 |
| 228 |
else |
| 229 |
{ |
| 230 |
delete[] ext; |
| 231 |
MK_THROW( "failed to get image reader for: " , fileName ); |
| 232 |
} |
| 233 |
reader->_width = width; |
| 234 |
reader->_height = height; |
| 235 |
reader->_channels = channels; |
| 236 |
|
| 237 |
delete[] ext; |
| 238 |
return reader; |
| 239 |
} |
| 240 |
inline void ImageReader::GetInfo( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels ) |
| 241 |
{ |
| 242 |
char* ext = FileNameParser::Extension( fileName ); |
| 243 |
#ifdef WIN32 |
| 244 |
if( !_stricmp( ext , "jpeg" ) || !_stricmp( ext , "jpg" ) ) JPEGReader::GetInfo( fileName , width , height , channels ); |
| 245 |
else if( !_stricmp( ext , "png" ) ) PNGReader::GetInfo( fileName , width , height , channels ); |
| 246 |
else if( !_stricmp( ext , "iGrid" ) ) TiledImageReader::GetInfo( fileName , width , height , channels ); |
| 247 |
#else // !WIN32 |
| 248 |
if( !strcasecmp( ext , "jpeg" ) || !strcasecmp( ext , "jpg" ) ) JPEGReader::GetInfo( fileName , width , height , channels ); |
| 249 |
else if( !strcasecmp( ext , "png" ) ) PNGReader::GetInfo( fileName , width , height , channels ); |
| 250 |
else if( !strcasecmp( ext , "iGrid" ) ) TiledImageReader::GetInfo( fileName , width , height , channels ); |
| 251 |
#endif // WIN32 |
| 252 |
delete[] ext; |
| 253 |
} |
| 254 |
|
| 255 |
inline bool ImageWriter::ValidExtension( const char *ext ) |
| 256 |
{ |
| 257 |
#ifdef WIN32 |
| 258 |
if( !_stricmp( ext , "jpeg" ) || !_stricmp( ext , "jpg" ) ) return true; |
| 259 |
else if( !_stricmp( ext , "png" ) ) return true; |
| 260 |
#ifdef SUPPORT_TILES |
| 261 |
else if( !_stricmp( ext , "iGrid" ) ) return true; |
| 262 |
#endif // SUPPORT_TILES |
| 263 |
#else // !WIN32 |
| 264 |
if( !strcasecmp( ext , "jpeg" ) || !strcasecmp( ext , "jpg" ) ) return true; |
| 265 |
else if( !strcasecmp( ext , "png" ) ) return true; |
| 266 |
#ifdef SUPPORT_TILES |
| 267 |
else if( !strcasecmp( ext , "iGrid" ) ) return true; |
| 268 |
#endif // SUPPORT_TILES |
| 269 |
#endif // WIN32 |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
inline ImageWriter* ImageWriter::Get( const char* fileName , unsigned int width , unsigned int height , unsigned int channels , ImageWriterParams params ) |
| 274 |
{ |
| 275 |
ImageWriter* writer = NULL; |
| 276 |
char* ext = FileNameParser::Extension( fileName ); |
| 277 |
#ifdef WIN32 |
| 278 |
if( !_stricmp( ext , "jpeg" ) || !_stricmp( ext , "jpg" ) ) writer = new JPEGWriter( fileName , width , height , channels , params.quality ); |
| 279 |
else if( !_stricmp( ext , "png" ) ) writer = new PNGWriter( fileName , width , height , channels , params.quality ); |
| 280 |
#ifdef SUPPORT_TILES |
| 281 |
else if( !_stricmp( ext , "iGrid" ) ) writer = new TiledImageWriter( fileName , width , height , channels , params ); |
| 282 |
#endif // SUPPORT_TILES |
| 283 |
#else // !WIN32 |
| 284 |
if( !strcasecmp( ext , "jpeg" ) || !strcasecmp( ext , "jpg" ) ) writer = new JPEGWriter( fileName , width , height , channels , params.quality ); |
| 285 |
else if( !strcasecmp( ext , "png" ) ) writer = new PNGWriter( fileName , width , height , channels , params.quality ); |
| 286 |
#ifdef SUPPORT_TILES |
| 287 |
else if( !strcasecmp( ext , "iGrid" ) ) writer = new TiledImageWriter( fileName , width , height , channels , params ); |
| 288 |
#endif // SUPPORT_TILES |
| 289 |
#endif // WIN32 |
| 290 |
else |
| 291 |
{ |
| 292 |
delete[] ext; |
| 293 |
MK_THROW( "failed to get image writer for: " , fileName ); |
| 294 |
} |
| 295 |
writer->_width = width; |
| 296 |
writer->_height = height; |
| 297 |
writer->_channels = channels; |
| 298 |
|
| 299 |
delete[] ext; |
| 300 |
return writer; |
| 301 |
} |
| 302 |
|
| 303 |
#ifdef SUPPORT_TILES |
| 304 |
inline bool TiledImageReader::GetInfo( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels ) |
| 305 |
{ |
| 306 |
char* fileDir = FileNameParser::Dir( fileName ); |
| 307 |
unsigned int *_tileHeights , *_tileWidths; |
| 308 |
unsigned int _tileRows , _tileColumns , _channels; |
| 309 |
FILE* fp = fopen( fileName , "r" ); |
| 310 |
if( !fp ){ MK_WARN( "Couldn't open file for reading: " , fileName ) ; return false; } |
| 311 |
{ |
| 312 |
char line[1024]; |
| 313 |
if( !fgets( line , 1024 , fp ) ) MK_THROW( "Failed to read column line from: " , fileName ); |
| 314 |
line[strlen(line)-1] = 0; |
| 315 |
if( sscanf( line , "Columns: %d" , &_tileColumns )!=1 ) MK_THROW( "Failed to read column count from: " , fileName , " (" , line , ")" ); |
| 316 |
if( !fgets( line , 1024 , fp ) ) MK_THROW( "Failed to read row line from: " , fileName ); |
| 317 |
line[strlen(line)-1] = 0; |
| 318 |
if( sscanf( line , "Rows: %d" , &_tileRows )!=1 ) MK_THROW( "Failed to read row count from: " , fileName , " (" , line , ")" ); |
| 319 |
_tileHeights = new unsigned int[ _tileRows+1 ]; |
| 320 |
_tileWidths = new unsigned int[ _tileColumns+1 ]; |
| 321 |
|
| 322 |
char tileName[2048]; |
| 323 |
for( unsigned int r=0 ; r<_tileRows ; r++ ) for( unsigned int c=0 ; c<_tileColumns ; c++ ) |
| 324 |
{ |
| 325 |
if( !fgets( line , 1024 , fp ) ) MK_THROW( "Failed to read tile name from: " , fileName ); |
| 326 |
line[strlen(line)-1] = 0; |
| 327 |
if( fileDir ) sprintf( tileName , "%s%c%s" , fileDir , FileNameParser::Separator , line ); |
| 328 |
else sprintf( tileName , "%s" , line ); |
| 329 |
|
| 330 |
unsigned int _w , _h , _c; |
| 331 |
ImageReader::GetInfo( tileName , _w , _h , _c ); |
| 332 |
if( !r && !c ) _channels = _c; |
| 333 |
else if( _channels!=_c ) MK_THROW( "Number of color channels don't match: " , _channels , " != " , _c ); |
| 334 |
if( !r ) _tileWidths[c+1] = _w; |
| 335 |
else if( _tileWidths[c+1]!=_w ) MK_THROW( "Images in the same column must have the same width: " , _tileWidths[c+1] , " != " , _w ); |
| 336 |
if( !c ) _tileHeights[r+1] = _h; |
| 337 |
else if( _tileHeights[r+1]!=_h ) MK_THROW( "Images in the same row must have the same heights: " , _tileHeights[r+1] ," != " , _h ); |
| 338 |
} |
| 339 |
} |
| 340 |
fclose( fp ); |
| 341 |
if( fileDir ) delete[] fileDir; |
| 342 |
_tileWidths[0] = _tileHeights[0] = 0; |
| 343 |
for( unsigned int c=0 ; c<_tileColumns ; c++ ) _tileWidths[c+1] += _tileWidths[c]; |
| 344 |
for( unsigned int r=0 ; r<_tileRows ; r++ ) _tileHeights[r+1] += _tileHeights[r]; |
| 345 |
width = _tileWidths[_tileColumns] , height = _tileHeights[_tileRows] , channels = _channels; |
| 346 |
return true; |
| 347 |
} |
| 348 |
|
| 349 |
inline TiledImageReader::TiledImageReader( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels ) |
| 350 |
{ |
| 351 |
char* fileDir = FileNameParser::Dir( fileName ); |
| 352 |
FILE* fp = fopen( fileName , "r" ); |
| 353 |
if( !fp ) MK_THROW( "Couldn't open file for reading: " , fileName ); |
| 354 |
{ |
| 355 |
char line[1024]; |
| 356 |
if( !fgets( line , 1024 , fp ) ) MK_THROW( "Failed read column line from: " , fileName ); |
| 357 |
line[strlen(line)-1] = 0; |
| 358 |
if( sscanf( line , "Columns: %d" , &_tileColumns )!=1 ) MK_THROW( "Failed to read column count from: " , fileName , " (" , line , ")" ); |
| 359 |
if( !fgets( line , 1024 , fp ) ) MK_THROW( "Failed read row line from: " , fileName ); |
| 360 |
line[strlen(line)-1] = 0; |
| 361 |
if( sscanf( line , "Rows: %d" , &_tileRows )!=1 ) MK_THROW( "Failed to read row count from: " , fileName , " (" , line , ")" ); |
| 362 |
|
| 363 |
_tileReaders = new ImageReader*[ _tileColumns ]; |
| 364 |
_tileHeights = new unsigned int[ _tileRows+1 ]; |
| 365 |
_tileWidths = new unsigned int[ _tileColumns+1 ]; |
| 366 |
|
| 367 |
_tileNames = new char*[ _tileColumns * _tileRows ]; |
| 368 |
char tileName[2048]; |
| 369 |
for( unsigned int r=0 ; r<_tileRows ; r++ ) for( unsigned int c=0 ; c<_tileColumns ; c++ ) |
| 370 |
{ |
| 371 |
if( !fgets( line , 1024 , fp ) ) MK_THROW( "Failed to read tile name from: " , fileName ); |
| 372 |
line[strlen(line)-1] = 0; |
| 373 |
if( fileDir ) sprintf( tileName , "%s%c%s" , fileDir , FileNameParser::Separator , line ); |
| 374 |
else sprintf( tileName , "%s" , line ); |
| 375 |
_tileNames[r*_tileColumns+c] = new char[ strlen(tileName)+1 ]; |
| 376 |
strcpy( _tileNames[r*_tileColumns+c] , tileName ); |
| 377 |
} |
| 378 |
} |
| 379 |
fclose( fp ); |
| 380 |
if( fileDir ) delete[] fileDir; |
| 381 |
for( unsigned int r=0 ; r<_tileRows ; r++ ) for( unsigned int c=0 ; c<_tileColumns ; c++ ) |
| 382 |
{ |
| 383 |
unsigned int _w , _h , _c; |
| 384 |
ImageReader::GetInfo( _tileNames[r*_tileColumns+c] , _w , _h , _c ); |
| 385 |
if( !r && !c ) _channels = _c; |
| 386 |
else if( _channels!=_c ) MK_THROW( "Number of color channels don't match: " , _channels , " != " , _c ); |
| 387 |
if( !r ) _tileWidths[c+1] = _w; |
| 388 |
else if( _tileWidths[c+1]!=_w ) MK_THROW( "Images in the same column must have the same width: " , _tileWidths[c+1] , " != " , _w ); |
| 389 |
if( !c ) _tileHeights[r+1] = _h; |
| 390 |
else if( _tileHeights[r+1]!=_h ) MK_THROW( "Images in the same row must have the same heights: " , _tileHeights[r+1] , " != " , _h ); |
| 391 |
} |
| 392 |
_tileWidths[0] = _tileHeights[0] = 0; |
| 393 |
for( unsigned int c=0 ; c<_tileColumns ; c++ ) _tileWidths[c+1] += _tileWidths[c]; |
| 394 |
for( unsigned int r=0 ; r<_tileRows ; r++ ) _tileHeights[r+1] += _tileHeights[r]; |
| 395 |
width = _width = _tileWidths[_tileColumns] , height = _height = _tileHeights[_tileRows] , channels = _channels; |
| 396 |
_currentPixelRow = _currentTileRow = 0; |
| 397 |
} |
| 398 |
inline TiledImageReader::~TiledImageReader( void ) |
| 399 |
{ |
| 400 |
delete[] _tileReaders; |
| 401 |
for( unsigned int i=0 ; i<_tileColumns*_tileRows ; i++ ) delete[] _tileNames[i]; |
| 402 |
delete[] _tileNames; |
| 403 |
delete[] _tileWidths; |
| 404 |
delete[] _tileHeights; |
| 405 |
} |
| 406 |
inline unsigned TiledImageReader::nextRow( unsigned char* row ) |
| 407 |
{ |
| 408 |
// If it's the first row, set up the readers |
| 409 |
if( _currentPixelRow==_tileHeights[ _currentTileRow ] ) for( unsigned int c=0 ; c<_tileColumns ; c++ ) _tileReaders[c] = ImageReader::Get( _tileNames[ _currentTileRow * _tileColumns + c ] ); |
| 410 |
|
| 411 |
// Read the row fragments |
| 412 |
for( unsigned int c=0 ; c<_tileColumns ; c++ ) _tileReaders[c]->nextRow( row + c * _tileWidths[c] * _channels ); |
| 413 |
|
| 414 |
// If it's the last row of the tile, free up the readers |
| 415 |
if( _currentPixelRow==_tileHeights[_currentTileRow+1]-1 ) |
| 416 |
{ |
| 417 |
for( unsigned int c=0 ; c<_tileColumns ; c++ ) delete _tileReaders[c]; |
| 418 |
_currentTileRow++; |
| 419 |
} |
| 420 |
|
| 421 |
return _currentPixelRow++; |
| 422 |
} |
| 423 |
|
| 424 |
inline TiledImageWriter::TiledImageWriter( const char* fileName , unsigned int width , unsigned int height , unsigned int channels , ImageWriterParams params ) |
| 425 |
{ |
| 426 |
_width = width , _height = height , _channels = channels , _tileWidth = params.tileWidth , _tileHeight = params.tileHeight; |
| 427 |
_tileColumns = ( _width + ( _tileWidth-1 ) ) / _tileWidth , _tileRows = ( _height + ( _tileHeight-1 ) ) / _tileHeight; |
| 428 |
_tileWriters = new ImageWriter*[ _tileColumns ]; |
| 429 |
_tileNames = new char*[ _tileColumns * _tileRows ]; |
| 430 |
if( params.tileParams ) _params = *params.tileParams; |
| 431 |
|
| 432 |
char tileName[1024]; |
| 433 |
char* tileHeader = FileNameParser::Header( fileName ); |
| 434 |
for( unsigned int r=0 ; r<_tileRows ; r++ ) for( unsigned int c=0 ; c<_tileColumns ; c++ ) |
| 435 |
{ |
| 436 |
sprintf( tileName , "%s.%d.%d.%s" , tileHeader , c , r , params.tileExtension ); |
| 437 |
_tileNames[r*_tileColumns+c] = new char[ strlen(tileName)+1 ]; |
| 438 |
strcpy( _tileNames[r*_tileColumns+c] , tileName ); |
| 439 |
} |
| 440 |
delete[] tileHeader; |
| 441 |
FILE* fp = fopen( fileName , "w" ); |
| 442 |
if( !fp ) MK_THROW( "Failed to open file for writing: " , fileName ); |
| 443 |
fprintf( fp , "Columns: %d\n" , _tileColumns ); |
| 444 |
fprintf( fp , "Rows: %d\n" , _tileRows ); |
| 445 |
for( unsigned int i=0 ; i<_tileRows*_tileColumns ; i++ ) |
| 446 |
{ |
| 447 |
char* localTileName = FileNameParser::Local( _tileNames[i] ); |
| 448 |
fprintf( fp , "%s\n" , localTileName ); |
| 449 |
delete[] localTileName; |
| 450 |
} |
| 451 |
fclose( fp ); |
| 452 |
_currentPixelRow = 0; |
| 453 |
} |
| 454 |
inline TiledImageWriter::~TiledImageWriter( void ) |
| 455 |
{ |
| 456 |
delete[] _tileWriters; |
| 457 |
for( unsigned int i=0 ; i<_tileColumns*_tileRows ; i++ ) delete[] _tileNames[i]; |
| 458 |
delete[] _tileNames; |
| 459 |
} |
| 460 |
inline unsigned int TiledImageWriter::nextRow( const unsigned char* row ) |
| 461 |
{ |
| 462 |
unsigned int r = _currentPixelRow / _tileHeight; |
| 463 |
if( ( _currentPixelRow % _tileHeight )==0 ) |
| 464 |
{ |
| 465 |
for( unsigned int c=0 ; c<_tileColumns ; c++ ) |
| 466 |
_tileWriters[c] = ImageWriter::Get( _tileNames[ r * _tileColumns + c ] , std::min< unsigned int >( _tileWidth , _width - _tileWidth*c ) , std::min< unsigned int >( _tileHeight , _height - _tileHeight*r ) , _channels , _params ); |
| 467 |
} |
| 468 |
for( int c=0 ; c<(int)_tileColumns ; c++ ) _tileWriters[c]->nextRow( row + c * _tileWidth * _channels ); |
| 469 |
if( ( _currentPixelRow % _tileHeight )==( _tileHeight-1 ) || _currentPixelRow==(_height-1) ) for( unsigned int c=0 ; c<_tileColumns ; c++ ) delete _tileWriters[c]; |
| 470 |
|
| 471 |
return _currentPixelRow++; |
| 472 |
} |
| 473 |
#endif // SUPPORT_TILES |
| 474 |
} |
| 475 |
|
| 476 |
#endif // IMAGE_INCLUDED |