Beispiel #1
0
static SEXP
GDALColorTable2Matrix(GDALColorTableH ctab) {

        installErrorHandler();
	int ncol = GDALGetColorEntryCount(ctab);
        uninstallErrorHandlerAndTriggerError();

	SEXP cmat = allocMatrix(INTSXP, ncol, 4);

        installErrorHandler();
	for (int i = 0; i < ncol; ++i) {

    	const GDALColorEntry* ce = GDALGetColorEntry(ctab, i);

    	INTEGER(cmat)[i] = static_cast<int>(ce->c1);
    	INTEGER(cmat)[i + ncol] = static_cast<int>(ce->c2);
    	INTEGER(cmat)[i + 2 * ncol] = static_cast<int>(ce->c3);
    	INTEGER(cmat)[i + 3 * ncol] = static_cast<int>(ce->c4);

  	}
        uninstallErrorHandlerAndTriggerError();

  	return(cmat);
	
}
/**
 * @param theBandNumber the number of the band for which you want a color table
 * @param theList a pointer the object that will hold the color table
 * @return true of a color table was able to be read, false otherwise
 */
QList<QgsColorRampShader::ColorRampItem> QgsGdalProviderBase::colorTable( GDALDatasetH gdalDataset, int theBandNumber )const
{
  QgsDebugMsg( "entered." );
  QList<QgsColorRampShader::ColorRampItem> ct;

  //Invalid band number, segfault prevention
  if ( 0 >= theBandNumber )
  {
    QgsDebugMsg( "Invalid parameter" );
    return ct;
  }

  GDALRasterBandH myGdalBand = GDALGetRasterBand( gdalDataset, theBandNumber );
  GDALColorTableH myGdalColorTable = GDALGetRasterColorTable( myGdalBand );

  if ( myGdalColorTable )
  {
    QgsDebugMsg( "Color table found" );

    // load category labels
    char ** categoryNames = GDALGetRasterCategoryNames( myGdalBand );
    QVector<QString> labels;
    if ( categoryNames )
    {
      int i = 0;
      while ( categoryNames[i] )
      {
        labels.append( QString( categoryNames[i] ) );
        i++;
      }
    }

    int myEntryCount = GDALGetColorEntryCount( myGdalColorTable );
    GDALColorInterp myColorInterpretation =  GDALGetRasterColorInterpretation( myGdalBand );
    QgsDebugMsg( "Color Interpretation: " + QString::number(( int )myColorInterpretation ) );
    GDALPaletteInterp myPaletteInterpretation  = GDALGetPaletteInterpretation( myGdalColorTable );
    QgsDebugMsg( "Palette Interpretation: " + QString::number(( int )myPaletteInterpretation ) );

    const GDALColorEntry* myColorEntry = 0;
    for ( int myIterator = 0; myIterator < myEntryCount; myIterator++ )
    {
      myColorEntry = GDALGetColorEntry( myGdalColorTable, myIterator );

      if ( !myColorEntry )
      {
        continue;
      }
      else
      {
        QString label = labels.value( myIterator );
        if ( label.isEmpty() )
        {
          label = QString::number( myIterator );
        }
        //Branch on the color interpretation type
        if ( myColorInterpretation == GCI_GrayIndex )
        {
          QgsColorRampShader::ColorRampItem myColorRampItem;
          myColorRampItem.value = ( double )myIterator;
          myColorRampItem.label = label;
          myColorRampItem.color = QColor::fromRgb( myColorEntry->c1, myColorEntry->c1, myColorEntry->c1, myColorEntry->c4 );
          ct.append( myColorRampItem );
        }
        else if ( myColorInterpretation == GCI_PaletteIndex )
        {
          QgsColorRampShader::ColorRampItem myColorRampItem;
          myColorRampItem.value = ( double )myIterator;
          myColorRampItem.label = label;
          //Branch on palette interpretation
          if ( myPaletteInterpretation  == GPI_RGB )
          {
            myColorRampItem.color = QColor::fromRgb( myColorEntry->c1, myColorEntry->c2, myColorEntry->c3, myColorEntry->c4 );
          }
          else if ( myPaletteInterpretation  == GPI_CMYK )
          {
            myColorRampItem.color = QColor::fromCmyk( myColorEntry->c1, myColorEntry->c2, myColorEntry->c3, myColorEntry->c4 );
          }
          else if ( myPaletteInterpretation  == GPI_HLS )
          {
            myColorRampItem.color = QColor::fromHsv( myColorEntry->c1, myColorEntry->c3, myColorEntry->c2, myColorEntry->c4 );
          }
          else
          {
            myColorRampItem.color = QColor::fromRgb( myColorEntry->c1, myColorEntry->c1, myColorEntry->c1, myColorEntry->c4 );
          }
          ct.append( myColorRampItem );
        }
        else
        {
          QgsDebugMsg( "Color interpretation type not supported yet" );
          return ct;
        }
      }
    }
  }
  else
  {
    QgsDebugMsg( "No color table found for band " + QString::number( theBandNumber ) );
    return ct;
  }

  QgsDebugMsg( "Color table loaded successfully" );
  return ct;
}
Beispiel #3
0
void toprsGadlReader::populateLut()
{
	theLut.reset(); // toprsRefPtr not a leak.
	if(isIndexed(1)&&theDataset)
	{
		GDALColorTableH aTable = GDALGetRasterColorTable(GDALGetRasterBand( theDataset, 1 ));
		GDALPaletteInterp interp = GDALGetPaletteInterpretation(aTable);
		if(aTable && ( (interp == GPI_Gray) || (interp == GPI_RGB)))
		{
			GDALColorEntry colorEntry;
			int numberOfElements = GDALGetColorEntryCount(aTable);
			int idx = 0;
			if(numberOfElements)
			{
				// GPI_Gray Grayscale (in GDALColorEntry.c1)
				// GPI_RGB Red, Green, Blue and Alpha in (in c1, c2, c3 and c4)
				theLut.reset(new toprsNBandLutDataObject(numberOfElements,4,TOPRS_UINT8,-1));
				bool nullSet = false;
				for(idx = 0; idx < numberOfElements; ++idx)
				{
					switch(interp)
					{
					case GPI_RGB:
						{
							if(GDALGetColorEntryAsRGB(aTable, idx, &colorEntry))
							{
								(*theLut)[idx][0] = colorEntry.c1;
								(*theLut)[idx][1] = colorEntry.c2;
								(*theLut)[idx][2] = colorEntry.c3;
								(*theLut)[idx][3] = colorEntry.c4;

								if ( !nullSet )
								{
									if ( m_preservePaletteIndexesFlag )
									{
										// If preserving palette set the null to the fix alpha of 0.
										if ( (*theLut)[idx][3] == 0 )
										{
											theLut->setNullPixelIndex(idx);
											nullSet = true;
										}
									}
									else
									{
										//---
										// Not using alpha.
										// Since the alpha is currently not used, look for the null
										// pixel index and set if we find. If at some point the alpha
										// is taken out this can be removed.
										//---
										if ( ( (*theLut)[idx][0] == 0 ) &&
											( (*theLut)[idx][1] == 0 ) &&
											( (*theLut)[idx][2] == 0 ) )
										{
											theLut->setNullPixelIndex(idx);
											nullSet = true;
										}
									}
								}
							}
							else
							{
								(*theLut)[idx][0] = 0;
								(*theLut)[idx][1] = 0;
								(*theLut)[idx][2] = 0;
								(*theLut)[idx][3] = 0;

								// Look for the null pixel index and set if we find.
								if ( !nullSet )
								{
									if ( (*theLut)[idx][0] == 0 )
									{
										theLut->setNullPixelIndex(idx);
									}
								}
							}
							break;
						}
					case GPI_Gray:
						{
							const GDALColorEntry* constEntry =  GDALGetColorEntry(aTable, idx);
							if(constEntry)
							{
								(*theLut)[idx][0] = constEntry->c1;
							}
							else
							{
								(*theLut)[idx][0] = 0;
							}
							break;
						}
					default:
						{
							break;
						}
					}
				}
			}
		}

		toprs_uint32 rasterCount = GDALGetRasterCount(theDataset);
		for(toprs_uint32 aGdalBandIndex=1; aGdalBandIndex <= rasterCount; ++aGdalBandIndex)
		{
			GDALRasterBandH aBand = GDALGetRasterBand( theDataset, aGdalBandIndex );
			if (aBand)
			{
				GDALRasterAttributeTableH hRAT = GDALGetDefaultRAT(aBand);
				int colCount = GDALRATGetColumnCount(hRAT);
				for (toprs_int32 col = 0; col < colCount; col++)
				{
					const char* colName = GDALRATGetNameOfCol(hRAT, col);
					if (colName)
					{
						if (strcmp(colName, "Class_Names") == 0)
						{
							std::vector<std::string> entryLabels;
							toprs_int32 rowCount = GDALRATGetRowCount(hRAT);
							for (toprs_int32 row = 0; row < rowCount; row++)
							{
								const char* className = GDALRATGetValueAsString(hRAT, row, col);
								std::string entryLabel(className);
								entryLabels.push_back(entryLabel);
							}
							theLut->setEntryLables(aGdalBandIndex-1, entryLabels);
						}
					}
				}
			}
		}
	}
}