Advertisement
jeanleflambeur

private_impl.cpp

Jan 6th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.68 KB | None | 0 0
  1. /**********************************************************
  2. Software developed by AVA ( Ava Group of the University of Cordoba, ava at uco dot es)
  3. Main author Rafael Munoz Salinas (rmsalinas at uco dot es)
  4. This software is released under BSD license as expressed below
  5. -------------------------------------------------------------------
  6. Copyright (c) 2013, AVA ( Ava Group University of Cordoba, ava at uco dot es)
  7. All rights reserved.
  8.  
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. 1. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. 2. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. 3. All advertising materials mentioning features or use of this software
  18. must display the following acknowledgement:
  19.  
  20. This product includes software developed by the Ava group of the University of Cordoba.
  21.  
  22. 4. Neither the name of the University nor the names of its contributors
  23. may be used to endorse or promote products derived from this software
  24. without specific prior written permission.
  25.  
  26. THIS SOFTWARE IS PROVIDED BY AVA ''AS IS'' AND ANY
  27. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. DISCLAIMED. IN NO EVENT SHALL AVA BE LIABLE FOR ANY
  30. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  33. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. ****************************************************************/
  37.  
  38. #include "private_impl.h"
  39. #include <iostream>
  40. #include <cstdio>
  41. #include "mmal/mmal_util.h"
  42. #include "mmal/mmal_util_params.h"
  43. using namespace std;
  44. namespace raspicam {
  45. namespace _private{
  46. #define MMAL_CAMERA_VIDEO_PORT 1
  47. #define MMAL_CAMERA_CAPTURE_PORT 2
  48. #define VIDEO_FRAME_RATE_DEN 1
  49. #define VIDEO_OUTPUT_BUFFERS_NUM 3
  50.  
  51.  
  52. Private_Impl::Private_Impl() {
  53. camera_video_port = NULL;
  54. // camera_still_port = NULL;
  55. _isOpened=false;
  56. _isCapturing=false;
  57. //set default state params
  58. setDefaultStateParams();
  59. }
  60.  
  61. Private_Impl::~Private_Impl() {
  62.  
  63. release();
  64. }
  65.  
  66. void Private_Impl::setDefaultStateParams() {
  67.  
  68. // Default everything to zero
  69. memset ( &State, 0, sizeof ( RASPIVID_STATE ) );
  70. State.framerate = 30;
  71. State.width = 1280; // use a multiple of 320 (640, 1280)
  72. State.height = 960; // use a multiple of 240 (480, 960)
  73. State.sharpness = 0;
  74. State.contrast = 0;
  75. State.brightness = 50;
  76. State.saturation = 0;
  77. State.ISO = 400;
  78. State.videoStabilisation = false;
  79. State.exposureCompensation = 0;
  80. State.captureFtm=RASPICAM_FORMAT_RGB;
  81. State.rpc_exposureMode = RASPICAM_EXPOSURE_AUTO;
  82. State.rpc_exposureMeterMode = RASPICAM_METERING_AVERAGE;
  83. State.rpc_awbMode = RASPICAM_AWB_AUTO;
  84. State.rpc_imageEffect = RASPICAM_IMAGE_EFFECT_NONE;
  85. State.colourEffects.enable = 0;
  86. State.colourEffects.u = 128;
  87. State.colourEffects.v = 128;
  88. State.rotation = 0;
  89. State.hflip = State.vflip = 0;
  90. State.roi.x = State.roi.y = 0.0;
  91. State.roi.w = State.roi.h = 1.0;
  92. State.shutterSpeed=0;//auto
  93.  
  94. }
  95. bool Private_Impl::open ( bool StartCapture ) {
  96. if ( _isOpened ) return false; //already opened
  97. // create camera
  98. if ( ! create_camera_component ( &State ) ) {
  99. cerr<<__func__<<" Failed to create camera component"<<__FILE__<<" "<<__LINE__<<endl;
  100. return false;
  101. }
  102. commitParameters();
  103. camera_video_port = State.camera_component->output[MMAL_CAMERA_VIDEO_PORT];
  104. callback_data.pstate = &State;
  105. // assign data to use for callback
  106. camera_video_port->userdata = ( struct MMAL_PORT_USERDATA_T * ) &callback_data;
  107.  
  108. _isOpened=true;
  109. if ( StartCapture ) return startCapture();
  110. else return true;
  111. }
  112. /**
  113. */
  114. bool Private_Impl::startCapture() {
  115. if ( !_isOpened ) {
  116. cerr<<__FILE__<<":"<<__LINE__<<":"<<__func__<<" not opened."<<endl;
  117. return false; //already opened
  118. }
  119.  
  120. // start capture
  121. if ( mmal_port_parameter_set_boolean ( camera_video_port, MMAL_PARAMETER_CAPTURE, 1 ) != MMAL_SUCCESS ) {
  122. release();
  123. return false;
  124. }
  125. // Send all the buffers to the video port
  126.  
  127. int num = mmal_queue_length ( State.video_pool->queue );
  128. int q;
  129. for ( q=0; q<num; q++ ) {
  130. MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get ( State.video_pool->queue );
  131.  
  132. if ( !buffer )
  133. cerr<<"Unable to get a required buffer"<<q<<" from pool queue"<<endl;
  134.  
  135. if ( mmal_port_send_buffer ( camera_video_port, buffer ) != MMAL_SUCCESS )
  136. cerr<<"Unable to send a buffer to encoder output port "<< q<<endl;
  137. }
  138. _isCapturing=true;
  139. return true;
  140. }
  141.  
  142. void Private_Impl::release() {
  143. if ( !_isOpened ) return;
  144.  
  145. // Disable camera_video_port
  146. if ( camera_video_port && camera_video_port->is_enabled ) {
  147. mmal_port_disable ( camera_video_port );
  148. camera_video_port = NULL;
  149. }
  150. ////
  151. // Disable all our ports that are not handled by connections
  152. if ( State.camera_component )
  153. mmal_component_disable ( State.camera_component );
  154.  
  155.  
  156. destroy_camera_component ( &State );
  157.  
  158. _isOpened=false;
  159. _isCapturing=false;
  160.  
  161. }
  162. /**
  163. *
  164. */
  165. bool Private_Impl::grab() {
  166. if ( !isCapturing() ) return false;
  167. callback_data.waitForFrame();
  168. return true;
  169. }
  170. /**
  171. *
  172. */
  173. void Private_Impl::retrieve ( unsigned char *data,RASPICAM_FORMAT type ) {
  174. if ( callback_data._buffData.size==0 ) return;
  175. if ( type!=RASPICAM_FORMAT_IGNORE ) {
  176. cerr<<__FILE__<<":"<<__LINE__<<" :Private_Impl::retrieve type is not RASPICAM_FORMAT_IGNORE as it should be"<<endl;
  177. }
  178. if ( State.captureFtm==RASPICAM_FORMAT_BGR ) {//need to swap channels
  179. convertBGR2RGB ( callback_data._buffData.data,data,getImageTypeSize ( State.captureFtm ) );
  180. } else
  181.  
  182. memcpy ( data,callback_data._buffData.data,getImageTypeSize ( State.captureFtm ) );
  183. }
  184.  
  185.  
  186. unsigned char *Private_Impl::getImageBufferData() const{
  187. return callback_data._buffData.data;
  188. }
  189.  
  190. size_t Private_Impl::getImageBufferSize() const{
  191. return getImageTypeSize ( getFormat() );
  192. }
  193.  
  194.  
  195. /**
  196. *
  197. *
  198. */
  199.  
  200. size_t Private_Impl::getImageTypeSize ( RASPICAM_FORMAT type ) const{
  201. switch ( type ) {
  202. case RASPICAM_FORMAT_YUV420:
  203. return getWidth() *getHeight() + 2* ( ( getWidth() /2 *getHeight() /2 ) );
  204. break;
  205. case RASPICAM_FORMAT_GRAY:
  206. return getWidth() *getHeight();
  207. break;
  208. case RASPICAM_FORMAT_BGR:
  209. case RASPICAM_FORMAT_RGB:
  210. return 3*getWidth() *getHeight();
  211. break;
  212. default:
  213. return 0;
  214. };
  215. }
  216.  
  217.  
  218.  
  219. /**
  220. * Destroy the camera component
  221. *
  222. * @param state Pointer to state control struct
  223. *
  224. */
  225. void Private_Impl::destroy_camera_component ( RASPIVID_STATE *state ) {
  226. if ( state->video_pool )
  227. mmal_port_pool_destroy ( state->camera_component->output[MMAL_CAMERA_VIDEO_PORT], state->video_pool );
  228. if ( state->camera_component ) {
  229. mmal_component_destroy ( state->camera_component );
  230. state->camera_component = NULL;
  231. }
  232. }
  233. MMAL_COMPONENT_T *Private_Impl::create_camera_component ( RASPIVID_STATE *state ) {
  234. MMAL_COMPONENT_T *camera = 0;
  235. MMAL_ES_FORMAT_T *format;
  236. MMAL_PORT_T *video_port = NULL;
  237.  
  238. MMAL_STATUS_T status;
  239. /* Create the component */
  240. status = mmal_component_create ( MMAL_COMPONENT_DEFAULT_CAMERA, &camera );
  241.  
  242. if ( status != MMAL_SUCCESS ) {
  243. cerr<< ( "Failed to create camera component" );
  244. return 0;
  245. }
  246.  
  247. if ( !camera->output_num ) {
  248. cerr<< ( "Camera doesn't have output ports" );
  249. mmal_component_destroy ( camera );
  250. return 0;
  251. }
  252.  
  253. video_port = camera->output[MMAL_CAMERA_VIDEO_PORT];
  254.  
  255. // set up the camera configuration
  256.  
  257. MMAL_PARAMETER_CAMERA_CONFIG_T cam_config;
  258. cam_config.hdr.id=MMAL_PARAMETER_CAMERA_CONFIG;
  259. cam_config.hdr.size=sizeof ( cam_config );
  260. cam_config.max_stills_w = state->width;
  261. cam_config.max_stills_h = state->height;
  262. cam_config.stills_yuv422 = 0;
  263. cam_config.one_shot_stills = 0;
  264. cam_config.max_preview_video_w = state->width;
  265. cam_config.max_preview_video_h = state->height;
  266. cam_config.num_preview_video_frames = 3;
  267. cam_config.stills_capture_circular_buffer_height = 0;
  268. cam_config.fast_preview_resume = 0;
  269. cam_config.use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RESET_STC;
  270. mmal_port_parameter_set ( camera->control, &cam_config.hdr );
  271.  
  272. // Set the encode format on the video port
  273.  
  274. format = video_port->format;
  275. format->encoding_variant = convertFormat ( State.captureFtm );
  276. format->encoding = convertFormat ( State.captureFtm );
  277. format->es->video.width = state->width;
  278. format->es->video.height = state->height;
  279. format->es->video.crop.x = 0;
  280. format->es->video.crop.y = 0;
  281. format->es->video.crop.width = state->width;
  282. format->es->video.crop.height = state->height;
  283. format->es->video.frame_rate.num = state->framerate;
  284. format->es->video.frame_rate.den = VIDEO_FRAME_RATE_DEN;
  285.  
  286. status = mmal_port_format_commit ( video_port );
  287. if ( status ) {
  288. cerr<< ( "camera video format couldn't be set" );
  289. mmal_component_destroy ( camera );
  290. return 0;
  291. }
  292.  
  293. // PR : plug the callback to the video port
  294. status = mmal_port_enable ( video_port,video_buffer_callback );
  295. if ( status ) {
  296. cerr<< ( "camera video callback2 error" );
  297. mmal_component_destroy ( camera );
  298. return 0;
  299. }
  300.  
  301. // Ensure there are enough buffers to avoid dropping frames
  302. if ( video_port->buffer_num < VIDEO_OUTPUT_BUFFERS_NUM )
  303. video_port->buffer_num = VIDEO_OUTPUT_BUFFERS_NUM;
  304.  
  305.  
  306.  
  307. //PR : create pool of message on video port
  308. MMAL_POOL_T *pool;
  309. video_port->buffer_size = video_port->buffer_size_recommended;
  310. video_port->buffer_num = video_port->buffer_num_recommended;
  311. pool = mmal_port_pool_create ( video_port, video_port->buffer_num, video_port->buffer_size );
  312. if ( !pool ) {
  313. cerr<< ( "Failed to create buffer header pool for video output port" );
  314. }
  315. state->video_pool = pool;
  316.  
  317.  
  318. /* Enable component */
  319. status = mmal_component_enable ( camera );
  320.  
  321. if ( status ) {
  322. cerr<< ( "camera component couldn't be enabled" );
  323. mmal_component_destroy ( camera );
  324. return 0;
  325. }
  326.  
  327. state->camera_component = camera;//this needs to be before set_all_parameters
  328.  
  329. return camera;
  330. }
  331.  
  332.  
  333. void Private_Impl::commitBrightness() {
  334. mmal_port_parameter_set_rational ( State.camera_component->control, MMAL_PARAMETER_BRIGHTNESS, ( MMAL_RATIONAL_T ) {
  335. State.brightness, 100
  336. } );
  337. }
  338.  
  339.  
  340. void Private_Impl::commitRotation() {
  341. int rotation = int ( State.rotation / 90 ) * 90;
  342. mmal_port_parameter_set_int32 ( State.camera_component->output[0], MMAL_PARAMETER_ROTATION,rotation );
  343. mmal_port_parameter_set_int32 ( State.camera_component->output[1], MMAL_PARAMETER_ROTATION,rotation );
  344. mmal_port_parameter_set_int32 ( State.camera_component->output[2], MMAL_PARAMETER_ROTATION, rotation );
  345. }
  346.  
  347. void Private_Impl::commitISO() {
  348. if ( mmal_port_parameter_set_uint32 ( State.camera_component->control, MMAL_PARAMETER_ISO, State.ISO ) != MMAL_SUCCESS )
  349. cout << __func__ << ": Failed to set ISO parameter.\n";
  350. }
  351.  
  352. void Private_Impl::commitSharpness() {
  353. if ( mmal_port_parameter_set_rational ( State.camera_component->control, MMAL_PARAMETER_SHARPNESS, ( MMAL_RATIONAL_T ) {
  354. State.sharpness, 100
  355. } ) != MMAL_SUCCESS )
  356. cout << __func__ << ": Failed to set sharpness parameter.\n";
  357. }
  358.  
  359. void Private_Impl::commitShutterSpeed() {
  360. if ( mmal_port_parameter_set_uint32 ( State.camera_component->control, MMAL_PARAMETER_SHUTTER_SPEED, State.shutterSpeed ) != MMAL_SUCCESS )
  361. cout << __func__ << ": Failed to set shutter parameter.\n";
  362. }
  363.  
  364.  
  365.  
  366. void Private_Impl::commitContrast() {
  367. if ( mmal_port_parameter_set_rational ( State.camera_component->control, MMAL_PARAMETER_CONTRAST, ( MMAL_RATIONAL_T ) {
  368. State.contrast, 100
  369. } ) != MMAL_SUCCESS )
  370. cout << __func__ << ": Failed to set contrast parameter.\n";
  371. }
  372.  
  373. void Private_Impl::commitSaturation() {
  374. if ( mmal_port_parameter_set_rational ( State.camera_component->control, MMAL_PARAMETER_SATURATION, ( MMAL_RATIONAL_T ) {
  375. State.saturation, 100
  376. } ) != MMAL_SUCCESS )
  377. cout << __func__ << ": Failed to set saturation parameter.\n";
  378. }
  379.  
  380. void Private_Impl::commitExposure() {
  381. MMAL_PARAMETER_EXPOSUREMODE_T exp_mode = {{MMAL_PARAMETER_EXPOSURE_MODE,sizeof ( exp_mode ) }, convertExposure ( State.rpc_exposureMode ) };
  382. if ( mmal_port_parameter_set ( State.camera_component->control, &exp_mode.hdr ) != MMAL_SUCCESS )
  383. cout << __func__ << ": Failed to set exposure parameter.\n";
  384. }
  385. /**
  386. * Adjust the exposure compensation for images (EV)
  387. * @param camera Pointer to camera component
  388. * @param exp_comp Value to adjust, -10 to +10
  389. * @return 0 if successful, non-zero if any parameters out of range
  390. */
  391. void Private_Impl::commitExposureCompensation() {
  392. if ( mmal_port_parameter_set_int32 ( State.camera_component->control, MMAL_PARAMETER_EXPOSURE_COMP , State.exposureCompensation ) !=MMAL_SUCCESS )
  393. cout << __func__ << ": Failed to set Exposure Compensation parameter.\n";
  394.  
  395. }
  396.  
  397.  
  398. void Private_Impl::commitAWB() {
  399. MMAL_PARAMETER_AWBMODE_T param = {{MMAL_PARAMETER_AWB_MODE,sizeof ( param ) }, convertAWB ( State.rpc_awbMode ) };
  400. if ( mmal_port_parameter_set ( State.camera_component->control, &param.hdr ) != MMAL_SUCCESS )
  401. cout << __func__ << ": Failed to set AWB parameter.\n";
  402. }
  403.  
  404. void Private_Impl::commitImageEffect() {
  405. MMAL_PARAMETER_IMAGEFX_T imgFX = {{MMAL_PARAMETER_IMAGE_EFFECT,sizeof ( imgFX ) }, convertImageEffect ( State.rpc_imageEffect ) };
  406. if ( mmal_port_parameter_set ( State.camera_component->control, &imgFX.hdr ) != MMAL_SUCCESS )
  407. cout << __func__ << ": Failed to set image effect parameter.\n";
  408. }
  409.  
  410. void Private_Impl::commitMetering() {
  411. MMAL_PARAMETER_EXPOSUREMETERINGMODE_T meter_mode = {{MMAL_PARAMETER_EXP_METERING_MODE, sizeof ( meter_mode ) }, convertMetering ( State.rpc_exposureMeterMode ) };
  412. if ( mmal_port_parameter_set ( State.camera_component->control, &meter_mode.hdr ) != MMAL_SUCCESS )
  413. cout << __func__ << ": Failed to set metering parameter.\n";
  414. }
  415.  
  416. void Private_Impl::commitFlips() {
  417. MMAL_PARAMETER_MIRROR_T mirror = {{MMAL_PARAMETER_MIRROR, sizeof ( MMAL_PARAMETER_MIRROR_T ) }, MMAL_PARAM_MIRROR_NONE};
  418. if ( State.hflip && State.vflip )
  419. mirror.value = MMAL_PARAM_MIRROR_BOTH;
  420. else if ( State.hflip )
  421. mirror.value = MMAL_PARAM_MIRROR_HORIZONTAL;
  422. else if ( State.vflip )
  423. mirror.value = MMAL_PARAM_MIRROR_VERTICAL;
  424. if ( mmal_port_parameter_set ( State.camera_component->output[0], &mirror.hdr ) != MMAL_SUCCESS ||
  425. mmal_port_parameter_set ( State.camera_component->output[1], &mirror.hdr ) != MMAL_SUCCESS ||
  426. mmal_port_parameter_set ( State.camera_component->output[2], &mirror.hdr ) )
  427. cout << __func__ << ": Failed to set horizontal/vertical flip parameter.\n";
  428. }
  429.  
  430.  
  431. /**
  432. * Set the specified camera to all the specified settings
  433. * @param camera Pointer to camera component
  434. * @param params Pointer to parameter block containing parameters
  435. * @return 0 if successful, none-zero if unsuccessful.
  436. */
  437. void Private_Impl::commitParameters ( ) {
  438. assert ( State.camera_component!=0 );
  439. commitSaturation();
  440. commitSharpness();
  441. commitContrast();
  442. commitBrightness();
  443. commitISO();
  444. if ( State.shutterSpeed!=0 ) {
  445. commitShutterSpeed();
  446. State.rpc_exposureMode=RASPICAM_EXPOSURE_FIXEDFPS;
  447. commitExposure();
  448. } else commitExposure();
  449. commitExposureCompensation();
  450. commitMetering();
  451. commitAWB();
  452. commitImageEffect();
  453. commitRotation();
  454. commitFlips();
  455. commitVideoStabilization();
  456.  
  457. }
  458. void Private_Impl::commitVideoStabilization() {
  459. // Set Video Stabilization
  460. if ( mmal_port_parameter_set_boolean ( State.camera_component->control, MMAL_PARAMETER_VIDEO_STABILISATION, State.videoStabilisation ) != MMAL_SUCCESS )
  461. cout << __func__ << ": Failed to set video stabilization parameter.\n";
  462. }
  463.  
  464.  
  465.  
  466. void Private_Impl::video_buffer_callback ( MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer ) {
  467. MMAL_BUFFER_HEADER_T *new_buffer;
  468. PORT_USERDATA *pData = ( PORT_USERDATA * ) port->userdata;
  469.  
  470. bool hasGrabbed=false;
  471. std::unique_lock<std::mutex> lck ( pData->_mutex );
  472. if ( pData ) {
  473. if ( pData->wantToGrab && buffer->length ) {
  474. mmal_buffer_header_mem_lock ( buffer );
  475. pData->_buffData.resize ( buffer->length );
  476. memcpy ( pData->_buffData.data,buffer->data,buffer->length );
  477. pData->wantToGrab =false;
  478. hasGrabbed=true;
  479. mmal_buffer_header_mem_unlock ( buffer );
  480. }
  481. }
  482. // release buffer back to the pool
  483. mmal_buffer_header_release ( buffer );
  484. // and send one back to the port (if still open)
  485. if ( port->is_enabled ) {
  486. MMAL_STATUS_T status;
  487.  
  488. new_buffer = mmal_queue_get ( pData->pstate->video_pool->queue );
  489.  
  490. if ( new_buffer )
  491. status = mmal_port_send_buffer ( port, new_buffer );
  492.  
  493. if ( !new_buffer || status != MMAL_SUCCESS )
  494. printf ( "Unable to return a buffer to the encoder port" );
  495. }
  496.  
  497. if ( pData->pstate->shutterSpeed!=0 )
  498. mmal_port_parameter_set_uint32 ( pData->pstate->camera_component->control, MMAL_PARAMETER_SHUTTER_SPEED, pData->pstate->shutterSpeed ) ;
  499.  
  500. //pData->_mutex.unlock(); //no need to do this anymore - the Broadcast will unlock the mutex as well
  501. if ( hasGrabbed ) pData->Thcond.BroadCast(); //wake up waiting client
  502. }
  503.  
  504.  
  505.  
  506. void Private_Impl::setWidth ( unsigned int width ) {
  507. State.width = width;
  508. }
  509.  
  510. void Private_Impl::setHeight ( unsigned int height ) {
  511. State.height = height;
  512. }
  513. void Private_Impl::setFormat ( RASPICAM_FORMAT fmt ) {
  514. if ( isOpened() ) {
  515. cerr<<__FILE__<<":"<<__LINE__<<":"<<__func__<<": can not change format with camera already opened"<<endl;
  516. return;
  517. }
  518. State.captureFtm = fmt;
  519. }
  520.  
  521. void Private_Impl::setCaptureSize ( unsigned int width, unsigned int height ) {
  522. setWidth ( width );
  523. setHeight ( height );
  524. }
  525.  
  526. void Private_Impl::setVideoStabilization ( bool v ) {
  527. State.videoStabilisation=v;
  528. if ( isOpened() ) commitVideoStabilization();
  529. }
  530.  
  531. void Private_Impl::setBrightness ( unsigned int brightness ) {
  532. if ( brightness > 100 ) brightness = 100 ;
  533. State.brightness = brightness;
  534. if ( isOpened() ) commitBrightness();
  535. }
  536. void Private_Impl::setShutterSpeed ( unsigned int shutter ) {
  537. if ( shutter > 330000 )
  538. shutter = 330000;
  539. State.shutterSpeed= shutter;
  540. if ( isOpened() ) commitShutterSpeed();
  541. }
  542.  
  543.  
  544.  
  545.  
  546. void Private_Impl::setRotation ( int rotation ) {
  547. while ( rotation < 0 )
  548. rotation += 360;
  549. if ( rotation >= 360 )
  550. rotation = rotation % 360;
  551. State.rotation = rotation;
  552. if ( isOpened() ) commitRotation();
  553. }
  554.  
  555. void Private_Impl::setISO ( int iso ) {
  556. State.ISO = iso;
  557. if ( isOpened() ) commitISO();
  558. }
  559.  
  560. void Private_Impl::setSharpness ( int sharpness ) {
  561. if ( sharpness < -100 ) sharpness = -100;
  562. if ( sharpness > 100 ) sharpness = 100;
  563. State.sharpness = sharpness;
  564. if ( isOpened() ) commitSharpness();
  565. }
  566.  
  567. void Private_Impl::setContrast ( int contrast ) {
  568. if ( contrast < -100 ) contrast = -100;
  569. if ( contrast > 100 ) contrast = 100;
  570. State.contrast = contrast;
  571. if ( isOpened() ) commitContrast();
  572. }
  573.  
  574. void Private_Impl::setSaturation ( int saturation ) {
  575. if ( saturation < -100 ) saturation = -100;
  576. if ( saturation > 100 ) saturation = 100;
  577. State.saturation = saturation;
  578. if ( isOpened() ) commitSaturation();
  579. }
  580.  
  581.  
  582.  
  583. void Private_Impl::setExposure ( RASPICAM_EXPOSURE exposure ) {
  584. State.rpc_exposureMode = exposure;
  585. if ( isOpened() ) commitExposure();
  586. }
  587.  
  588. void Private_Impl::setAWB ( RASPICAM_AWB awb ) {
  589. State.rpc_awbMode = awb;
  590. if ( isOpened() ) commitAWB();
  591. }
  592.  
  593. void Private_Impl::setImageEffect ( RASPICAM_IMAGE_EFFECT imageEffect ) {
  594. State.rpc_imageEffect = imageEffect;
  595. if ( isOpened() ) commitImageEffect();
  596. }
  597.  
  598. void Private_Impl::setMetering ( RASPICAM_METERING metering ) {
  599. State.rpc_exposureMeterMode = metering;
  600. if ( isOpened() ) commitMetering();
  601. }
  602. void Private_Impl::setExposureCompensation ( int val ) {
  603. if ( val < -10 ) val= -10;
  604. if ( val > 10 ) val = 10;
  605. State.exposureCompensation=val;
  606. if ( isOpened() ) commitExposureCompensation();
  607. }
  608.  
  609. void Private_Impl::setHorizontalFlip ( bool hFlip ) {
  610. State.hflip = hFlip;
  611. if ( isOpened() ) commitFlips();
  612. }
  613.  
  614. void Private_Impl::setVerticalFlip ( bool vFlip ) {
  615. State.vflip = vFlip;
  616. if ( isOpened() ) commitFlips();
  617. }
  618.  
  619.  
  620. MMAL_PARAM_EXPOSUREMETERINGMODE_T Private_Impl::convertMetering ( RASPICAM_METERING metering ) {
  621. switch ( metering ) {
  622. case RASPICAM_METERING_AVERAGE:
  623. return MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
  624. case RASPICAM_METERING_SPOT:
  625. return MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
  626. case RASPICAM_METERING_BACKLIT:
  627. return MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
  628. case RASPICAM_METERING_MATRIX:
  629. return MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
  630. default:
  631. return MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
  632. }
  633. }
  634. MMAL_PARAM_EXPOSUREMODE_T Private_Impl::convertExposure ( RASPICAM_EXPOSURE exposure ) {
  635.  
  636. switch ( exposure ) {
  637. case RASPICAM_EXPOSURE_OFF:
  638. return MMAL_PARAM_EXPOSUREMODE_OFF;
  639. case RASPICAM_EXPOSURE_AUTO:
  640. return MMAL_PARAM_EXPOSUREMODE_AUTO;
  641. case RASPICAM_EXPOSURE_NIGHT:
  642. return MMAL_PARAM_EXPOSUREMODE_NIGHT;
  643. case RASPICAM_EXPOSURE_NIGHTPREVIEW:
  644. return MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW;
  645. case RASPICAM_EXPOSURE_BACKLIGHT:
  646. return MMAL_PARAM_EXPOSUREMODE_BACKLIGHT;
  647. case RASPICAM_EXPOSURE_SPOTLIGHT:
  648. return MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT;
  649. case RASPICAM_EXPOSURE_SPORTS:
  650. return MMAL_PARAM_EXPOSUREMODE_SPORTS;
  651. case RASPICAM_EXPOSURE_SNOW:
  652. return MMAL_PARAM_EXPOSUREMODE_SNOW;
  653. case RASPICAM_EXPOSURE_BEACH:
  654. return MMAL_PARAM_EXPOSUREMODE_BEACH;
  655. case RASPICAM_EXPOSURE_VERYLONG:
  656. return MMAL_PARAM_EXPOSUREMODE_VERYLONG;
  657. case RASPICAM_EXPOSURE_FIXEDFPS:
  658. return MMAL_PARAM_EXPOSUREMODE_FIXEDFPS;
  659. case RASPICAM_EXPOSURE_ANTISHAKE:
  660. return MMAL_PARAM_EXPOSUREMODE_ANTISHAKE;
  661. case RASPICAM_EXPOSURE_FIREWORKS:
  662. return MMAL_PARAM_EXPOSUREMODE_FIREWORKS;
  663. default:
  664. return MMAL_PARAM_EXPOSUREMODE_AUTO;
  665. }
  666. }
  667.  
  668. MMAL_PARAM_AWBMODE_T Private_Impl::convertAWB ( RASPICAM_AWB awb ) {
  669. switch ( awb ) {
  670. case RASPICAM_AWB_OFF:
  671. return MMAL_PARAM_AWBMODE_OFF;
  672. case RASPICAM_AWB_AUTO:
  673. return MMAL_PARAM_AWBMODE_AUTO;
  674. case RASPICAM_AWB_SUNLIGHT:
  675. return MMAL_PARAM_AWBMODE_SUNLIGHT;
  676. case RASPICAM_AWB_CLOUDY:
  677. return MMAL_PARAM_AWBMODE_CLOUDY;
  678. case RASPICAM_AWB_SHADE:
  679. return MMAL_PARAM_AWBMODE_SHADE;
  680. case RASPICAM_AWB_TUNGSTEN:
  681. return MMAL_PARAM_AWBMODE_TUNGSTEN;
  682. case RASPICAM_AWB_FLUORESCENT:
  683. return MMAL_PARAM_AWBMODE_FLUORESCENT;
  684. case RASPICAM_AWB_INCANDESCENT:
  685. return MMAL_PARAM_AWBMODE_INCANDESCENT;
  686. case RASPICAM_AWB_FLASH:
  687. return MMAL_PARAM_AWBMODE_FLASH;
  688. case RASPICAM_AWB_HORIZON:
  689. return MMAL_PARAM_AWBMODE_HORIZON;
  690. default:
  691. return MMAL_PARAM_AWBMODE_AUTO;
  692. }
  693. }
  694.  
  695. MMAL_PARAM_IMAGEFX_T Private_Impl::convertImageEffect ( RASPICAM_IMAGE_EFFECT imageEffect ) {
  696. switch ( imageEffect ) {
  697. case RASPICAM_IMAGE_EFFECT_NONE:
  698. return MMAL_PARAM_IMAGEFX_NONE;
  699. case RASPICAM_IMAGE_EFFECT_NEGATIVE:
  700. return MMAL_PARAM_IMAGEFX_NEGATIVE;
  701. case RASPICAM_IMAGE_EFFECT_SOLARIZE:
  702. return MMAL_PARAM_IMAGEFX_SOLARIZE;
  703. case RASPICAM_IMAGE_EFFECT_SKETCH:
  704. return MMAL_PARAM_IMAGEFX_SKETCH;
  705. case RASPICAM_IMAGE_EFFECT_DENOISE:
  706. return MMAL_PARAM_IMAGEFX_DENOISE;
  707. case RASPICAM_IMAGE_EFFECT_EMBOSS:
  708. return MMAL_PARAM_IMAGEFX_EMBOSS;
  709. case RASPICAM_IMAGE_EFFECT_OILPAINT:
  710. return MMAL_PARAM_IMAGEFX_OILPAINT;
  711. case RASPICAM_IMAGE_EFFECT_HATCH:
  712. return MMAL_PARAM_IMAGEFX_HATCH;
  713. case RASPICAM_IMAGE_EFFECT_GPEN:
  714. return MMAL_PARAM_IMAGEFX_GPEN;
  715. case RASPICAM_IMAGE_EFFECT_PASTEL:
  716. return MMAL_PARAM_IMAGEFX_PASTEL;
  717. case RASPICAM_IMAGE_EFFECT_WATERCOLOR:
  718. return MMAL_PARAM_IMAGEFX_WATERCOLOUR;
  719. case RASPICAM_IMAGE_EFFECT_FILM:
  720. return MMAL_PARAM_IMAGEFX_FILM;
  721. case RASPICAM_IMAGE_EFFECT_BLUR:
  722. return MMAL_PARAM_IMAGEFX_BLUR;
  723. case RASPICAM_IMAGE_EFFECT_SATURATION:
  724. return MMAL_PARAM_IMAGEFX_SATURATION;
  725. case RASPICAM_IMAGE_EFFECT_COLORSWAP:
  726. return MMAL_PARAM_IMAGEFX_COLOURSWAP;
  727. case RASPICAM_IMAGE_EFFECT_WASHEDOUT:
  728. return MMAL_PARAM_IMAGEFX_WASHEDOUT;
  729. case RASPICAM_IMAGE_EFFECT_POSTERISE:
  730. return MMAL_PARAM_IMAGEFX_POSTERISE;
  731. case RASPICAM_IMAGE_EFFECT_COLORPOINT:
  732. return MMAL_PARAM_IMAGEFX_COLOURPOINT;
  733. case RASPICAM_IMAGE_EFFECT_COLORBALANCE:
  734. return MMAL_PARAM_IMAGEFX_COLOURBALANCE;
  735. case RASPICAM_IMAGE_EFFECT_CARTOON:
  736. return MMAL_PARAM_IMAGEFX_CARTOON;
  737. default:
  738. return MMAL_PARAM_IMAGEFX_NONE;
  739. }
  740. }
  741.  
  742. int Private_Impl::convertFormat ( RASPICAM_FORMAT fmt ) {
  743. switch ( fmt ) {
  744. case RASPICAM_FORMAT_RGB:
  745. return MMAL_ENCODING_BGR24;
  746. case RASPICAM_FORMAT_BGR:
  747. return MMAL_ENCODING_BGR24;
  748. case RASPICAM_FORMAT_GRAY:
  749. return MMAL_ENCODING_I420;
  750. case RASPICAM_FORMAT_YUV420:
  751. return MMAL_ENCODING_I420;
  752. default:
  753. return MMAL_ENCODING_I420;
  754. }
  755. }
  756.  
  757.  
  758. //Returns an id of the camera. We assume the camera id is the one of the raspberry
  759. //the id is obtained using raspberry serial number obtained in /proc/cpuinfo
  760. string Private_Impl::getId() const{
  761. char serial[1024];
  762. serial[0]='\0';
  763. ifstream file ( "/proc/cpuinfo" );
  764. if ( !file ) {
  765. cerr<<__FILE__<<" "<<__LINE__<<":"<<__func__<<"Could not read /proc/cpuinfo"<<endl;
  766. return serial;
  767. }
  768. //read lines until find serial
  769. bool found=false;
  770. while ( !file.eof() && !found ) {
  771. char line[1024];
  772. file.getline ( line,1024 );
  773. string str ( line );
  774. char aux[100];
  775.  
  776. if ( str.find ( "Serial" ) !=string::npos ) {
  777. if ( sscanf ( line,"%s : %s",aux,serial ) !=2 ) {
  778. cerr<<__FILE__<<" "<<__LINE__<<":"<<__func__<<"Error parsing /proc/cpuinfo"<<endl;
  779. } else found=true;
  780. }
  781. };
  782. return serial;
  783. }
  784.  
  785. void Private_Impl::convertBGR2RGB ( unsigned char * in_bgr,unsigned char * out_rgb,int size ) {
  786. unsigned char *end=in_bgr+size;
  787. unsigned char *in_ptr=in_bgr;
  788. while ( in_ptr<end ) {
  789. swap ( in_ptr[2],in_ptr[0] );
  790. in_ptr+=3;
  791. }
  792. mempcpy ( out_rgb,in_bgr,size );
  793.  
  794.  
  795. }
  796.  
  797. };
  798. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement