You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

429 lines
17 KiB

5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseNodeSensor.h"
  2. #include <opencv2/opencv.hpp>
  3. #define LOG( X ) m_pLog->Log( "NODSEN", this, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "NODSEN", this, _LOG_ERROR, X )
  5. argusNodeSensor::argusNodeSensor( uint32_t id ):argusNetNode( id ),m_oPosition(){
  6. m_pLog = basicLogCollection::getInstance();
  7. m_pTargetFilter = NULL;
  8. };
  9. argusNodeSensor::argusNodeSensor( DBC_NODE_DATA *Data ):argusNetNode( Data ),m_oPosition(){
  10. m_pLog = basicLogCollection::getInstance();
  11. m_pTargetFilter = NULL;
  12. };
  13. argusNodeSensor::~argusNodeSensor(){
  14. };
  15. #define SKIP_LINE_PIXELS 28
  16. #define HEIGHT 512
  17. #define WIDTH 1024
  18. // scale factor -> 4/9
  19. bool argusNodeSensor::handleDeviceDraw( uint8_t kID, void *th_data, size_t th_size, bool byMinimum ){
  20. LOG("TRY SAVE");
  21. if( th_data == NULL ) return false;
  22. if( th_size == 0 ) return false;
  23. if( m_pDBC == NULL ) return false;
  24. LOG("SAVE");
  25. CSTR old = m_pDBC->getLRFDraw( getNodeID() );
  26. if ( old ) {
  27. // delete old draw
  28. LOG("DELETE OLD DRAW");
  29. char filename[256];
  30. sprintf( filename, "data/device_draws/%s_100.png", old );
  31. if( fileExist( filename ) ){
  32. unlink( filename );
  33. };
  34. sprintf( filename, "data/device_draws/%s_map.png", old );
  35. if( fileExist( filename ) ){
  36. unlink( filename );
  37. };
  38. free(old);
  39. } else {
  40. LOG("NO OLD DRAW");
  41. }
  42. CSTR uid = getUniqID_MD5();
  43. if( uid == NULL ) return false;
  44. switch (getNodeType()) {
  45. case ARGUS_ALS_SENSOR:
  46. case ARGUS_LFS_SENSOR:
  47. case ARGUS_PLS_200_SENSOR:
  48. case ARGUS_PLS_360_SENSOR:
  49. case ARGUS_NET_PLS_200:{
  50. break;
  51. };
  52. case ARGUS_3D_SENSOR:{
  53. char * emptyBG = "data/assets/images/empty_3d.png";
  54. thresholdNode * ptr = (thresholdNode *) th_data;
  55. LOG("SAVE 3D DRAW!");
  56. if ( fileExist( emptyBG ) ) {
  57. LOG("BG Exists");
  58. cv::Mat tImage = cv::imread( emptyBG, cv::IMREAD_UNCHANGED );
  59. if( tImage.rows != 512 || tImage.cols != 1024 ){
  60. LOG_ERROR( "Background Image wrong size (should be 1024x512)!" );
  61. free( uid );
  62. return false;
  63. } else LOG("BG READ");
  64. uint8_t * tMemory = tImage.data;
  65. thresholdNode * cNode = NULL;
  66. uint8_t * cPixel = NULL;
  67. int pix = 0;
  68. int mem = 0;
  69. bool s_bit = false;
  70. bool no_data = false;
  71. float tVal;
  72. bool master = ( kID < 16 );
  73. LOG("Start Draw ");
  74. for( int idx = 0; idx < ( TH_SIZE_NODES << 2 ); idx++ ) { // tik-tak
  75. if( ( idx % 4 ) == 0 ){
  76. mem++;
  77. if ( master ) {
  78. cNode = ptr + TH_SIZE_NODES - mem - 1;
  79. } else {
  80. cNode = ptr + mem - 1;
  81. };
  82. if( !s_bit ) s_bit = cNode->values.sbit;
  83. if( !no_data ) no_data = ( cNode->values.cnt == 0 );
  84. if( !s_bit && !no_data ){
  85. if( byMinimum ) {
  86. tVal = ((float)cNode->values.min) / 4096.f;
  87. } else {
  88. tVal = ((float)cNode->values.max) / 4096.f;
  89. };
  90. };
  91. };
  92. if( ( idx % 9 ) == 0 ){
  93. if( mem > ( SKIP_LINE_PIXELS * TH_LINE_NODES ) ) {
  94. cPixel = tMemory + (pix << 2);
  95. pix++;
  96. if( s_bit ) { // Set Red
  97. cPixel[0] = 0x00; cPixel[1] = 0x00; cPixel[2] = 0xff; cPixel[3] = 0xff;
  98. } else {
  99. if( no_data ) { // Set Blue
  100. cPixel[0] = 0xff; cPixel[1] = 0x00; cPixel[2] = 0x00; cPixel[3] = 0xff;
  101. } else {
  102. uint32_t tColor;
  103. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  104. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  105. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  106. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  107. cPixel[3] = tColor & 0xFF;
  108. };
  109. };
  110. s_bit = false;
  111. no_data = false;
  112. };
  113. };
  114. };
  115. char filename[256];
  116. sprintf( filename, "data/device_draws/%s_100.png", uid );
  117. //int RetVal = stbi_write_png( filename, 1024, 512, cSpace, tMemory, 1024 * cSpace );
  118. vector<int> compression_params;
  119. compression_params.push_back( cv::IMWRITE_PNG_COMPRESSION );
  120. compression_params.push_back(9);
  121. if(! cv::imwrite( filename, tImage, compression_params ) ){
  122. tImage.release();
  123. free( uid );
  124. return false;
  125. };
  126. tImage.release();
  127. LOG("Draw Saved!");
  128. cv::Mat tMap( Q_SCAN_COUNT, TH_LINE_NODES, CV_8UC4 );
  129. LOG("Map Creating");
  130. tMemory = tMap.data;
  131. for( int j = 0; j < Q_SCAN_COUNT; j++ ){
  132. for( int i = 0; i < TH_LINE_NODES; i++ ){
  133. //cNode = ptr+(j*i);
  134. //cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
  135. int ids = 0;
  136. int idd = 0;
  137. if ( master ) {
  138. //cNode = ptr+makeIds( i, j , true );
  139. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  140. ids = makeIds( i, j , true );
  141. idd = 4 * makeIds( i, j , false );
  142. //cNode = ptr + ( i * j );
  143. //cPixel = tMemory + ( cSpace * i * j );
  144. } else {
  145. //cNode = ptr+makeIds( i, j , false );
  146. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  147. ids = makeIds( i, j , false );
  148. idd = 4 * makeIds( i, j , false );
  149. };
  150. cNode = ptr + ids ;
  151. cPixel = tMemory + idd;
  152. if( 0 ) { // Monochrome
  153. #ifdef CHECK_SUN_BIT
  154. if( cNode->values.sbit ) {
  155. cPixel[0] = 0;
  156. } else {
  157. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  158. };
  159. #else
  160. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  161. #endif
  162. } else {
  163. #ifdef CHECK_SUN_BIT
  164. if( cNode->values.sbit ) {
  165. // Set Red
  166. cPixel[0] = 0x00;
  167. cPixel[1] = 0x00;
  168. cPixel[2] = 0xff;
  169. cPixel[3] = 0xff;
  170. } else {
  171. if( cNode->values.cnt == 0 ) {
  172. // Set Blue
  173. cPixel[0] = 0xff;
  174. cPixel[1] = 0x00;
  175. cPixel[2] = 0x00;
  176. cPixel[3] = 0xff;
  177. } else {
  178. uint32_t tColor;
  179. float tVal = ((float) cNode->values.min ) / 4096.f;
  180. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  181. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  182. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  183. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  184. cPixel[3] = tColor & 0xFF;
  185. };
  186. };
  187. #else
  188. if( cNode->values.cnt == 0 ) {
  189. // Set Blue
  190. cPixel[0] = 0xff;
  191. cPixel[1] = 0x00;
  192. cPixel[2] = 0x00;
  193. cPixel[3] = 0xff;
  194. } else {
  195. uint32_t tColor;
  196. float tVal = ((float) cNode->values.min ) / 4096.f;
  197. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  198. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  199. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  200. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  201. cPixel[3] = tColor & 0xFF;
  202. };
  203. #endif
  204. };
  205. };
  206. };
  207. sprintf( filename, "data/device_draws/%s_map.png", uid );
  208. LOG(filename);
  209. if(! cv::imwrite( filename, tMap, compression_params ) ){
  210. tMap.release();
  211. free( uid );
  212. return false;
  213. };
  214. tMap.release();
  215. m_pDBC->addLRFDraw( getNodeID(), TH_SIZE_NODES, uid, kID, 1, true );
  216. } else {
  217. LOG_ERROR( "Background Image is Missed!" );
  218. free( uid );
  219. return false;
  220. };
  221. break;
  222. };
  223. };
  224. free( uid );
  225. return true;
  226. };
  227. /*
  228. #define STB_IMAGE_IMPLEMENTATION
  229. #include "../data/stbi/stb_image.h"
  230. #define STB_IMAGE_WRITE_IMPLEMENTATION
  231. #include "../data/stbi/stb_image_write.h"
  232. #undef STB_IMAGE_WRITE_IMPLEMENTATION
  233. #undef STB_IMAGE_IMPLEMENTATION
  234. bool argus3DLIDAR::saveTHPicture( thresholdNode *ptr, CSTR uid, bool master, bool byMinimum, bool monochrome ){
  235. uint8_t *tMemory = NULL;
  236. thresholdNode *cNode = NULL;
  237. uint8_t *cPixel = NULL;
  238. int cSpace;
  239. int x, y;
  240. tMemory = stbi_load( "data/assets/images/empty_3d.png", &x, &y, &cSpace, STBI_default );
  241. if( tMemory == NULL ){
  242. LOG_ERROR("Draw Memory Alloc Eror!");
  243. return false;
  244. };
  245. if( x != 1024 || y != 512 ){
  246. LOG("Wrong IMG Size!");
  247. free( tMemory );
  248. return NULL;
  249. };
  250. int pix = 0;
  251. int mem = 0;
  252. bool s_bit = false;
  253. bool no_data = false;
  254. float tVal;
  255. for ( int idx = 0; idx < ( TH_SIZE_NODES << 2 ); idx++ ) { // tik-tak
  256. if( ( idx % 4 ) == 0 ){
  257. mem++;
  258. if ( master ) {
  259. cNode = ptr + TH_SIZE_NODES - mem - 1;
  260. } else {
  261. cNode = ptr + mem - 1;
  262. };
  263. if( !s_bit ) s_bit = cNode->values.sbit;
  264. if( !no_data ) no_data = ( cNode->values.cnt == 0 );
  265. if( !s_bit && !no_data ){
  266. if( byMinimum ) {
  267. tVal = ((float)cNode->values.min) / 4096.f;
  268. } else {
  269. tVal = ((float)cNode->values.max) / 4096.f;
  270. };
  271. };
  272. };
  273. if( ( idx % 9 ) == 0 ){
  274. if( mem > ( SKIP_LINE_PIXELS * TH_LINE_NODES ) ) {
  275. cPixel = tMemory + (pix << 2);
  276. pix++;
  277. if( s_bit ) { // Set Red
  278. cPixel[0] = 0x00; cPixel[1] = 0xff; cPixel[2] = 0x00; cPixel[3] = 0xff;
  279. } else {
  280. if( no_data ) { // Set Blue
  281. cPixel[0] = 0x00; cPixel[1] = 0x00; cPixel[2] = 0xff; cPixel[3] = 0xff;
  282. } else {
  283. uint32_t tColor;
  284. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  285. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  286. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  287. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  288. cPixel[3] = tColor & 0xFF;
  289. };
  290. };
  291. s_bit = false;
  292. no_data = false;
  293. }
  294. };
  295. };
  296. char filename[256];
  297. sprintf( filename, "data/device_draws/%s_100.png", uid );
  298. int RetVal = stbi_write_png( filename, 1024, 512, cSpace, tMemory, 1024 * cSpace );
  299. free( tMemory );
  300. if( RetVal == 0 ) return false;
  301. if( monochrome ) {
  302. cSpace = 1;
  303. } else {
  304. cSpace = 4;
  305. };
  306. tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace );
  307. if( tMemory == NULL ){
  308. LOG_ERROR("Map Memory Alloc Eror!");
  309. return false;
  310. };
  311. for (int j = 0; j < Q_SCAN_COUNT; j++) {
  312. for ( int i = 0; i < TH_LINE_NODES; i++ ) {
  313. //cNode = ptr+(j*i);
  314. //cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
  315. int ids = 0;
  316. int idd = 0;
  317. if ( master ) {
  318. //cNode = ptr+makeIds( i, j , true );
  319. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  320. ids = makeIds( i, j , true );
  321. idd = cSpace * makeIds( i, j , false );
  322. //cNode = ptr + ( i * j );
  323. //cPixel = tMemory + ( cSpace * i * j );
  324. } else {
  325. //cNode = ptr+makeIds( i, j , false );
  326. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  327. ids = makeIds( i, j , false );
  328. idd = cSpace * makeIds( i, j , false );
  329. };
  330. cNode = ptr + ids ;
  331. cPixel = tMemory + idd;
  332. if( monochrome ) {
  333. #ifdef CHECK_SUN_BIT
  334. if( cNode->values.sbit ) {
  335. cPixel[0] = 0;
  336. } else {
  337. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  338. };
  339. #else
  340. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  341. #endif
  342. } else {
  343. #ifdef CHECK_SUN_BIT
  344. if( cNode->values.sbit ) {
  345. // Set Red
  346. cPixel[0] = 0x00;
  347. cPixel[1] = 0xff;
  348. cPixel[2] = 0x00;
  349. cPixel[3] = 0xff;
  350. } else {
  351. if( cNode->values.cnt == 0 ) {
  352. // Set Blue
  353. cPixel[0] = 0x00;
  354. cPixel[1] = 0x00;
  355. cPixel[2] = 0xff;
  356. cPixel[3] = 0xff;
  357. } else {
  358. uint32_t tColor;
  359. float tVal = ((float) cNode->values.min ) / 4096.f;
  360. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  361. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  362. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  363. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  364. cPixel[3] = tColor & 0xFF;
  365. };
  366. };
  367. #else
  368. if( cNode->values.cnt == 0 ) {
  369. // Set Blue
  370. cPixel[0] = 0x00;
  371. cPixel[1] = 0x00;
  372. cPixel[2] = 0xff;
  373. cPixel[3] = 0xff;
  374. } else {
  375. uint32_t tColor;
  376. float tVal = ((float) cNode->values.min ) / 4096.f;
  377. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  378. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  379. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  380. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  381. cPixel[3] = tColor & 0xFF;
  382. };
  383. #endif
  384. };
  385. };
  386. };
  387. sprintf( filename, "data/device_draws/%s_map.png", uid );
  388. RetVal = stbi_write_png( filename, TH_LINE_NODES, Q_SCAN_COUNT, cSpace, tMemory, TH_LINE_NODES * cSpace );
  389. free( tMemory );
  390. if( RetVal == 0 ) return false;
  391. return true;
  392. };
  393. */
  394. #undef LOG
  395. #undef LOG_ERROR