EncodeDecodeHelper.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "EncodeDecodeHelper.h"
  2. /// <summary>
  3. /// 构造函数
  4. /// </summary>
  5. EncodeDecodeHelper::EncodeDecodeHelper()
  6. {
  7. _imgDecode = cv::Mat();
  8. _imgEncode = cv::Mat();
  9. }
  10. /// <summary>
  11. /// 析构函数
  12. /// </summary>
  13. EncodeDecodeHelper::~EncodeDecodeHelper()
  14. {
  15. _imgDecode.release();
  16. _imgEncode.release();
  17. _dataEncode.clear();
  18. _dataDecode.clear();
  19. }
  20. /// <summary>
  21. /// 编码
  22. /// </summary>
  23. bool EncodeDecodeHelper::Encode(ImageInfo srcImgInfo,ImwriteExtension extension,
  24. ImwriteParam* imwriteParams,int paramCount, uint8_t* dstImgData, int& dstDataSize)
  25. {
  26. try
  27. {
  28. auto depthFlag = ImageHelper::GetDepthFlag(srcImgInfo.colorType);
  29. _imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), depthFlag, (void*)srcImgInfo.dataBuffer);
  30. std::vector<int> params;
  31. if(paramCount>0)
  32. {
  33. for(int ni=0; ni<paramCount;ni++)
  34. {
  35. ImwriteParam imwriteParam = imwriteParams[ni];
  36. switch(imwriteParam.flagName)
  37. {
  38. case JpegQuality:
  39. params.push_back(cv::IMWRITE_JPEG_QUALITY);
  40. break;
  41. case JpegProgressive:
  42. params.push_back(cv::IMWRITE_JPEG_PROGRESSIVE);
  43. break;
  44. case JpegOptimize:
  45. params.push_back(cv::IMWRITE_JPEG_OPTIMIZE);
  46. break;
  47. case JpegRstInterval:
  48. params.push_back(cv::IMWRITE_JPEG_RST_INTERVAL);
  49. break;
  50. case JpegLumaQuality:
  51. params.push_back(cv::IMWRITE_JPEG_LUMA_QUALITY);
  52. break;
  53. case JpegChromaQuality:
  54. params.push_back(cv::IMWRITE_JPEG_CHROMA_QUALITY);
  55. break;
  56. case PngCompression:
  57. params.push_back(cv::IMWRITE_PNG_COMPRESSION);
  58. break;
  59. case PngStrategy:
  60. params.push_back(cv::IMWRITE_PNG_STRATEGY);
  61. break;
  62. case PngBilevel:
  63. params.push_back(cv::IMWRITE_PNG_BILEVEL);
  64. break;
  65. }
  66. // Todo 没有检查值是否在范围内
  67. params.push_back(imwriteParam.flagValue);
  68. }
  69. }
  70. cv::String ext;
  71. switch(extension)
  72. {
  73. case Png:
  74. ext = ".png";
  75. break;
  76. case Jpg:
  77. ext = ".jpg";
  78. break;
  79. case Bmp:
  80. ext = ".bmp";
  81. break;
  82. default:
  83. char strMsgBuff[32];
  84. std::snprintf(strMsgBuff, 32, "unexpected extension value ( %i ) ", extension);
  85. ErrorMsg::SetErrorMsg(EncodeError,{ strMsgBuff });
  86. return false;
  87. }
  88. cv::imencode(ext, _imgEncode, _dataEncode, params);
  89. int encodedSize = _dataEncode.size();
  90. if(encodedSize > dstDataSize)
  91. {
  92. char strMsgBuff[32];
  93. std::snprintf(strMsgBuff, 32, "not enough space to copy the encoded image data. ( need: %i actual: %i ) ",
  94. _dataEncode.size(), dstDataSize);
  95. ErrorMsg::SetErrorMsg(EncodeError,{ strMsgBuff });
  96. return false;
  97. }
  98. for(int ni=0; ni<encodedSize; ni++)
  99. {
  100. dstImgData[ni] = _dataEncode[ni];
  101. }
  102. dstDataSize = encodedSize;
  103. return true;
  104. }
  105. catch (const std::exception& ex)
  106. {
  107. ErrorMsg::SetErrorMsg(EncodeError,{ ex.what() });
  108. return false;
  109. }
  110. }
  111. /// <summary>
  112. /// 解码
  113. /// </summary>
  114. bool EncodeDecodeHelper::Decode(const uint8_t* srcImgData, const int srcDataSize,
  115. uint8_t* dstImgData, const int dstDataSize, ImreadModes readMode)
  116. {
  117. try
  118. {
  119. _dataDecode.resize(srcDataSize);
  120. for(int ni=0; ni<srcDataSize; ni++)
  121. {
  122. _dataDecode[ni] = srcImgData[ni];
  123. }
  124. _imgDecode = cv::imdecode(_dataDecode, readMode);
  125. int size = _imgDecode.total() * _imgDecode.elemSize();
  126. if(dstDataSize < size)
  127. {
  128. char strMsgBuff[64];
  129. std::snprintf(strMsgBuff, 64, "the dstDataSize ( %i ) is less than need ( %i )", dstDataSize, size);
  130. ErrorMsg::SetErrorMsg(DecodeError,{ strMsgBuff });
  131. return false;
  132. }
  133. std::memcpy(dstImgData, _imgDecode.data, size);
  134. return true;
  135. }
  136. catch (const std::exception& ex)
  137. {
  138. ErrorMsg::SetErrorMsg(DecodeError, { ex.what() });
  139. return false;
  140. }
  141. }
  142. /// <summary>
  143. /// 图像编码并保存至本地
  144. /// </summary>
  145. bool EncodeDecodeHelper::SaveImage(ImageInfo srcImgInfo, ImwriteExtension extension,
  146. ImwriteParam* imwriteParams, int paramCount, const char* savePath)
  147. {
  148. try
  149. {
  150. auto depthFlag = ImageHelper::GetDepthFlag(srcImgInfo.colorType);
  151. _imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), depthFlag, (void*)srcImgInfo.dataBuffer);
  152. std::vector<int> params;
  153. if (paramCount > 0)
  154. {
  155. for (int ni = 0; ni < paramCount; ni++)
  156. {
  157. ImwriteParam imwriteParam = imwriteParams[ni];
  158. switch (imwriteParam.flagName)
  159. {
  160. case JpegQuality:
  161. params.push_back(cv::IMWRITE_JPEG_QUALITY);
  162. break;
  163. case JpegProgressive:
  164. params.push_back(cv::IMWRITE_JPEG_PROGRESSIVE);
  165. break;
  166. case JpegOptimize:
  167. params.push_back(cv::IMWRITE_JPEG_OPTIMIZE);
  168. break;
  169. case JpegRstInterval:
  170. params.push_back(cv::IMWRITE_JPEG_RST_INTERVAL);
  171. break;
  172. case JpegLumaQuality:
  173. params.push_back(cv::IMWRITE_JPEG_LUMA_QUALITY);
  174. break;
  175. case JpegChromaQuality:
  176. params.push_back(cv::IMWRITE_JPEG_CHROMA_QUALITY);
  177. break;
  178. case PngCompression:
  179. params.push_back(cv::IMWRITE_PNG_COMPRESSION);
  180. break;
  181. case PngStrategy:
  182. params.push_back(cv::IMWRITE_PNG_STRATEGY);
  183. break;
  184. case PngBilevel:
  185. params.push_back(cv::IMWRITE_PNG_BILEVEL);
  186. break;
  187. }
  188. // Todo 没有检查值是否在范围内
  189. params.push_back(imwriteParam.flagValue);
  190. }
  191. }
  192. cv::String ext;
  193. switch (extension)
  194. {
  195. case Png:
  196. ext = ".png";
  197. break;
  198. case Jpg:
  199. ext = ".jpg";
  200. break;
  201. case Bmp:
  202. ext = ".bmp";
  203. break;
  204. default:
  205. char strMsgBuff[32];
  206. std::snprintf(strMsgBuff, 32, "unexpected extension value ( %i ) ", extension);
  207. ErrorMsg::SetErrorMsg(EncodeError, { strMsgBuff });
  208. return false;
  209. }
  210. cv::imwrite(savePath, _imgEncode);
  211. return true;
  212. }
  213. catch (const std::exception& ex)
  214. {
  215. ErrorMsg::SetErrorMsg(EncodeError, { ex.what() });
  216. return false;
  217. }
  218. }