找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 137|回复: 0

[每日一码] 如何查询raster image的像素值

[复制链接]

0

主题

0

回帖

28

积分

管理员

积分
28
发表于 2024-3-14 20:23:09 | 显示全部楼层 |阅读模式
  1. void AsdkGetPixelValue()
  2. {
  3. ads采用name name;
  4. ads采用point pt;
  5. Acad::ErrorStatus es;
  6. if (acedEntSel(L"\nSelect a Raster Image : ", name, pt) != RTNORM)
  7. {
  8.   return;
  9. }
  10. AcDbObjectId objId;
  11. acdbGetObjectId(objId, name);
  12. AcDbEntity * pEnt;
  13. es = acdbOpenObject(pEnt, objId, AcDb::kForRead);
  14. AcDbRasterImage *pRaster = AcDbRasterImage::cast(pEnt);
  15. if (pRaster == NULL)
  16. {
  17. pEnt->close();
  18.   return;
  19. }
  20. // Get pixel value
  21. AcGeVector2d imgSize = pRaster->imageSize();
  22. AcGeMatrix3d mat;
  23. es = pRaster->getPixelToModelTransform(mat);
  24. AcGePoint3d modpt;
  25. if (acedGetPoint(NULL, L"\nSelect point in image : ", asDblArray(modpt)) != RTNORM)
  26. {
  27.   return;
  28. }
  29. AcGePoint3d pixpt(0,0,0);
  30. pixpt = mat.inverse() * modpt;
  31. int x = (int)pixpt[0];
  32. int y = (int)pixpt[1];
  33. if ((x > imgSize.x) || (x < 0) || (y > imgSize.y) || (y < 0))
  34. {
  35. acutPrintf(L"\n*** This point is not within the image ***\n");
  36. pRaster->close();
  37.   return;
  38. }
  39. else
  40. {
  41. acutPrintf(L"\nPixel Selected: (%d, %d)", x, y);
  42. }
  43. // Get a copy of the image from the rastier image def
  44. AcDbRasterImageDef * pDef;
  45. es = acdbOpenObject(pDef, pRaster->imageDefId(), AcDb::kForWrite);
  46. Atil::Image* pImg = pDef->imageCopy();
  47. pRaster->close();
  48. pDef->close();
  49. // Find out bits per pixel of image
  50. Atil::ImageContext* imgContext = pImg->createContext(Atil::ImageContext::kRead, pImg->size(), Atil::Offset(0,0));
  51. Atil::DataModelAttributes::BitsPerPixel bpp = pImg->dataModel().bitsPerPixel();
  52. // Show pixel value
  53. switch (bpp)
  54. {
  55. case Atil::DataModelAttributes::BitsPerPixel::k1:
  56. acutPrintf(L"\nPixel value = %u\n", imgContext->get1(x,y));
  57.   break;
  58. case Atil::DataModelAttributes::BitsPerPixel::k8:
  59. acutPrintf(L"\nPixel value = %u\n", imgContext->get8(x,y));
  60.   break;
  61. case Atil::DataModelAttributes::BitsPerPixel::k16:
  62. acutPrintf(L"\nPixel value = %u\n", imgContext->get16(x,y));
  63.   break;
  64. case Atil::DataModelAttributes::BitsPerPixel::k32:
  65. acutPrintf(L"\nPixel value = %u\n", imgContext->get32(x,y));
  66.   break;
  67. case Atil::DataModelAttributes::BitsPerPixel::k64:
  68. acutPrintf(L"\nPixel value = %u\n", imgContext->get64(x,y));
  69.   break;
  70. default:
  71. acutPrintf(L"\n*** This color scale is not supported. ***\n");
  72. }
  73. //Clean up
  74. delete imgContext;
  75. delete pImg;
  76. return;
  77. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|膜结构网

GMT+8, 2024-12-29 05:17 , Processed in 0.147307 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表