• osgEarth示例分析——osgearth_colorfilter


    前言

    osgearth_colorfilter颜色过滤器示例。本示例中,主要展示了6种颜色过滤器的使用,分别是:HSLColorFilter、RGBColorFilter、CMYKColorFilter、BrightnessContrastColorFilter、GammaColorFilter、ChromaKeyColorFilter。

    执行命令

    1. // 一条命令是一次效果
    2. // hsl效果
    3. osgearth_colorfilterd.exe earth_image\china-simple.earth --hsl
    4. // rgb效果
    5. osgearth_colorfilterd.exe earth_image\china-simple.earth --rgb
    6. // cmyk效果
    7. osgearth_colorfilterd.exe earth_image\china-simple.earth --cmyk
    8. // bc效果
    9. osgearth_colorfilterd.exe earth_image\china-simple.earth --bc
    10. // gamma效果
    11. osgearth_colorfilterd.exe earth_image\china-simple.earth --gamma
    12. // chromakey效果
    13. osgearth_colorfilterd.exe earth_image\china-simple.earth --chromakey

    执行效果

    原图

     hsl 随意调整后

    rgb 随意调整后

     cmyk 随意调整后

     bc 随意调整后

     gamma 随意调整后

     chromakey 随意调整后

     代码分析

    关于颜色过滤器,本节涉及到的一些新知识。

    HSLColorFilter:调整色相/饱和度/亮度的滤色器。HSL是一种将RGB色彩模型中的点在圆柱坐标系中的表示法。HSL:色相、饱和度、亮度(英语:Hue, Saturation, Lightness)。有3个值可以修改。

    RGBColorFilter:调整红/绿/蓝的滤色器。有3个值可以修改。

    CMYKColorFilter:CMYK过滤器。CMYK:印刷四色模式,彩色印刷时采用的一种套色模式,利用色料的三原色混色原理,加上黑色油墨,共计四种颜色混合叠加,形成所谓“全彩印刷”。四种标准颜色是:C:Cyan = 青色,又称为‘天蓝色’或是‘湛蓝’;M:Magenta = 品红色,又称为‘洋红色’;Y:Yellow = 黄色;K:blacK=黑色。有4个值可以修改。

    BrightnessContrastColorFilter:亮度对比度 颜色过滤器。调整亮度和对比度。有2个值可以修改。

    GammaColorFilter:伽(ga)马颜色空间过滤器。伽马(Gamma)颜色空间是指线性颜色空间中的像素值经过伽马校正得到的颜色空间。有1个值可以修改。

    ChromaKeyColorFilter:更改RGB颜色透明度的过滤器,包括修改距离值。色度键是一种编辑技术,它将两幅图像合成在一起,形成一幅完整的图像。比如,拍摄视频时,背景色为绿色,后期合成某种其他背景。有4个值可以修改。

    完整代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. using namespace osgEarth;
    15. using namespace osgEarth::Util;
    16. using namespace osgEarth::Util::Controls;
    17. using namespace osgEarth::Symbology;
    18. // 界面ui垂直box
    19. Container*
    20. createControlPanel(osgViewer::View* view)
    21. {
    22. ControlCanvas* canvas = ControlCanvas::getOrCreate(view);
    23. VBox* vbox = canvas->addControl(new VBox());
    24. vbox->setChildSpacing(10);
    25. return vbox;
    26. }
    27. // HSL是一种将RGB色彩模型中的点在圆柱坐标系中的表示法。
    28. // HSL :色相、饱和度、亮度(英语:Hue, Saturation, Lightness)
    29. // HSLColorFilter:调整色相/饱和度/亮度的滤色器。
    30. namespace HSL
    31. {
    32. struct SetHSL: public ControlEventHandler
    33. {
    34. SetHSL(HSLColorFilter* filter, unsigned index) :
    35. _filter(filter), _index(index)
    36. { }
    37. void onValueChanged( Control* control, float value )
    38. {
    39. osg::Vec3f hsl = _filter->getHSLOffset();
    40. hsl[_index] = value;
    41. _filter->setHSLOffset( hsl );
    42. }
    43. HSLColorFilter* _filter;
    44. unsigned _index;
    45. };
    46. // 重置HSL过滤器
    47. struct ResetHSL : public ControlEventHandler
    48. {
    49. ResetHSL(HSliderControl* h, HSliderControl* s, HSliderControl* l)
    50. : _h(h), _s(s), _l(l) { }
    51. void onClick( Control* control )
    52. {
    53. _h->setValue( 0.0 );
    54. _s->setValue( 0.0 );
    55. _l->setValue( 0.0 );
    56. }
    57. HSliderControl* _h;
    58. HSliderControl* _s;
    59. HSliderControl* _l;
    60. };
    61. // 添加控件,网格挂载到container上
    62. void
    63. addControls(HSLColorFilter* filter, Container* container, unsigned i)
    64. {
    65. // the outer container:
    66. // 网格控件
    67. Grid* s_layerBox = container->addControl(new Grid());
    68. s_layerBox->setBackColor(0,0,0,0.5);
    69. s_layerBox->setMargin( 10 );// 外边距
    70. s_layerBox->setPadding( 10 );// 内边距
    71. s_layerBox->setChildSpacing( 10 );
    72. s_layerBox->setChildVertAlign( Control::ALIGN_CENTER );// 子控件垂直居中
    73. s_layerBox->setAbsorbEvents( true );
    74. s_layerBox->setVertAlign( Control::ALIGN_TOP );// 网格控件居上
    75. // Title: 提示Layer信息
    76. s_layerBox->setControl( 0, 0, new LabelControl(Stringify()<<"Layer "<Vec4(1,1,0,1)));
    77. // Hue:色相
    78. LabelControl* hLabel = new LabelControl( "Hue" );
    79. hLabel->setVertAlign( Control::ALIGN_CENTER );
    80. s_layerBox->setControl( 0, 1, hLabel );
    81. // 水平滑块,设置色相,index=0
    82. HSliderControl* hAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new SetHSL(filter,0) );
    83. hAdjust->setWidth( 125 );
    84. hAdjust->setHeight( 12 );
    85. hAdjust->setVertAlign( Control::ALIGN_CENTER );
    86. s_layerBox->setControl( 1, 1, hAdjust );
    87. s_layerBox->setControl( 2, 1, new LabelControl(hAdjust) );// 显示滑块数值
    88. // Saturation: 饱和度
    89. LabelControl* sLabel = new LabelControl( "Saturation" );
    90. sLabel->setVertAlign( Control::ALIGN_CENTER );
    91. s_layerBox->setControl( 0, 2, sLabel );
    92. // 水平滑块,设置饱和度,index=1
    93. HSliderControl* sAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new SetHSL(filter,1) );
    94. sAdjust->setWidth( 125 );
    95. sAdjust->setHeight( 12 );
    96. sAdjust->setVertAlign( Control::ALIGN_CENTER );
    97. s_layerBox->setControl( 1, 2, sAdjust );
    98. s_layerBox->setControl( 2, 2, new LabelControl(sAdjust) );// 显示滑块数值
    99. // Lightness:亮度
    100. LabelControl* lLabel = new LabelControl( "Lightness" );
    101. lLabel->setVertAlign( Control::ALIGN_CENTER );
    102. s_layerBox->setControl( 0, 3, lLabel );
    103. // 水平滑块,设置亮度,index=2
    104. HSliderControl* lAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new SetHSL(filter,2) );
    105. lAdjust->setWidth( 125 );
    106. lAdjust->setHeight( 12 );
    107. lAdjust->setVertAlign( Control::ALIGN_CENTER );
    108. s_layerBox->setControl( 1, 3, lAdjust );
    109. s_layerBox->setControl( 2, 3, new LabelControl(lAdjust) );// 显示滑块数值
    110. // Reset button:重置按钮
    111. LabelControl* resetButton = new LabelControl( "Reset" );
    112. resetButton->setBackColor( osg::Vec4(0.5,0.5,0.5,1) );// 按钮背景色
    113. resetButton->setActiveColor( osg::Vec4(0.5,0.5,1,1) );// 按钮激活色
    114. resetButton->addEventHandler( new ResetHSL(hAdjust, sAdjust, lAdjust) );
    115. s_layerBox->setControl( 1, 4, resetButton );
    116. }
    117. }
    118. // RGB控制颜色过滤器
    119. // RGBColorFilter: 调整红/绿/蓝的滤色器。
    120. namespace RGB
    121. {
    122. struct Set: public ControlEventHandler
    123. {
    124. Set(RGBColorFilter* filter, unsigned index) :
    125. _filter(filter), _index(index)
    126. { }
    127. void onValueChanged( Control* control, float value )
    128. {
    129. osg::Vec3f hsl = _filter->getRGBOffset();// 临时变量hsl换成rgb会更容易理解
    130. hsl[_index] = value;// 根据索引,决定要修改RGB哪一个值
    131. _filter->setRGBOffset( hsl );// 再将更改后的值设置回去
    132. }
    133. RGBColorFilter* _filter;
    134. unsigned _index;
    135. };
    136. // 重置颜色事件
    137. struct Reset : public ControlEventHandler
    138. {
    139. Reset(HSliderControl* r, HSliderControl* g, HSliderControl* b)
    140. : _r(r), _g(g), _b(b) { }
    141. void onClick( Control* control )
    142. {
    143. _r->setValue( 0.0 );
    144. _g->setValue( 0.0 );
    145. _b->setValue( 0.0 );
    146. }
    147. HSliderControl* _r;
    148. HSliderControl* _g;
    149. HSliderControl* _b;
    150. };
    151. void
    152. addControls(RGBColorFilter* filter, Container* container, unsigned i)
    153. {
    154. // the outer container: 网格容器
    155. Grid* s_layerBox = container->addControl(new Grid());
    156. s_layerBox->setBackColor(0,0,0,0.5);
    157. s_layerBox->setMargin( 10 );
    158. s_layerBox->setPadding( 10 );
    159. s_layerBox->setChildSpacing( 10 );
    160. s_layerBox->setChildVertAlign( Control::ALIGN_CENTER );
    161. s_layerBox->setAbsorbEvents( true );
    162. s_layerBox->setVertAlign( Control::ALIGN_TOP );
    163. // Title: 提示Layer信息
    164. s_layerBox->setControl( 0, 0, new LabelControl(Stringify()<<"Layer "<
    165. // Red:红色
    166. LabelControl* rLabel = new LabelControl( "Red" );
    167. rLabel->setVertAlign( Control::ALIGN_CENTER );
    168. s_layerBox->setControl( 0, 1, rLabel );
    169. // 控制红色值的滑块,index=0
    170. HSliderControl* rAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new RGB::Set(filter,0) );
    171. rAdjust->setWidth( 125 );
    172. rAdjust->setHeight( 12 );
    173. rAdjust->setVertAlign( Control::ALIGN_CENTER );
    174. s_layerBox->setControl( 1, 1, rAdjust );
    175. s_layerBox->setControl( 2, 1, new LabelControl(rAdjust) );
    176. // Green:绿色
    177. LabelControl* gLabel = new LabelControl( "Green" );
    178. gLabel->setVertAlign( Control::ALIGN_CENTER );
    179. s_layerBox->setControl( 0, 2, gLabel );
    180. // 控制绿色值的滑块,index=1
    181. HSliderControl* gAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new RGB::Set(filter,1) );
    182. gAdjust->setWidth( 125 );
    183. gAdjust->setHeight( 12 );
    184. gAdjust->setVertAlign( Control::ALIGN_CENTER );
    185. s_layerBox->setControl( 1, 2, gAdjust );
    186. s_layerBox->setControl( 2, 2, new LabelControl(gAdjust) );
    187. // Blue:蓝色
    188. LabelControl* bLabel = new LabelControl( "Blue" );
    189. bLabel->setVertAlign( Control::ALIGN_CENTER );
    190. s_layerBox->setControl( 0, 3, bLabel );
    191. // 控制蓝色值的滑块,index=2
    192. HSliderControl* bAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new RGB::Set(filter,2) );
    193. bAdjust->setWidth( 125 );
    194. bAdjust->setHeight( 12 );
    195. bAdjust->setVertAlign( Control::ALIGN_CENTER );
    196. s_layerBox->setControl( 1, 3, bAdjust );
    197. s_layerBox->setControl( 2, 3, new LabelControl(bAdjust) );
    198. // Reset button,重置按钮
    199. LabelControl* resetButton = new LabelControl( "Reset" );
    200. resetButton->setBackColor( Color::Gray );// 设置背景色
    201. resetButton->setActiveColor( Color::Blue );// 设置激活色
    202. resetButton->addEventHandler( new Reset(rAdjust, gAdjust, bAdjust) );// 重置
    203. s_layerBox->setControl( 1, 4, resetButton );
    204. }
    205. }
    206. // CMYK:印刷四色模式,彩色印刷时采用的一种套色模式,利用色料的三原色混色原理,
    207. // 加上黑色油墨,共计四种颜色混合叠加,形成所谓“全彩印刷”。
    208. // 四种标准颜色是:
    209. // C:Cyan = 青色,又称为‘天蓝色’或是‘湛蓝’;M:Magenta = 品红色,又称为‘洋红色’;Y:Yellow = 黄色;K:blacK=黑色
    210. namespace CMYK
    211. {
    212. struct Set: public ControlEventHandler
    213. {
    214. Set(CMYKColorFilter* filter, unsigned index) :
    215. _filter(filter), _index(index)
    216. { }
    217. void onValueChanged( Control* control, float value )
    218. {
    219. osg::Vec4 cmyk = _filter->getCMYKOffset();// 获取当前cmyk值
    220. cmyk[_index] = value;// 根据索引,修改对应的颜色
    221. _filter->setCMYKOffset( cmyk );// 设置新的cmyk
    222. }
    223. CMYKColorFilter* _filter;
    224. unsigned _index;
    225. };
    226. // 重置CMYK值
    227. struct Reset : public ControlEventHandler
    228. {
    229. Reset(HSliderControl* c, HSliderControl* m, HSliderControl* y, HSliderControl* k)
    230. : _c(c), _m(m), _y(y), _k(k) { }
    231. void onClick( Control* control )
    232. {
    233. _c->setValue( 0.0 );
    234. _m->setValue( 0.0 );
    235. _y->setValue( 0.0 );
    236. _k->setValue( 0.0 );
    237. }
    238. HSliderControl* _c;
    239. HSliderControl* _m;
    240. HSliderControl* _y;
    241. HSliderControl* _k;
    242. };
    243. void
    244. addControls(CMYKColorFilter* filter, Container* container, unsigned i)
    245. {
    246. // the outer container:
    247. Grid* s_layerBox = container->addControl(new Grid());
    248. s_layerBox->setBackColor(0,0,0,0.5);
    249. s_layerBox->setMargin( 10 );
    250. s_layerBox->setPadding( 10 );
    251. s_layerBox->setChildSpacing( 10 );
    252. s_layerBox->setChildVertAlign( Control::ALIGN_CENTER );
    253. s_layerBox->setAbsorbEvents( true );
    254. s_layerBox->setVertAlign( Control::ALIGN_TOP );
    255. // Title:
    256. s_layerBox->setControl( 0, 0, new LabelControl(Stringify()<<"Layer "<
    257. // Cyan:
    258. LabelControl* cLabel = new LabelControl( "Cyan" );
    259. cLabel->setVertAlign( Control::ALIGN_CENTER );
    260. s_layerBox->setControl( 0, 1, cLabel );
    261. HSliderControl* cAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new CMYK::Set(filter,0) );
    262. cAdjust->setWidth( 125 );
    263. cAdjust->setHeight( 12 );
    264. cAdjust->setVertAlign( Control::ALIGN_CENTER );
    265. s_layerBox->setControl( 1, 1, cAdjust );
    266. s_layerBox->setControl( 2, 1, new LabelControl(cAdjust) );
    267. // Magenta:
    268. LabelControl* mLabel = new LabelControl( "Magenta" );
    269. mLabel->setVertAlign( Control::ALIGN_CENTER );
    270. s_layerBox->setControl( 0, 2, mLabel );
    271. HSliderControl* mAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new CMYK::Set(filter,1) );
    272. mAdjust->setWidth( 125 );
    273. mAdjust->setHeight( 12 );
    274. mAdjust->setVertAlign( Control::ALIGN_CENTER );
    275. s_layerBox->setControl( 1, 2, mAdjust );
    276. s_layerBox->setControl( 2, 2, new LabelControl(mAdjust) );
    277. // Yellow
    278. LabelControl* yLabel = new LabelControl( "Yellow" );
    279. yLabel->setVertAlign( Control::ALIGN_CENTER );
    280. s_layerBox->setControl( 0, 3, yLabel );
    281. HSliderControl* yAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new CMYK::Set(filter,2) );
    282. yAdjust->setWidth( 125 );
    283. yAdjust->setHeight( 12 );
    284. yAdjust->setVertAlign( Control::ALIGN_CENTER );
    285. s_layerBox->setControl( 1, 3, yAdjust );
    286. s_layerBox->setControl( 2, 3, new LabelControl(yAdjust) );
    287. // Black
    288. LabelControl* kLabel = new LabelControl( "Black" );
    289. kLabel->setVertAlign( Control::ALIGN_CENTER );
    290. s_layerBox->setControl( 0, 4, kLabel );
    291. HSliderControl* kAdjust = new HSliderControl( -1.0f, 1.0f, 0.0f, new CMYK::Set(filter,3) );
    292. kAdjust->setWidth( 125 );
    293. kAdjust->setHeight( 12 );
    294. kAdjust->setVertAlign( Control::ALIGN_CENTER );
    295. s_layerBox->setControl( 1, 4, kAdjust );
    296. s_layerBox->setControl( 2, 4, new LabelControl(kAdjust) );
    297. // Reset button
    298. LabelControl* resetButton = new LabelControl( "Reset" );
    299. resetButton->setBackColor( Color::Gray );
    300. resetButton->setActiveColor( Color::Blue );
    301. resetButton->addEventHandler( new Reset(cAdjust, mAdjust, yAdjust, kAdjust) );
    302. s_layerBox->setControl( 1, 5, resetButton );
    303. }
    304. }
    305. // BrightnessContrastColorFilter:亮度对比度 颜色过滤器
    306. namespace BC
    307. {
    308. struct Set: public ControlEventHandler
    309. {
    310. Set(BrightnessContrastColorFilter* filter, unsigned index) :
    311. _filter(filter), _index(index)
    312. { }
    313. void onValueChanged( Control* control, float value )
    314. {
    315. osg::Vec2f bc = _filter->getBrightnessContrast();// 获取值
    316. bc[_index] = value;// 根据索引设置值
    317. _filter->setBrightnessContrast( bc );// 将修改后的值设置回过滤器
    318. }
    319. BrightnessContrastColorFilter* _filter;
    320. unsigned _index;
    321. };
    322. // 重置按钮
    323. struct Reset : public ControlEventHandler
    324. {
    325. Reset(HSliderControl* b, HSliderControl* c)
    326. : _b(b), _c(c) { }
    327. void onClick( Control* control )
    328. {
    329. _b->setValue( 1.0f );
    330. _c->setValue( 1.0f );
    331. }
    332. HSliderControl* _b;
    333. HSliderControl* _c;
    334. };
    335. // 网格容器控件
    336. void
    337. addControls(BrightnessContrastColorFilter* filter, Container* container, unsigned i)
    338. {
    339. // the outer container:
    340. Grid* s_layerBox = container->addControl(new Grid());
    341. s_layerBox->setBackColor(0,0,0,0.5);
    342. s_layerBox->setMargin( 10 );
    343. s_layerBox->setPadding( 10 );
    344. s_layerBox->setChildSpacing( 10 );
    345. s_layerBox->setChildVertAlign( Control::ALIGN_CENTER );
    346. s_layerBox->setAbsorbEvents( true );
    347. s_layerBox->setVertAlign( Control::ALIGN_TOP );
    348. // Title: 图层
    349. s_layerBox->setControl( 0, 0, new LabelControl(Stringify()<<"Layer "<
    350. // Brightness:亮度
    351. LabelControl* bLabel = new LabelControl( "Brightness" );
    352. bLabel->setVertAlign( Control::ALIGN_CENTER );
    353. s_layerBox->setControl( 0, 1, bLabel );
    354. // 设置亮度滑块,index=0
    355. HSliderControl* bAdjust = new HSliderControl( 0.0f, 5.0f, 1.0f, new BC::Set(filter,0) );
    356. bAdjust->setWidth( 125 );
    357. bAdjust->setHeight( 12 );
    358. bAdjust->setVertAlign( Control::ALIGN_CENTER );
    359. s_layerBox->setControl( 1, 1, bAdjust );
    360. s_layerBox->setControl( 2, 1, new LabelControl(bAdjust) );
    361. // Contrast:对比度
    362. LabelControl* cLabel = new LabelControl( "Contrast" );
    363. cLabel->setVertAlign( Control::ALIGN_CENTER );
    364. s_layerBox->setControl( 0, 2, cLabel );
    365. // 设置对比度滑块,index=1
    366. HSliderControl* cAdjust = new HSliderControl( 0.0f, 5.0f, 1.0f, new BC::Set(filter,1) );
    367. cAdjust->setWidth( 125 );
    368. cAdjust->setHeight( 12 );
    369. cAdjust->setVertAlign( Control::ALIGN_CENTER );
    370. s_layerBox->setControl( 1, 2, cAdjust );
    371. s_layerBox->setControl( 2, 2, new LabelControl(cAdjust) );
    372. // Reset button: 重置按钮
    373. LabelControl* resetButton = new LabelControl( "Reset" );
    374. resetButton->setBackColor( Color::Gray );
    375. resetButton->setActiveColor( Color::Blue );
    376. resetButton->addEventHandler( new Reset(bAdjust, cAdjust) );
    377. s_layerBox->setControl( 1, 3, resetButton );
    378. }
    379. }
    380. // GAMMA:伽(ga)马颜色空间。
    381. // 伽马(Gamma)颜色空间是指线性颜色空间中的像素值经过伽马校正得到的颜色空间。
    382. namespace GAMMA
    383. {
    384. // 设置颜色
    385. struct Set: public ControlEventHandler
    386. {
    387. Set(GammaColorFilter* filter) : _filter(filter) { }
    388. void onValueChanged( Control* control, float value )
    389. {
    390. _filter->setGamma( value );
    391. }
    392. GammaColorFilter* _filter;
    393. };
    394. // 重置颜色
    395. struct Reset : public ControlEventHandler
    396. {
    397. Reset(HSliderControl* g)
    398. : _g(g) { }
    399. void onClick( Control* control )
    400. {
    401. _g->setValue( 1.0f );
    402. }
    403. HSliderControl* _g;
    404. };
    405. // 网格,添加控件
    406. void
    407. addControls(GammaColorFilter* filter, Container* container, unsigned i)
    408. {
    409. // the outer container: 外层容器
    410. Grid* s_layerBox = container->addControl(new Grid());
    411. s_layerBox->setBackColor(0,0,0,0.5);
    412. s_layerBox->setMargin( 10 );
    413. s_layerBox->setPadding( 10 );
    414. s_layerBox->setChildSpacing( 10 );
    415. s_layerBox->setChildVertAlign( Control::ALIGN_CENTER );
    416. s_layerBox->setAbsorbEvents( true );
    417. s_layerBox->setVertAlign( Control::ALIGN_TOP );
    418. // Title:标题
    419. s_layerBox->setControl( 0, 0, new LabelControl(Stringify()<<"Layer "<
    420. // Gamma: 伽马仅有一个值需要更改
    421. LabelControl* gLabel = new LabelControl( "Gamma" );
    422. gLabel->setVertAlign( Control::ALIGN_CENTER );
    423. s_layerBox->setControl( 0, 1, gLabel );
    424. HSliderControl* gAdjust = new HSliderControl( 0.1f, 3.0f, 1.0f, new GAMMA::Set(filter) );
    425. gAdjust->setWidth( 125 );
    426. gAdjust->setHeight( 12 );
    427. gAdjust->setVertAlign( Control::ALIGN_CENTER );
    428. s_layerBox->setControl( 1, 1, gAdjust );
    429. s_layerBox->setControl( 2, 1, new LabelControl(gAdjust) );
    430. // Reset button
    431. LabelControl* resetButton = new LabelControl( "Reset" );
    432. resetButton->setBackColor( Color::Gray );
    433. resetButton->setActiveColor( Color::Blue );
    434. resetButton->addEventHandler( new Reset(gAdjust) );
    435. s_layerBox->setControl( 1, 3, resetButton );
    436. }
    437. }
    438. // CHROMAKEY:色度键是一种编辑技术,它将两幅图像合成在一起,形成一幅完整的图像。
    439. namespace CHROMAKEY
    440. {
    441. // 设置颜色
    442. struct SetColor: public ControlEventHandler
    443. {
    444. SetColor(ChromaKeyColorFilter* filter, unsigned index) :
    445. _filter(filter), _index(index)
    446. { }
    447. void onValueChanged( Control* control, float value )
    448. {
    449. osg::Vec3f color = _filter->getColor();// 有3个索引的颜色
    450. color[_index] = value;
    451. _filter->setColor( color );
    452. }
    453. ChromaKeyColorFilter* _filter;
    454. unsigned _index;
    455. };
    456. // 设置距离
    457. struct SetDistance: public ControlEventHandler
    458. {
    459. SetDistance(ChromaKeyColorFilter* filter) :
    460. _filter(filter)
    461. { }
    462. void onValueChanged( Control* control, float value )
    463. {
    464. _filter->setDistance( value );
    465. }
    466. ChromaKeyColorFilter* _filter;
    467. };
    468. // 重置颜色和距离
    469. struct Reset : public ControlEventHandler
    470. {
    471. Reset(HSliderControl* r, HSliderControl* g, HSliderControl* b, HSliderControl* distance)
    472. : _r(r), _g(g), _b(b), _distance( distance) { }
    473. void onClick( Control* control )
    474. {
    475. _r->setValue( 0.0 );
    476. _g->setValue( 0.0 );
    477. _b->setValue( 0.0 );
    478. _distance->setValue( 0.0 );
    479. }
    480. HSliderControl* _r;
    481. HSliderControl* _g;
    482. HSliderControl* _b;
    483. HSliderControl* _distance;
    484. };
    485. // 添加控件
    486. void
    487. addControls(ChromaKeyColorFilter* filter, Container* container, unsigned i)
    488. {
    489. // the outer container:外层容器
    490. Grid* s_layerBox = container->addControl(new Grid());
    491. s_layerBox->setBackColor(0,0,0,0.5);
    492. s_layerBox->setMargin( 10 );
    493. s_layerBox->setPadding( 10 );
    494. s_layerBox->setChildSpacing( 10 );
    495. s_layerBox->setChildVertAlign( Control::ALIGN_CENTER );
    496. s_layerBox->setAbsorbEvents( true );
    497. s_layerBox->setVertAlign( Control::ALIGN_TOP );
    498. // Title:标题
    499. s_layerBox->setControl( 0, 0, new LabelControl(Stringify()<<"Layer "<
    500. // Red:红色
    501. LabelControl* rLabel = new LabelControl( "Red" );
    502. rLabel->setVertAlign( Control::ALIGN_CENTER );
    503. s_layerBox->setControl( 0, 1, rLabel );
    504. // 设置红色,index=0
    505. HSliderControl* rAdjust = new HSliderControl( 0.0f, 1.0f, 0.0f, new CHROMAKEY::SetColor(filter,0) );
    506. rAdjust->setWidth( 125 );
    507. rAdjust->setHeight( 12 );
    508. rAdjust->setVertAlign( Control::ALIGN_CENTER );
    509. s_layerBox->setControl( 1, 1, rAdjust );
    510. s_layerBox->setControl( 2, 1, new LabelControl(rAdjust) );
    511. // Green:绿色
    512. LabelControl* gLabel = new LabelControl( "Green" );
    513. gLabel->setVertAlign( Control::ALIGN_CENTER );
    514. s_layerBox->setControl( 0, 2, gLabel );
    515. // 设置绿色,index=1
    516. HSliderControl* gAdjust = new HSliderControl( 0.0f, 1.0f, 0.0f, new CHROMAKEY::SetColor(filter,1) );
    517. gAdjust->setWidth( 125 );
    518. gAdjust->setHeight( 12 );
    519. gAdjust->setVertAlign( Control::ALIGN_CENTER );
    520. s_layerBox->setControl( 1, 2, gAdjust );
    521. s_layerBox->setControl( 2, 2, new LabelControl(gAdjust) );
    522. // Blue:蓝色
    523. LabelControl* bLabel = new LabelControl( "Blue" );
    524. bLabel->setVertAlign( Control::ALIGN_CENTER );
    525. s_layerBox->setControl( 0, 3, bLabel );
    526. // 设置蓝色,index=2
    527. HSliderControl* bAdjust = new HSliderControl( 0.0f, 1.0f, 0.0f, new CHROMAKEY::SetColor(filter,2) );
    528. bAdjust->setWidth( 125 );
    529. bAdjust->setHeight( 12 );
    530. bAdjust->setVertAlign( Control::ALIGN_CENTER );
    531. s_layerBox->setControl( 1, 3, bAdjust );
    532. s_layerBox->setControl( 2, 3, new LabelControl(bAdjust) );
    533. // Distance:距离
    534. LabelControl* distLabel = new LabelControl( "Distance" );
    535. distLabel->setVertAlign( Control::ALIGN_CENTER );
    536. s_layerBox->setControl( 0, 4, distLabel );
    537. // 设置距离
    538. HSliderControl* distAdjust = new HSliderControl( 0.0f, 2.0f, 0.0f, new CHROMAKEY::SetDistance(filter) );
    539. distAdjust->setWidth( 125 );
    540. distAdjust->setHeight( 12 );
    541. distAdjust->setVertAlign( Control::ALIGN_CENTER );
    542. s_layerBox->setControl( 1, 4, distAdjust );
    543. s_layerBox->setControl( 2, 4, new LabelControl(distAdjust) );
    544. // Reset button:重置按钮
    545. LabelControl* resetButton = new LabelControl( "Reset" );
    546. resetButton->setBackColor( Color::Gray );
    547. resetButton->setActiveColor( Color::Blue );
    548. resetButton->addEventHandler( new Reset(rAdjust, gAdjust, bAdjust, distAdjust) );
    549. s_layerBox->setControl( 1, 5, resetButton );
    550. }
    551. }
    552. bool usage( const std::string& msg )
    553. {
    554. OE_WARN << std::endl
    555. << msg << "\n\n"
    556. << "osgearth_colorfilter \n"
    557. << " [--hsl] Use the HSL (hue/saturation/lightness) filter\n"
    558. << " [--rgb] Use the RGB (red/green/blue/alpha) filter\n"
    559. << " [--cmyk] Use the CMYK (cyan/magenta/yellow/black) filter\n"
    560. << " [--bc] Use the Brightness/Contract filter\n"
    561. << " [--gamma] Use the Gamma filter\n"
    562. << " [--chromakey] Use the chromakey filter\n"
    563. << std::endl;
    564. return -1;
    565. }
    566. int
    567. main(int argc, char** argv)
    568. {
    569. osg::ArgumentParser arguments(&argc,argv);
    570. // Which filter?,在cmd窗口执行时,需要输入一种控制器
    571. bool useHSL = arguments.read("--hsl");
    572. bool useRGB = arguments.read("--rgb");
    573. bool useCMYK = arguments.read("--cmyk");
    574. bool useBC = arguments.read("--bc");
    575. bool useGamma = arguments.read("--gamma");
    576. bool useChromaKey = arguments.read("--chromakey");
    577. // 不输入,会退出程序。
    578. if ( !useHSL && !useRGB && !useCMYK && !useBC && !useGamma && !useChromaKey )
    579. {
    580. return usage( "Please select one of the filter options!" );
    581. }
    582. osgViewer::Viewer viewer(arguments);
    583. viewer.setCameraManipulator( new EarthManipulator() );
    584. // load an earth file
    585. osg::Node* node = MapNodeHelper().load(arguments, &viewer);
    586. if ( !node )
    587. return usage( "Unable to load map from earth file!" );
    588. viewer.setSceneData( node );
    589. //Create the control panel,创建控制面板
    590. Container* box = createControlPanel(&viewer);
    591. osgEarth::MapNode* mapNode = osgEarth::MapNode::findMapNode( node );
    592. if ( node )
    593. {
    594. // typedef std::vector< osg::ref_ptr > ImageLayerVector;
    595. ImageLayerVector imageLayers;
    596. mapNode->getMap()->getLayers(imageLayers);
    597. // 需要提供的earth文件,至少有一层图层
    598. if (imageLayers.empty())
    599. {
    600. return usage("Please provide a map with at least one image layer.");
    601. }
    602. // attach color filter to each layer.
    603. // 为每一个图层都绑定颜色过滤器
    604. for (unsigned i = 0; isize(); ++i)
    605. {
    606. ImageLayer* layer = imageLayers[i].get();
    607. // 当前图层,可见
    608. if ( layer->getEnabled() && layer->getVisible() )
    609. {
    610. // 根据输入的过滤器,对图层进行过滤
    611. if ( useHSL )
    612. {
    613. HSLColorFilter* filter = new HSLColorFilter();
    614. layer->addColorFilter( filter );
    615. HSL::addControls( filter, box, i );
    616. }
    617. else if ( useRGB )
    618. {
    619. RGBColorFilter* filter = new RGBColorFilter();
    620. layer->addColorFilter( filter );
    621. RGB::addControls( filter, box, i );
    622. }
    623. else if ( useCMYK )
    624. {
    625. CMYKColorFilter* filter = new CMYKColorFilter();
    626. layer->addColorFilter( filter );
    627. CMYK::addControls( filter, box, i );
    628. }
    629. else if ( useBC )
    630. {
    631. BrightnessContrastColorFilter* filter = new BrightnessContrastColorFilter();
    632. layer->addColorFilter( filter );
    633. BC::addControls( filter, box, i );
    634. }
    635. else if ( useGamma )
    636. {
    637. GammaColorFilter* filter = new GammaColorFilter();
    638. layer->addColorFilter( filter );
    639. GAMMA::addControls( filter, box, i );
    640. }
    641. else if ( useChromaKey )
    642. {
    643. ChromaKeyColorFilter* filter = new ChromaKeyColorFilter();
    644. layer->addColorFilter( filter );
    645. CHROMAKEY::addControls( filter, box , i );
    646. }
    647. }
    648. }
    649. }
    650. return viewer.run();
    651. }

  • 相关阅读:
    【论文笔记】基于视觉特征提取的强化学习自动驾驶系统
    使用Python进行健身手表数据分析
    活动图高阶讲解-03
    王道数据结构二叉树算法大题代码总结
    广州搬砖第三年,从一枚小菜鸡到架构师
    数组、二维数组及数组的排序(JAVA基础二)
    RabbitMQ(消息队列)
    CCSP(Certified Cloud Security Professional) 国际注册云安全专家
    C++ 多態筆記
    java基础01
  • 原文地址:https://blog.csdn.net/qq_34732729/article/details/128198682