| 1304 | | // rcg, added to support Create(). |
|---|
| 1305 | | |
|---|
| 1306 | | PNGDataset& ds = *(PNGDataset*)poDS; |
|---|
| 1307 | | |
|---|
| 1308 | | |
|---|
| 1309 | | // Write the block (or consolidate into multichannel block) |
|---|
| 1310 | | // and then write. |
|---|
| 1311 | | |
|---|
| 1312 | | const GDALDataType dt = this->GetRasterDataType(); |
|---|
| 1313 | | const size_t wordsize = ds.m_nBitDepth / 8; |
|---|
| 1314 | | GDALCopyWords( pvData, dt, wordsize, |
|---|
| 1315 | | ds.m_pabyBuffer + (nBand-1) * wordsize, |
|---|
| 1316 | | dt, ds.nBands * wordsize, |
|---|
| 1317 | | nBlockXSize ); |
|---|
| 1318 | | |
|---|
| 1319 | | // See if we got all the bands. |
|---|
| 1320 | | size_t i; |
|---|
| 1321 | | m_bBandProvided[nBand - 1] = TRUE; |
|---|
| 1322 | | for(i = 0; i < ds.nBands; i++) |
|---|
| 1323 | | { |
|---|
| 1324 | | if(!m_bBandProvided[i]) |
|---|
| 1325 | | return CE_None; |
|---|
| 1326 | | } |
|---|
| 1327 | | |
|---|
| 1328 | | // We received all the bands, so |
|---|
| 1329 | | // reset band flags and write pixels out. |
|---|
| 1330 | | this->reset_band_provision_flags(); |
|---|
| 1331 | | |
|---|
| 1332 | | |
|---|
| 1333 | | // If first block, write out file header. |
|---|
| 1334 | | if(x == 0 && y == 0) |
|---|
| 1335 | | { |
|---|
| 1336 | | CPLErr err = ds.write_png_header(); |
|---|
| 1337 | | if(err != CE_None) |
|---|
| 1338 | | return err; |
|---|
| 1339 | | } |
|---|
| | 1305 | // rcg, added to support Create(). |
|---|
| | 1306 | |
|---|
| | 1307 | PNGDataset& ds = *(PNGDataset*)poDS; |
|---|
| | 1308 | |
|---|
| | 1309 | |
|---|
| | 1310 | // Write the block (or consolidate into multichannel block) |
|---|
| | 1311 | // and then write. |
|---|
| | 1312 | |
|---|
| | 1313 | const GDALDataType dt = this->GetRasterDataType(); |
|---|
| | 1314 | const size_t wordsize = ds.m_nBitDepth / 8; |
|---|
| | 1315 | GDALCopyWords( pvData, dt, wordsize, |
|---|
| | 1316 | ds.m_pabyBuffer + (nBand-1) * wordsize, |
|---|
| | 1317 | dt, ds.nBands * wordsize, |
|---|
| | 1318 | nBlockXSize ); |
|---|
| | 1319 | |
|---|
| | 1320 | // See if we got all the bands. |
|---|
| | 1321 | size_t i; |
|---|
| | 1322 | m_bBandProvided[nBand - 1] = TRUE; |
|---|
| | 1323 | for(i = 0; i < ds.nBands; i++) |
|---|
| | 1324 | { |
|---|
| | 1325 | if(!m_bBandProvided[i]) |
|---|
| | 1326 | return CE_None; |
|---|
| | 1327 | } |
|---|
| | 1328 | |
|---|
| | 1329 | // We received all the bands, so |
|---|
| | 1330 | // reset band flags and write pixels out. |
|---|
| | 1331 | this->reset_band_provision_flags(); |
|---|
| | 1332 | |
|---|
| | 1333 | |
|---|
| | 1334 | // If first block, write out file header. |
|---|
| | 1335 | if(x == 0 && y == 0) |
|---|
| | 1336 | { |
|---|
| | 1337 | CPLErr err = ds.write_png_header(); |
|---|
| | 1338 | if(err != CE_None) |
|---|
| | 1339 | return err; |
|---|
| | 1340 | } |
|---|
| 1383 | | // rcg, added to support Create(). |
|---|
| 1384 | | // We get called even for grayscale files, since some |
|---|
| 1385 | | // formats need a palette even then. PNG doesn't, so |
|---|
| 1386 | | // if a gray palette is given, just ignore it. |
|---|
| 1387 | | |
|---|
| 1388 | | GDALColorEntry sEntry; |
|---|
| 1389 | | for( size_t i = 0; i < poCT->GetColorEntryCount(); i++ ) |
|---|
| 1390 | | { |
|---|
| 1391 | | poCT->GetColorEntryAsRGB( i, &sEntry ); |
|---|
| 1392 | | if( sEntry.c1 != sEntry.c2 || sEntry.c1 != sEntry.c3) |
|---|
| 1393 | | { |
|---|
| 1394 | | CPLErr err = GDALPamRasterBand::SetColorTable(poCT); |
|---|
| 1395 | | if(err != CE_None) |
|---|
| 1396 | | return err; |
|---|
| 1397 | | |
|---|
| 1398 | | PNGDataset& ds = *(PNGDataset*)poDS; |
|---|
| 1399 | | ds.m_nColorType = PNG_COLOR_TYPE_PALETTE; |
|---|
| 1400 | | break; |
|---|
| 1401 | | // band::IWriteBlock will emit color table as part of |
|---|
| 1402 | | // header preceding first block write. |
|---|
| 1403 | | } |
|---|
| 1404 | | } |
|---|
| 1405 | | |
|---|
| 1406 | | return CE_None; |
|---|
| | 1384 | // rcg, added to support Create(). |
|---|
| | 1385 | // We get called even for grayscale files, since some |
|---|
| | 1386 | // formats need a palette even then. PNG doesn't, so |
|---|
| | 1387 | // if a gray palette is given, just ignore it. |
|---|
| | 1388 | |
|---|
| | 1389 | GDALColorEntry sEntry; |
|---|
| | 1390 | for( size_t i = 0; i < poCT->GetColorEntryCount(); i++ ) |
|---|
| | 1391 | { |
|---|
| | 1392 | poCT->GetColorEntryAsRGB( i, &sEntry ); |
|---|
| | 1393 | if( sEntry.c1 != sEntry.c2 || sEntry.c1 != sEntry.c3) |
|---|
| | 1394 | { |
|---|
| | 1395 | CPLErr err = GDALPamRasterBand::SetColorTable(poCT); |
|---|
| | 1396 | if(err != CE_None) |
|---|
| | 1397 | return err; |
|---|
| | 1398 | |
|---|
| | 1399 | PNGDataset& ds = *(PNGDataset*)poDS; |
|---|
| | 1400 | ds.m_nColorType = PNG_COLOR_TYPE_PALETTE; |
|---|
| | 1401 | break; |
|---|
| | 1402 | // band::IWriteBlock will emit color table as part of |
|---|
| | 1403 | // header preceding first block write. |
|---|
| | 1404 | } |
|---|
| | 1405 | } |
|---|
| | 1406 | |
|---|
| | 1407 | return CE_None; |
|---|
| 1416 | | /* -------------------------------------------------------------------- */ |
|---|
| 1417 | | /* Initialize PNG access to the file. */ |
|---|
| 1418 | | /* -------------------------------------------------------------------- */ |
|---|
| 1419 | | |
|---|
| 1420 | | m_hPNG = png_create_write_struct( |
|---|
| 1421 | | PNG_LIBPNG_VER_STRING, NULL, |
|---|
| 1422 | | png_gdal_error, png_gdal_warning ); |
|---|
| 1423 | | |
|---|
| 1424 | | m_psPNGInfo = png_create_info_struct( m_hPNG ); |
|---|
| 1425 | | |
|---|
| 1426 | | png_set_write_fn( m_hPNG, m_fpImage, png_vsi_write_data, png_vsi_flush ); |
|---|
| 1427 | | |
|---|
| 1428 | | png_set_IHDR( m_hPNG, m_psPNGInfo, nRasterXSize, nRasterYSize, |
|---|
| 1429 | | m_nBitDepth, m_nColorType, PNG_INTERLACE_NONE, |
|---|
| 1430 | | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT ); |
|---|
| | 1417 | /* -------------------------------------------------------------------- */ |
|---|
| | 1418 | /* Initialize PNG access to the file. */ |
|---|
| | 1419 | /* -------------------------------------------------------------------- */ |
|---|
| | 1420 | |
|---|
| | 1421 | m_hPNG = png_create_write_struct( |
|---|
| | 1422 | PNG_LIBPNG_VER_STRING, NULL, |
|---|
| | 1423 | png_gdal_error, png_gdal_warning ); |
|---|
| | 1424 | |
|---|
| | 1425 | m_psPNGInfo = png_create_info_struct( m_hPNG ); |
|---|
| | 1426 | |
|---|
| | 1427 | png_set_write_fn( m_hPNG, m_fpImage, png_vsi_write_data, png_vsi_flush ); |
|---|
| | 1428 | |
|---|
| | 1429 | png_set_IHDR( m_hPNG, m_psPNGInfo, nRasterXSize, nRasterYSize, |
|---|
| | 1430 | m_nBitDepth, m_nColorType, PNG_INTERLACE_NONE, |
|---|
| | 1431 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT ); |
|---|
| 1449 | | if( m_nColorType == PNG_COLOR_TYPE_GRAY ) |
|---|
| 1450 | | { |
|---|
| 1451 | | dfNoDataValue = this->GetRasterBand(1)->GetNoDataValue( &bHaveNoData ); |
|---|
| 1452 | | |
|---|
| 1453 | | if ( dfNoDataValue > 0 && dfNoDataValue < 65536 ) |
|---|
| 1454 | | { |
|---|
| 1455 | | sTRNSColor.gray = (png_uint_16) dfNoDataValue; |
|---|
| 1456 | | png_set_tRNS( m_hPNG, m_psPNGInfo, NULL, 0, &sTRNSColor ); |
|---|
| 1457 | | } |
|---|
| 1458 | | } |
|---|
| | 1450 | if( m_nColorType == PNG_COLOR_TYPE_GRAY ) |
|---|
| | 1451 | { |
|---|
| | 1452 | dfNoDataValue = this->GetRasterBand(1)->GetNoDataValue( &bHaveNoData ); |
|---|
| | 1453 | |
|---|
| | 1454 | if ( dfNoDataValue > 0 && dfNoDataValue < 65536 ) |
|---|
| | 1455 | { |
|---|
| | 1456 | sTRNSColor.gray = (png_uint_16) dfNoDataValue; |
|---|
| | 1457 | png_set_tRNS( m_hPNG, m_psPNGInfo, NULL, 0, &sTRNSColor ); |
|---|
| | 1458 | } |
|---|
| | 1459 | } |
|---|
| 1469 | | if( CSLCount(papszValues) >= 3 ) |
|---|
| 1470 | | { |
|---|
| 1471 | | sTRNSColor.red = (png_uint_16) atoi(papszValues[0]); |
|---|
| 1472 | | sTRNSColor.green = (png_uint_16) atoi(papszValues[1]); |
|---|
| 1473 | | sTRNSColor.blue = (png_uint_16) atoi(papszValues[2]); |
|---|
| 1474 | | png_set_tRNS( m_hPNG, m_psPNGInfo, NULL, 0, &sTRNSColor ); |
|---|
| 1475 | | } |
|---|
| 1476 | | |
|---|
| 1477 | | CSLDestroy( papszValues ); |
|---|
| 1478 | | } |
|---|
| 1479 | | // Otherwise, get the nodata value from the bands. |
|---|
| 1480 | | else |
|---|
| 1481 | | { |
|---|
| 1482 | | int bHaveNoData = FALSE; |
|---|
| 1483 | | double dfNoDataValueRed = -1; |
|---|
| 1484 | | double dfNoDataValueGreen = -1; |
|---|
| 1485 | | double dfNoDataValueBlue = -1; |
|---|
| 1486 | | |
|---|
| 1487 | | dfNoDataValueRed = this->GetRasterBand(1)->GetNoDataValue( &bHaveNoData ); |
|---|
| 1488 | | dfNoDataValueGreen= this->GetRasterBand(2)->GetNoDataValue( &bHaveNoData ); |
|---|
| 1489 | | dfNoDataValueBlue = this->GetRasterBand(3)->GetNoDataValue( &bHaveNoData ); |
|---|
| 1490 | | |
|---|
| 1491 | | if ( ( dfNoDataValueRed > 0 && dfNoDataValueRed < 65536 ) && |
|---|
| 1492 | | ( dfNoDataValueGreen > 0 && dfNoDataValueGreen < 65536 ) && |
|---|
| 1493 | | ( dfNoDataValueBlue > 0 && dfNoDataValueBlue < 65536 ) ) |
|---|
| 1494 | | { |
|---|
| 1495 | | sTRNSColor.red = (png_uint_16) dfNoDataValueRed; |
|---|
| 1496 | | sTRNSColor.green = (png_uint_16) dfNoDataValueGreen; |
|---|
| 1497 | | sTRNSColor.blue = (png_uint_16) dfNoDataValueBlue; |
|---|
| 1498 | | png_set_tRNS( m_hPNG, m_psPNGInfo, NULL, 0, &sTRNSColor ); |
|---|
| 1499 | | } |
|---|
| 1500 | | } |
|---|
| | 1470 | if( CSLCount(papszValues) >= 3 ) |
|---|
| | 1471 | { |
|---|
| | 1472 | sTRNSColor.red = (png_uint_16) atoi(papszValues[0]); |
|---|
| | 1473 | sTRNSColor.green = (png_uint_16) atoi(papszValues[1]); |
|---|
| | 1474 | sTRNSColor.blue = (png_uint_16) atoi(papszValues[2]); |
|---|
| | 1475 | png_set_tRNS( m_hPNG, m_psPNGInfo, NULL, 0, &sTRNSColor ); |
|---|
| | 1476 | } |
|---|
| | 1477 | |
|---|
| | 1478 | CSLDestroy( papszValues ); |
|---|
| | 1479 | } |
|---|
| | 1480 | // Otherwise, get the nodata value from the bands. |
|---|
| | 1481 | else |
|---|
| | 1482 | { |
|---|
| | 1483 | int bHaveNoData = FALSE; |
|---|
| | 1484 | double dfNoDataValueRed = -1; |
|---|
| | 1485 | double dfNoDataValueGreen = -1; |
|---|
| | 1486 | double dfNoDataValueBlue = -1; |
|---|
| | 1487 | |
|---|
| | 1488 | dfNoDataValueRed = this->GetRasterBand(1)->GetNoDataValue( &bHaveNoData ); |
|---|
| | 1489 | dfNoDataValueGreen= this->GetRasterBand(2)->GetNoDataValue( &bHaveNoData ); |
|---|
| | 1490 | dfNoDataValueBlue = this->GetRasterBand(3)->GetNoDataValue( &bHaveNoData ); |
|---|
| | 1491 | |
|---|
| | 1492 | if ( ( dfNoDataValueRed > 0 && dfNoDataValueRed < 65536 ) && |
|---|
| | 1493 | ( dfNoDataValueGreen > 0 && dfNoDataValueGreen < 65536 ) && |
|---|
| | 1494 | ( dfNoDataValueBlue > 0 && dfNoDataValueBlue < 65536 ) ) |
|---|
| | 1495 | { |
|---|
| | 1496 | sTRNSColor.red = (png_uint_16) dfNoDataValueRed; |
|---|
| | 1497 | sTRNSColor.green = (png_uint_16) dfNoDataValueGreen; |
|---|
| | 1498 | sTRNSColor.blue = (png_uint_16) dfNoDataValueBlue; |
|---|
| | 1499 | png_set_tRNS( m_hPNG, m_psPNGInfo, NULL, 0, &sTRNSColor ); |
|---|
| | 1500 | } |
|---|
| | 1501 | } |
|---|