Guest User

Untitled

a guest
Dec 14th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. typedef struct image_struct
  2. {
  3. // Header
  4. uint32_t id;
  5. uint32_t timestamp;
  6. uint16_t nHeight;
  7. uint16_t nWidth;
  8. // Buffer
  9. float *depth_map;
  10. uint16_t *amplitude_map;
  11.  
  12. uint16_t getWidth(){ return nWidth;};
  13. uint16_t getHeight(){return nHeight;};
  14.  
  15. } IMAGE_STRUCT;
  16.  
  17. void atImageEvent( short eventType, int size, short imageFormat, char* sfimageData, int dataLength, std::string& pScannerData )
  18. {
  19. std::string image_format;
  20. switch ( imageFormat )
  21. {
  22. case JPEG_FILE_SECTION:
  23. image_format = ".jpg";
  24. break;
  25. case TIFF_FILE_SECTION:
  26. image_format = ".tiff";
  27. break;
  28. case BMP_FILE_SECTION:
  29. image_format = ".bmp";
  30. break;
  31. case RAW_FILE_SELECTION:
  32. image_format = ".deb";
  33. break;
  34. case RAW_PROCESSED_FILE_SELECTION:
  35. image_format = ".dpf";
  36. break;
  37. case DEPTH_FRAME_FILE_SELECTION:
  38. image_format = ".dpf";
  39. break;
  40. default:
  41. cout << "Different type of image format, but saving as .dpf." << endl;
  42. image_format = ".dpf";
  43. break;
  44. }
  45.  
  46. string sFileName;
  47. stringstream ss;
  48. ss << "imagedata";
  49. ss << this->iImageFileNumber++;
  50. ss << image_format;
  51. sFileName = ss.str();
  52.  
  53.  
  54. // cast image data into depth frame buffer.
  55. IMAGE_STRUCT output_image_struct;
  56.  
  57.  
  58. // define lengths for image struct
  59. int dataIndex=0;
  60. int idLength = sizeof(uint32_t);
  61. int tstampLength = sizeof(uint32_t);
  62. int heightLength = sizeof(uint16_t);
  63. int widthLength = sizeof(uint16_t);
  64.  
  65.  
  66. uint8_t *id, *timeStamp, *imgHeight, *imgWidth, *depthFrameData, *amplitudeData;
  67.  
  68. id = (uint8_t *)malloc(sizeof(uint32_t));
  69. memset(id, 0x00, idLength);
  70. dataIndex = idLength;
  71. memcpy(id, sfimageData, sizeof(uint32_t));
  72.  
  73. union
  74. {
  75. uint32_t _id;
  76. uint8_t *_byteArray;
  77. }uId;
  78. memcpy(&uId._byteArray, id, sizeof(uint32_t));
  79. free(id);
  80.  
  81. output_image_struct.id = uId._id;
  82.  
  83. timeStamp = (uint8_t *)malloc(sizeof(uint32_t));
  84. memset(timeStamp, 0x00, tstampLength);
  85. memcpy(timeStamp, sfimageData+dataIndex, sizeof(uint32_t));
  86. dataIndex += tstampLength;
  87.  
  88. union
  89. {
  90. uint32_t _timeStamp;
  91. uint8_t *_byteArray;
  92. }uTimeStamp;
  93. memcpy(&uTimeStamp._byteArray, timeStamp, tstampLength);
  94. free(timeStamp);
  95. output_image_struct.timestamp = uTimeStamp._timeStamp;
  96.  
  97. imgHeight = (uint8_t *)malloc(heightLength);
  98. memcpy(imgHeight, sfimageData+dataIndex, sizeof(uint16_t));
  99. dataIndex += heightLength;
  100.  
  101. union
  102. {
  103. uint16_t _imgHeight;
  104. uint8_t *_byteArray;
  105. }uImgHeight;
  106. memcpy(&uImgHeight._byteArray, imgHeight, heightLength);
  107. free(imgHeight);
  108. output_image_struct.nHeight = uImgHeight._imgHeight;
  109.  
  110. imgWidth = (uint8_t *)malloc(widthLength);
  111. memset(imgWidth, 0x00, widthLength);
  112. memcpy(imgWidth, sfimageData+dataIndex, sizeof(uint16_t));
  113. dataIndex += widthLength;
  114.  
  115. union
  116. {
  117. uint32_t _imgWidth;
  118. uint8_t *_byteArray;
  119. }uImgWidth;
  120.  
  121. memcpy(&uImgWidth._byteArray, imgWidth, widthLength);
  122. free(imgWidth);
  123. output_image_struct.nWidth = uImgWidth._imgWidth;
  124.  
  125. int depthFrameLength = output_depth_frame.nHeight * output_depth_frame.nWidth * sizeof(float);
  126. int amplitudeMapLength = output_depth_frame.nHeight * output_depth_frame.nWidth * sizeof(uint16_t);
  127.  
  128. depthFrameData = (uint8_t *)malloc(depthFrameLength);
  129. memset(depthFrameData, 0x00, depthFrameLength);
  130. memcpy(depthFrameData, sfimageData+dataIndex, depthFrameLength);
  131. dataIndex += depthFrameLength;
  132.  
  133. union
  134. {
  135. uint8_t *byteArray;
  136. float *floatArray;
  137. } uDepthFrame;
  138.  
  139. uDepthFrame.byteArray = depthFrameData;
  140.  
  141. output_image_struct.depth_map = uDepthFrame.floatArray;
  142.  
  143. amplitudeData = (uint8_t *)malloc(amplitudeMapLength);
  144. memset(amplitudeData, 0x00, amplitudeMapLength);
  145. memcpy(amplitudeData, sfimageData + dataIndex, amplitudeMapLength);
  146. dataIndex += amplitudeMapLength;
  147.  
  148. union
  149. {
  150. uint8_t *byteArray;
  151. uint16_t *uint16Array;
  152. } uAmplitude;
  153.  
  154. uAmplitude.byteArray = amplitudeData;
  155.  
  156. output_image_struct.amplitude_map = uAmplitude.uint16Array;
  157.  
  158. int middleIndex = (output_image_struct.getHeight()+1)*output_image_struct.getWidth()/2;
  159.  
  160. // Print output_image_struct (primitive members)
  161. cout << "-----------------------------------------------------" << endl;
  162. cout << "ID: " << output_image_struct.id << endl;
  163. cout << "TimeStamp: " << output_image_struct.timestamp << endl;
  164. cout << "Img Height: " << output_image_struct.nHeight << endl;
  165. cout << "Img Width: " << output_image_struct.nWidth << endl;
  166. cout << "First Index: " << 0 << " value: " << output_image_struct.depth_map[0] << endl;
  167. cout << "Middle Index: " << middleIndex << " value: " << output_image_struct.depth_map[middleIndex] << endl;
  168. cout << "last Index: " << (output_image_struct.getHeight() * output_image_struct.getWidth()) - 1 << " value: " << output_image_struct.depth_map[ (output_image_struct.getHeight() * output_image_struct.getWidth()) - 1 ] << endl;
  169. cout << "-----------------------------------------------------" << endl;
  170.  
  171. // print data if interested
  172. for(int x=0; x<depthFrameLength/sizeof(float); x++)
  173. {
  174. cout << " " << output_depth_frame.depth_map[x];
  175. }
  176.  
  177. for(int x=0; x<amplitudeMapLength/sizeof(uint16_t); x++)
  178. {
  179. cout << " " << output_image_struct.amplitude_map[x];
  180. }
  181.  
  182. cout << "END ********************" << endl;
  183.  
  184. free(depthFrameData);
  185. free(amplitudeData);
  186. }
  187.  
  188. const char * fileExtensionOfFormat(img_fmt); // JPEG_FILE_SECTION -> ".jpg"
  189.  
  190. typedef uint8_t byte;
  191.  
  192. IMAGE_STRUCT * decodeImageData(byte * img_data);
  193.  
  194. uint16_t read_uint16(byte *);
  195. uint32_t read_uint32(byte *);
  196. float read_float(byte *);
  197.  
  198. uint16_t read_short(byte * p) {
  199. return *(uint16_t *)p;
  200. }
  201.  
  202. union
  203. {
  204. uint16_t _imgHeight;
  205. uint8_t *_byteArray;
  206. }uImgHeight;
  207. memcpy(&uImgHeight._byteArray, imgHeight, heightLength);
  208. free(imgHeight);
  209. output_image_struct.nHeight = uImgHeight._imgHeight;
  210.  
  211. id = (uint8_t *)malloc(sizeof(uint32_t)); // #1
  212. memset(id, 0x00, idLength); // #2
  213. dataIndex = idLength;
  214. memcpy(id, sfimageData, sizeof(uint32_t)); // #3
  215.  
  216. union
  217. {
  218. uint32_t _id;
  219. uint8_t *_byteArray;
  220. }uId;
  221. memcpy(&uId._byteArray, id, sizeof(uint32_t)); // #4
  222. free(id);
  223.  
  224. output_image_struct.id = uId._id; // #5
  225.  
  226. #pragma pack(push,1) // structure packing syntax may vary
  227. struct image_struct_data
  228. {
  229. uint32_t id;
  230. uint32_t timestamp;
  231. uint16_t nHeight;
  232. uint16_t nWidth;
  233. };
  234. #pragma pack(pop)
  235. struct image_struct
  236. {
  237. image_struct_data data;
  238. // Buffers
  239. float *depth_map;
  240. uint16_t *amplitude_map;
  241. uint16_t getWidth() {return data.nWidth;};
  242. uint16_t getHeight() {return data.nHeight;};
  243. };
  244. ...
  245. image_struct_data output_image_struct;
  246. memcpy(&output_image_struct, sfimageData, sizeof(output_image_struct));
  247. sfimageData += sizeof(output_image_struct);
  248.  
  249. int depthFrameLength = output_image_struct.nHeight * output_image_struct.nWidth * sizeof(float);
  250. int amplitudeMapLength = output_image_struct.nHeight * output_image_struct.nWidth * sizeof(uint16_t);
  251. output_image_struct.depth_map = malloc(depthFrameLength);
  252. memcpy(output_image_struct.depth_map, sfimageData, depthFrameLength);
  253. sfimageData += depthFrameLength;
  254. output_image_struct.amplitude_map = malloc(amplitudeMapLength);
  255. memcpy(output_image_struct.amplitude_map, sfimageData, amplitudeMapLength);
  256. sfimageData += amplitudeMapLength;
  257. ...
Add Comment
Please, Sign In to add comment