1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
/******************************************************************************
*
* Purpose: Implementation of the Create() function to create new PCIDSK files.
*
******************************************************************************
* Copyright (c) 2009
* PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "pcidsk.h"
#include "pcidsk_config.h"
#include "pcidsk_types.h"
#include "pcidsk_exception.h"
#include "pcidsk_file.h"
#include "pcidsk_georef.h"
#include "core/pcidsk_utils.h"
#include "segment/sysblockmap.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace PCIDSK;
/************************************************************************/
/* Create() */
/************************************************************************/
/**
* Create a PCIDSK (.pix) file.
*
* @param filename the name of the PCIDSK file to create.
* @param pixels the width of the new file in pixels.
* @param lines the height of the new file in scanlines.
* @param channel_count the number of channels to create.
* @param channel_types an array of types for all the channels, or NULL for
* all CHN_8U channels.
* @param options creation options (interleaving, etc)
* @param interfaces Either NULL to use default interfaces, or a pointer
* to a populated interfaces object.
*
* @return a pointer to a file object for accessing the PCIDSK file.
*/
PCIDSKFile PCIDSK_DLL *
PCIDSK::Create( std::string filename, int pixels, int lines,
int channel_count, eChanType *channel_types,
std::string options, const PCIDSKInterfaces *interfaces )
{
/* -------------------------------------------------------------------- */
/* Use default interfaces if none are passed in. */
/* -------------------------------------------------------------------- */
PCIDSKInterfaces default_interfaces;
if( interfaces == NULL )
interfaces = &default_interfaces;
/* -------------------------------------------------------------------- */
/* Default the channel types to all 8U if not provided. */
/* -------------------------------------------------------------------- */
std::vector<eChanType> default_channel_types;
if( channel_types == NULL )
{
default_channel_types.resize( channel_count+1, CHN_8U );
channel_types = &(default_channel_types[0]);
}
/* -------------------------------------------------------------------- */
/* Validate parameters. */
/* -------------------------------------------------------------------- */
const char *interleaving = NULL;
std::string compression = "NONE";
bool nozero = false;
bool nocreate = false;
bool externallink = false;
int blocksize = 127;
UCaseStr( options );
if(strncmp(options.c_str(),"PIXEL",5) == 0 )
interleaving = "PIXEL";
else if( strncmp(options.c_str(),"BAND",4) == 0 )
interleaving = "BAND";
else if( strncmp(options.c_str(),"TILED",5) == 0 )
{
interleaving = "FILE";
ParseTileFormat( options, blocksize, compression );
}
else if( strncmp(options.c_str(),"FILE",4) == 0 )
{
if( strncmp(options.c_str(),"FILENOCREATE",12) == 0 )
nocreate = true;
else if( strncmp(options.c_str(),"FILELINK",8) == 0 )
{
nocreate = true;
externallink = true;
}
interleaving = "FILE";
}
else
ThrowPCIDSKException( "PCIDSK::Create() options '%s' not recognised.",
options.c_str() );
if( strstr(options.c_str(),"NOZERO") != NULL )
nozero = true;
/* -------------------------------------------------------------------- */
/* Validate the channel types. */
/* -------------------------------------------------------------------- */
int channels[7] = {0,0,0,0,0,0,0};
int chan_index;
bool regular = true;
for( chan_index=0; chan_index < channel_count; chan_index++ )
{
if( chan_index > 0
&& ((int) channel_types[chan_index])
< ((int) channel_types[chan_index-1]) )
regular = false;
channels[((int) channel_types[chan_index])]++;
}
if( !regular && strcmp(interleaving,"FILE") != 0 )
{
ThrowPCIDSKException(
"Requested mixture of band types not supported for interleaving=%s.",
interleaving );
}
/* -------------------------------------------------------------------- */
/* Create the file. */
/* -------------------------------------------------------------------- */
void *io_handle = interfaces->io->Open( filename, "w+" );
assert( io_handle != NULL );
/* ==================================================================== */
/* Establish some key file layout information. */
/* ==================================================================== */
int image_header_start = 1; // in blocks
uint64 image_data_start, image_data_size=0; // in blocks
uint64 segment_ptr_start, segment_ptr_size=64; // in blocks
int pixel_group_size, line_size; // in bytes
int image_header_count = channel_count;
/* -------------------------------------------------------------------- */
/* Pixel interleaved. */
/* -------------------------------------------------------------------- */
if( strcmp(interleaving,"PIXEL") == 0 )
{
pixel_group_size =
channels[0] + // CHN_8U
channels[1] * DataTypeSize(CHN_16U) +
channels[2] * DataTypeSize(CHN_16S) +
channels[3] * DataTypeSize(CHN_32R) +
channels[4] * DataTypeSize(CHN_C16U) +
channels[5] * DataTypeSize(CHN_C16S) +
channels[6] * DataTypeSize(CHN_C32R);
//channels[0] + channels[1]*2 + channels[2]*2 + channels[3]*4;
line_size = ((pixel_group_size * pixels + 511) / 512) * 512;
image_data_size = (((uint64)line_size) * lines) / 512;
// TODO: Old code enforces a 1TB limit for some reason.
}
/* -------------------------------------------------------------------- */
/* Band interleaved. */
/* -------------------------------------------------------------------- */
else if( strcmp(interleaving,"BAND") == 0 )
{
pixel_group_size =
channels[0] + // CHN_8U
channels[1] * DataTypeSize(CHN_16U) +
channels[2] * DataTypeSize(CHN_16S) +
channels[3] * DataTypeSize(CHN_32R) +
channels[4] * DataTypeSize(CHN_C16U) +
channels[5] * DataTypeSize(CHN_C16S) +
channels[6] * DataTypeSize(CHN_C32R);
// BAND interleaved bands are tightly packed.
image_data_size =
(((uint64)pixel_group_size) * pixels * lines + 511) / 512;
// TODO: Old code enforces a 1TB limit for some reason.
}
/* -------------------------------------------------------------------- */
/* FILE/Tiled. */
/* -------------------------------------------------------------------- */
else if( strcmp(interleaving,"FILE") == 0 )
{
// For some reason we reserve extra space, but only for FILE.
if( channel_count < 64 )
image_header_count = 64;
image_data_size = 0;
// TODO: Old code enforces a 1TB limit on the fattest band.
}
/* -------------------------------------------------------------------- */
/* Place components. */
/* -------------------------------------------------------------------- */
segment_ptr_start = image_header_start + image_header_count*2;
image_data_start = segment_ptr_start + segment_ptr_size;
/* ==================================================================== */
/* Prepare the file header. */
/* ==================================================================== */
PCIDSKBuffer fh(512);
char current_time[17];
GetCurrentDateTime( current_time );
// Initialize everything to spaces.
fh.Put( "", 0, 512 );
/* -------------------------------------------------------------------- */
/* File Type, Version, and Size */
/* Notice: we get the first 4 characters from PCIVERSIONAME. */
/* -------------------------------------------------------------------- */
// FH1 - magic format string.
fh.Put( "PCIDSK", 0, 8 );
// FH2 - TODO: Allow caller to pass this in.
fh.Put( "SDK V1.0", 8, 8 );
// FH3 - file size later.
fh.Put( (image_data_start + image_data_size), 16, 16 );
// FH4 - 16 characters reserved - spaces.
// FH5 - Description
fh.Put( filename.c_str(), 48, 64 );
// FH6 - Facility
fh.Put( "PCI Inc., Richmond Hill, Canada", 112, 32 );
// FH7.1 / FH7.2 - left blank (64+64 bytes @ 144)
// FH8 Creation date/time
fh.Put( current_time, 272, 16 );
// FH9 Update date/time
fh.Put( current_time, 288, 16 );
/* -------------------------------------------------------------------- */
/* Image Data */
/* -------------------------------------------------------------------- */
// FH10 - start block of image data
fh.Put( image_data_start+1, 304, 16 );
// FH11 - number of blocks of image data.
fh.Put( image_data_size, 320, 16 );
// FH12 - start block of image headers.
fh.Put( image_header_start+1, 336, 16 );
// FH13 - number of blocks of image headers.
fh.Put( image_header_count*2, 352, 8);
// FH14 - interleaving.
fh.Put( interleaving, 360, 8);
// FH15 - reserved - MIXED is for some ancient backwards compatability.
fh.Put( "MIXED", 368, 8);
// FH16 - number of image bands.
fh.Put( channel_count, 376, 8 );
// FH17 - width of image in pixels.
fh.Put( pixels, 384, 8 );
// FH18 - height of image in pixels.
fh.Put( lines, 392, 8 );
// FH19 - pixel ground size interpretation.
fh.Put( "METRE", 400, 8 );
// TODO:
//PrintDouble( fh->XPixelSize, "%16.9f", 1.0 );
//PrintDouble( fh->YPixelSize, "%16.9f", 1.0 );
fh.Put( "1.0", 408, 16 );
fh.Put( "1.0", 424, 16 );
/* -------------------------------------------------------------------- */
/* Segment Pointers */
/* -------------------------------------------------------------------- */
// FH22 - start block of segment pointers.
fh.Put( segment_ptr_start+1, 440, 16 );
// fH23 - number of blocks of segment pointers.
fh.Put( segment_ptr_size, 456, 8 );
/* -------------------------------------------------------------------- */
/* Number of different types of Channels */
/* -------------------------------------------------------------------- */
// FH24.1 - 8U bands.
fh.Put( channels[0], 464, 4 );
// FH24.2 - 16S bands.
fh.Put( channels[1], 468, 4 );
// FH24.3 - 16U bands.
fh.Put( channels[2], 472, 4 );
// FH24.4 - 32R bands.
fh.Put( channels[3], 476, 4 );
// FH24.5 - C16U bands
fh.Put( channels[4], 480, 4 );
// FH24.6 - C16S bands
fh.Put( channels[5], 484, 4 );
// FH24.7 - C32R bands
fh.Put( channels[6], 488, 4 );
/* -------------------------------------------------------------------- */
/* Write out the file header. */
/* -------------------------------------------------------------------- */
interfaces->io->Write( fh.buffer, 512, 1, io_handle );
/* ==================================================================== */
/* Write out the image headers. */
/* ==================================================================== */
PCIDSKBuffer ih( 1024 );
ih.Put( " ", 0, 1024 );
// IHi.1 - Text describing Channel Contents
ih.Put( "Contents Not Specified", 0, 64 );
// IHi.2 - Filename storing image.
if( strncmp(interleaving,"FILE",4) == 0 )
ih.Put( "<unintialized>", 64, 64 );
if( externallink )
{
// IHi.6.7 - IHi.6.10
ih.Put( 0, 250, 8 );
ih.Put( 0, 258, 8 );
ih.Put( pixels, 266, 8 );
ih.Put( lines, 274, 8 );
}
// IHi.3 - Creation time and date.
ih.Put( current_time, 128, 16 );
// IHi.4 - Creation time and date.
ih.Put( current_time, 144, 16 );
interfaces->io->Seek( io_handle, image_header_start*512, SEEK_SET );
for( chan_index = 0; chan_index < channel_count; chan_index++ )
{
ih.Put(DataTypeName(channel_types[chan_index]).c_str(), 160, 8);
if( strncmp("TILED",options.c_str(),5) == 0 )
{
char sis_filename[65];
sprintf( sis_filename, "/SIS=%d", chan_index );
ih.Put( sis_filename, 64, 64 );
// IHi.6.7 - IHi.6.10
ih.Put( 0, 250, 8 );
ih.Put( 0, 258, 8 );
ih.Put( pixels, 266, 8 );
ih.Put( lines, 274, 8 );
// IHi.6.11
ih.Put( 1, 282, 8 );
}
interfaces->io->Write( ih.buffer, 1024, 1, io_handle );
}
for( chan_index = channel_count;
chan_index < image_header_count;
chan_index++ )
{
ih.Put( "", 160, 8 );
ih.Put( "<unintialized>", 64, 64 );
ih.Put( "", 250, 40 );
interfaces->io->Write( ih.buffer, 1024, 1, io_handle );
}
/* ==================================================================== */
/* Write out the segment pointers, all spaces. */
/* ==================================================================== */
PCIDSKBuffer segment_pointers( (int) (segment_ptr_size*512) );
segment_pointers.Put( " ", 0, (int) (segment_ptr_size*512) );
interfaces->io->Seek( io_handle, segment_ptr_start*512, SEEK_SET );
interfaces->io->Write( segment_pointers.buffer, segment_ptr_size, 512,
io_handle );
/* -------------------------------------------------------------------- */
/* Ensure we write out something at the end of the image data */
/* to force the file size. */
/* -------------------------------------------------------------------- */
if( image_data_size > 0 )
{
interfaces->io->Seek( io_handle, (image_data_start + image_data_size)*512-1,
SEEK_SET );
interfaces->io->Write( "\0", 1, 1, io_handle );
}
/* -------------------------------------------------------------------- */
/* Close the raw file, and reopen as a pcidsk file. */
/* -------------------------------------------------------------------- */
interfaces->io->Close( io_handle );
PCIDSKFile *file = Open( filename, "r+", interfaces );
/* -------------------------------------------------------------------- */
/* Create a default georeferencing segment. */
/* -------------------------------------------------------------------- */
file->CreateSegment( "GEOref",
"Master Georeferencing Segment for File",
SEG_GEO, 6 );
/* -------------------------------------------------------------------- */
/* If the dataset is tiled, create the file band data. */
/* -------------------------------------------------------------------- */
if( strncmp(options.c_str(),"TILED",5) == 0 )
{
file->SetMetadataValue( "_DBLayout", options );
// For sizing the SysBMDir we want an approximate size of the
// the imagery.
uint64 rough_image_size =
(channels[0] + // CHN_8U
channels[1] * DataTypeSize(CHN_16U) +
channels[2] * DataTypeSize(CHN_16S) +
channels[3] * DataTypeSize(CHN_32R) +
channels[4] * DataTypeSize(CHN_C16U) +
channels[5] * DataTypeSize(CHN_C16S) +
channels[6] * DataTypeSize(CHN_C32R))
* (pixels * (uint64) lines);
uint64 sysbmdir_size = ((rough_image_size / 8192) * 28) / 512;
sysbmdir_size = (int) (sysbmdir_size * 1.1 + 100);
int segment = file->CreateSegment( "SysBMDir",
"System Block Map Directory - Do not modify.",
SEG_SYS, sysbmdir_size );
SysBlockMap *bm =
dynamic_cast<SysBlockMap *>(file->GetSegment( segment ));
for( chan_index = 0; chan_index < channel_count; chan_index++ )
{
bm->CreateVirtualImageFile( pixels, lines, blocksize, blocksize,
channel_types[chan_index],
compression );
}
}
/* -------------------------------------------------------------------- */
/* If we have a non-tiled FILE interleaved file, should we */
/* create external band files now? */
/* -------------------------------------------------------------------- */
if( strncmp(interleaving,"FILE",4) == 0
&& strncmp(options.c_str(),"TILED",5) != 0
&& !nocreate )
{
for( chan_index = 0; chan_index < channel_count; chan_index++ )
{
PCIDSKChannel *channel = file->GetChannel( chan_index + 1 );
int pixel_size = DataTypeSize(channel->GetType());
// build a band filename that uses the basename of the PCIDSK
// file, and adds ".nnn" based on the band.
std::string band_filename = filename;
char ext[5];
sprintf( ext, ".%03d", chan_index+1 );
size_t last_dot = band_filename.find_last_of(".");
if( last_dot != std::string::npos
&& (band_filename.find_last_of("/\\:") == std::string::npos
|| band_filename.find_last_of("/\\:") < last_dot) )
{
band_filename.resize( last_dot );
}
band_filename += ext;
// Now build a version without a path.
std::string relative_band_filename;
size_t path_div = band_filename.find_last_of( "/\\:" );
if( path_div == std::string::npos )
relative_band_filename = band_filename;
else
relative_band_filename = band_filename.c_str() + path_div + 1;
// create the file - ought we write the whole file?
void *band_io_handle = interfaces->io->Open( band_filename, "w" );
interfaces->io->Write( "\0", 1, 1, band_io_handle );
interfaces->io->Close( band_io_handle );
// Set the channel header information.
channel->SetChanInfo( relative_band_filename, 0, pixel_size,
pixel_size * pixels, true );
}
}
return file;
}
|