[每日一码] 如何查询raster image的像素值
void AsdkGetPixelValue(){
ads采用name name;
ads采用point pt;
Acad::ErrorStatus es;
if (acedEntSel(L"\nSelect a Raster Image : ", name, pt) != RTNORM)
{
return;
}
AcDbObjectId objId;
acdbGetObjectId(objId, name);
AcDbEntity * pEnt;
es = acdbOpenObject(pEnt, objId, AcDb::kForRead);
AcDbRasterImage *pRaster = AcDbRasterImage::cast(pEnt);
if (pRaster == NULL)
{
pEnt->close();
return;
}
// Get pixel value
AcGeVector2d imgSize = pRaster->imageSize();
AcGeMatrix3d mat;
es = pRaster->getPixelToModelTransform(mat);
AcGePoint3d modpt;
if (acedGetPoint(NULL, L"\nSelect point in image : ", asDblArray(modpt)) != RTNORM)
{
return;
}
AcGePoint3d pixpt(0,0,0);
pixpt = mat.inverse() * modpt;
int x = (int)pixpt;
int y = (int)pixpt;
if ((x > imgSize.x) || (x < 0) || (y > imgSize.y) || (y < 0))
{
acutPrintf(L"\n*** This point is not within the image ***\n");
pRaster->close();
return;
}
else
{
acutPrintf(L"\nPixel Selected: (%d, %d)", x, y);
}
// Get a copy of the image from the rastier image def
AcDbRasterImageDef * pDef;
es = acdbOpenObject(pDef, pRaster->imageDefId(), AcDb::kForWrite);
Atil::Image* pImg = pDef->imageCopy();
pRaster->close();
pDef->close();
// Find out bits per pixel of image
Atil::ImageContext* imgContext = pImg->createContext(Atil::ImageContext::kRead, pImg->size(), Atil::Offset(0,0));
Atil::DataModelAttributes::BitsPerPixel bpp = pImg->dataModel().bitsPerPixel();
// Show pixel value
switch (bpp)
{
case Atil::DataModelAttributes::BitsPerPixel::k1:
acutPrintf(L"\nPixel value = %u\n", imgContext->get1(x,y));
break;
case Atil::DataModelAttributes::BitsPerPixel::k8:
acutPrintf(L"\nPixel value = %u\n", imgContext->get8(x,y));
break;
case Atil::DataModelAttributes::BitsPerPixel::k16:
acutPrintf(L"\nPixel value = %u\n", imgContext->get16(x,y));
break;
case Atil::DataModelAttributes::BitsPerPixel::k32:
acutPrintf(L"\nPixel value = %u\n", imgContext->get32(x,y));
break;
case Atil::DataModelAttributes::BitsPerPixel::k64:
acutPrintf(L"\nPixel value = %u\n", imgContext->get64(x,y));
break;
default:
acutPrintf(L"\n*** This color scale is not supported. ***\n");
}
//Clean up
delete imgContext;
delete pImg;
return;
}
页:
[1]