- UID
- 5043
- 积分
- 1347
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-5-13
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Issue
How do I find out the pixel value in a raster image inserted into an AutoCAD
drawing.
Solution
To convert a coordinate in the world coordinates system, you can use the AcDbRasterImage::getPixelToModelTransform() function. Then you will need to use the Atil SDK to access the individual pixels of the image.
Following code demonstrates how to do it:
- 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[0];
- int y = (int)pixpt[1];
- 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;
- }
Please note that this code incorporates only minimal error checking.
|
|