| 423 | | |
| 424 | | // which band do we want to read from... |
| 425 | | int numBands = _gdalDataSet->GetRasterCount(); |
| 426 | | GDALRasterBand* bandGray = 0; |
| 427 | | GDALRasterBand* bandRed = 0; |
| 428 | | GDALRasterBand* bandGreen = 0; |
| 429 | | GDALRasterBand* bandBlue = 0; |
| 430 | | GDALRasterBand* bandAlpha = 0; |
| 431 | | |
| 432 | | for(int b=1;b<=numBands;++b) |
| 433 | | { |
| 434 | | GDALRasterBand* band = _gdalDataSet->GetRasterBand(b); |
| 435 | | if (band->GetColorInterpretation()==GCI_GrayIndex) bandGray = band; |
| 436 | | else if (band->GetColorInterpretation()==GCI_RedBand) bandRed = band; |
| 437 | | else if (band->GetColorInterpretation()==GCI_GreenBand) bandGreen = band; |
| 438 | | else if (band->GetColorInterpretation()==GCI_BlueBand) bandBlue = band; |
| 439 | | else if (band->GetColorInterpretation()==GCI_AlphaBand) bandAlpha = band; |
| 440 | | else bandGray = band; |
| 441 | | } |
| 442 | | |
| 443 | | |
| 444 | | GDALRasterBand* bandSelected = 0; |
| 445 | | if (!bandSelected && bandGray) bandSelected = bandGray; |
| 446 | | else if (!bandSelected && bandAlpha) bandSelected = bandAlpha; |
| 447 | | else if (!bandSelected && bandRed) bandSelected = bandRed; |
| 448 | | else if (!bandSelected && bandGreen) bandSelected = bandGreen; |
| 449 | | else if (!bandSelected && bandBlue) bandSelected = bandBlue; |
| 450 | | |
| 451 | | if (!(bandRed && bandGreen && bandBlue) && bandSelected) |
| 452 | | { |
| 453 | | // do a hack to get the handling of gery scale images working by |
| 454 | | // copying the single band three times... |
| 455 | | // will fix later, Robert Osfield, May 2nd 2004. |
| 456 | | bandRed = bandSelected; |
| 457 | | bandGreen = bandSelected; |
| 458 | | bandBlue = bandSelected; |
| 459 | | } |
| 460 | | |
| 461 | | |
| 462 | | if (bandRed && bandGreen && bandBlue) |
| | 423 | |
| | 424 | bool hasRGB = _gdalDataSet->GetRasterCount() >= 3; |
| | 425 | bool hasColorTable = _gdalDataSet->GetRasterCount() >= 1 && _gdalDataSet->GetRasterBand(1)->GetColorTable(); |
| | 426 | bool hasGreyScale = _gdalDataSet->GetRasterCount() == 1; |
| | 427 | |
| | 428 | if (hasRGB || hasColorTable || hasGreyScale) |
| 475 | | bool directCopy = false; |
| 476 | | if (directCopy) |
| 477 | | { |
| 478 | | bandRed->RasterIO(GF_Read,windowX,_numValuesY-(windowY+windowHeight),windowWidth,windowHeight,(void*)(imageData+0),destWidth,destHeight,targetGDALType,pixelSpace,lineSpace); |
| 479 | | bandGreen->RasterIO(GF_Read,windowX,_numValuesY-(windowY+windowHeight),windowWidth,windowHeight,(void*)(imageData+1),destWidth,destHeight,targetGDALType,pixelSpace,lineSpace); |
| 480 | | bandBlue->RasterIO(GF_Read,windowX,_numValuesY-(windowY+windowHeight),windowWidth,windowHeight,(void*)(imageData+2),destWidth,destHeight,targetGDALType,pixelSpace,lineSpace); |
| 481 | | } |
| 482 | | else |
| 483 | | { |
| 484 | | unsigned char* tempImage = new unsigned char[destWidth*destHeight*3]; |
| 485 | | |
| 486 | | bandRed->RasterIO(GF_Read,windowX,_numValuesY-(windowY+windowHeight),windowWidth,windowHeight,(void*)(tempImage+0),destWidth,destHeight,targetGDALType,pixelSpace,pixelSpace*destWidth); |
| 487 | | bandGreen->RasterIO(GF_Read,windowX,_numValuesY-(windowY+windowHeight),windowWidth,windowHeight,(void*)(tempImage+1),destWidth,destHeight,targetGDALType,pixelSpace,pixelSpace*destWidth); |
| 488 | | bandBlue->RasterIO(GF_Read,windowX,_numValuesY-(windowY+windowHeight),windowWidth,windowHeight,(void*)(tempImage+2),destWidth,destHeight,targetGDALType,pixelSpace,pixelSpace*destWidth); |
| 489 | | |
| 490 | | // now copy into destination image |
| 491 | | unsigned char* sourceRowPtr = tempImage; |
| 492 | | unsigned int sourceRowDelta = pixelSpace*destWidth; |
| 493 | | unsigned char* destinationRowPtr = imageData; |
| 494 | | unsigned int destinationRowDelta = lineSpace; |
| 495 | | |
| 496 | | for(int row=0; |
| 497 | | row<destHeight; |
| 498 | | ++row, sourceRowPtr+=sourceRowDelta, destinationRowPtr+=destinationRowDelta) |
| | 441 | unsigned char* tempImage = new unsigned char[destWidth*destHeight*3]; |
| | 442 | |
| | 443 | |
| | 444 | /* New code courtesy of Frank Warmerdam of the GDAL group */ |
| | 445 | |
| | 446 | // RGB images ... or at least we assume 3+ band images can be treated |
| | 447 | // as RGB. |
| | 448 | if( hasRGB ) |
| | 449 | { |
| | 450 | GDALRasterBand *bandRed, *bandGreen, *bandBlue; |
| | 451 | |
| | 452 | |
| | 453 | bandRed = _gdalDataSet->GetRasterBand(1); |
| | 454 | bandGreen = _gdalDataSet->GetRasterBand(2); |
| | 455 | bandBlue = _gdalDataSet->GetRasterBand(3); |
| | 456 | |
| | 457 | |
| | 458 | bandRed->RasterIO(GF_Read, windowX,_numValuesY-(windowY+windowHeight), |
| | 459 | windowWidth,windowHeight, |
| | 460 | (void*)(tempImage+0),destWidth,destHeight, |
| | 461 | targetGDALType,pixelSpace,pixelSpace*destWidth); |
| | 462 | bandGreen->RasterIO(GF_Read, |
| | 463 | windowX,_numValuesY-(windowY+windowHeight), |
| | 464 | windowWidth,windowHeight, |
| | 465 | (void*)(tempImage+1),destWidth,destHeight, |
| | 466 | targetGDALType,pixelSpace,pixelSpace*destWidth); |
| | 467 | bandBlue->RasterIO(GF_Read, |
| | 468 | windowX,_numValuesY-(windowY+windowHeight), |
| | 469 | windowWidth,windowHeight, |
| | 470 | (void*)(tempImage+2),destWidth,destHeight, |
| | 471 | targetGDALType,pixelSpace,pixelSpace*destWidth); |
| | 472 | } |
| | 473 | |
| | 474 | else if( hasColorTable ) |
| | 475 | { |
| | 476 | // Pseudocolored image. Convert 1 band + color table to 24bit RGB. |
| | 477 | |
| | 478 | GDALRasterBand *band; |
| | 479 | GDALColorTable *ct; |
| | 480 | int i; |
| | 481 | |
| | 482 | |
| | 483 | band = _gdalDataSet->GetRasterBand(1); |
| | 484 | |
| | 485 | |
| | 486 | band->RasterIO(GF_Read, |
| | 487 | windowX,_numValuesY-(windowY+windowHeight), |
| | 488 | windowWidth,windowHeight, |
| | 489 | (void*)(tempImage+0),destWidth,destHeight, |
| | 490 | targetGDALType,pixelSpace,pixelSpace*destWidth); |
| | 491 | |
| | 492 | |
| | 493 | ct = band->GetColorTable(); |
| | 494 | |
| | 495 | |
| | 496 | for( i = 0; i < destWidth * destHeight; i++ ) |
| | 497 | { |
| | 498 | GDALColorEntry sEntry; |
| | 499 | |
| | 500 | |
| | 501 | // default to greyscale equilvelent. |
| | 502 | sEntry.c1 = tempImage[i*3]; |
| | 503 | sEntry.c2 = tempImage[i*3]; |
| | 504 | sEntry.c3 = tempImage[i*3]; |
| | 505 | |
| | 506 | |
| | 507 | ct->GetColorEntryAsRGB( tempImage[i*3], &sEntry ); |
| | 508 | |
| | 509 | |
| | 510 | // Apply RGB back over destination image. |
| | 511 | tempImage[i*3 + 0] = sEntry.c1; |
| | 512 | tempImage[i*3 + 1] = sEntry.c2; |
| | 513 | tempImage[i*3 + 2] = sEntry.c3; |
| | 514 | } |
| | 515 | } |
| | 516 | |
| | 517 | |
| | 518 | else if (hasGreyScale) |
| | 519 | { |
| | 520 | // Greyscale image. Convert 1 band to 24bit RGB. |
| | 521 | GDALRasterBand *band; |
| | 522 | |
| | 523 | |
| | 524 | band = _gdalDataSet->GetRasterBand(1); |
| | 525 | |
| | 526 | |
| | 527 | band->RasterIO(GF_Read, |
| | 528 | windowX,_numValuesY-(windowY+windowHeight), |
| | 529 | windowWidth,windowHeight, |
| | 530 | (void*)(tempImage+0),destWidth,destHeight, |
| | 531 | targetGDALType,pixelSpace,pixelSpace*destWidth); |
| | 532 | band->RasterIO(GF_Read, |
| | 533 | windowX,_numValuesY-(windowY+windowHeight), |
| | 534 | windowWidth,windowHeight, |
| | 535 | (void*)(tempImage+1),destWidth,destHeight, |
| | 536 | targetGDALType,pixelSpace,pixelSpace*destWidth); |
| | 537 | band->RasterIO(GF_Read, |
| | 538 | windowX,_numValuesY-(windowY+windowHeight), |
| | 539 | windowWidth,windowHeight, |
| | 540 | (void*)(tempImage+2),destWidth,destHeight, |
| | 541 | targetGDALType,pixelSpace,pixelSpace*destWidth); |
| | 542 | } |
| | 543 | |
| | 544 | |
| | 545 | // now copy into destination image |
| | 546 | unsigned char* sourceRowPtr = tempImage; |
| | 547 | unsigned int sourceRowDelta = pixelSpace*destWidth; |
| | 548 | unsigned char* destinationRowPtr = imageData; |
| | 549 | unsigned int destinationRowDelta = lineSpace; |
| | 550 | |
| | 551 | for(int row=0; |
| | 552 | row<destHeight; |
| | 553 | ++row, sourceRowPtr+=sourceRowDelta, destinationRowPtr+=destinationRowDelta) |
| | 554 | { |
| | 555 | unsigned char* sourceColumnPtr = sourceRowPtr; |
| | 556 | unsigned char* destinationColumnPtr = destinationRowPtr; |
| | 557 | |
| | 558 | for(int col=0; |
| | 559 | col<destWidth; |
| | 560 | ++col, sourceColumnPtr+=pixelSpace, destinationColumnPtr+=pixelSpace) |