|  | 
|  | 1 | +/* Copyright 2025 The xLLM Authors. All Rights Reserved. | 
|  | 2 | +Copyright 2024 The ScaleLLM Authors. All Rights Reserved. | 
|  | 3 | +
 | 
|  | 4 | +Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +you may not use this file except in compliance with the License. | 
|  | 6 | +You may obtain a copy of the License at | 
|  | 7 | +
 | 
|  | 8 | +    https://github.com/jd-opensource/xllm/blob/main/LICENSE | 
|  | 9 | +
 | 
|  | 10 | +Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +See the License for the specific language governing permissions and | 
|  | 14 | +limitations under the License. | 
|  | 15 | +==============================================================================*/ | 
|  | 16 | + | 
|  | 17 | +#include "mm_codec.h" | 
|  | 18 | + | 
|  | 19 | +namespace xllm { | 
|  | 20 | + | 
|  | 21 | +bool OpenCVImageDecoder::decode(const std::string& raw_data, torch::Tensor& t) { | 
|  | 22 | +  cv::Mat buffer(1, raw_data.size(), CV_8UC1, (void*)raw_data.data()); | 
|  | 23 | +  cv::Mat image = cv::imdecode(buffer, cv::IMREAD_COLOR); | 
|  | 24 | +  if (image.empty()) { | 
|  | 25 | +    LOG(INFO) << " opencv image decode failed"; | 
|  | 26 | +    return false; | 
|  | 27 | +  } | 
|  | 28 | + | 
|  | 29 | +  cv::cvtColor(image, image, cv::COLOR_BGR2RGB);  // RGB | 
|  | 30 | + | 
|  | 31 | +  torch::Tensor tensor = | 
|  | 32 | +      torch::from_blob(image.data, {image.rows, image.cols, 3}, torch::kUInt8); | 
|  | 33 | + | 
|  | 34 | +  t = tensor.permute({2, 0, 1}).clone();  // [C, H, W] | 
|  | 35 | +  return true; | 
|  | 36 | +} | 
|  | 37 | + | 
|  | 38 | +bool OpenCVImageEncoder::encode(const torch::Tensor& t, std::string& raw_data) { | 
|  | 39 | +  if (!valid(t)) { | 
|  | 40 | +    return false; | 
|  | 41 | +  } | 
|  | 42 | + | 
|  | 43 | +  auto img = t.permute({1, 2, 0}).contiguous(); | 
|  | 44 | +  cv::Mat mat(img.size(0), img.size(1), CV_32FC3, img.data_ptr<float>()); | 
|  | 45 | + | 
|  | 46 | +  cv::Mat mat_8u; | 
|  | 47 | +  mat.convertTo(mat_8u, CV_8UC3, 255.0); | 
|  | 48 | + | 
|  | 49 | +  // rgb -> bgr | 
|  | 50 | +  cv::cvtColor(mat_8u, mat_8u, cv::COLOR_RGB2BGR); | 
|  | 51 | + | 
|  | 52 | +  std::vector<uchar> data; | 
|  | 53 | +  if (!cv::imencode(".png", mat_8u, data)) { | 
|  | 54 | +    LOG(ERROR) << "image encode faild"; | 
|  | 55 | +    return false; | 
|  | 56 | +  } | 
|  | 57 | + | 
|  | 58 | +  raw_data.assign(data.begin(), data.end()); | 
|  | 59 | +  return true; | 
|  | 60 | +} | 
|  | 61 | + | 
|  | 62 | +bool OpenCVImageEncoder::valid(const torch::Tensor& t) { | 
|  | 63 | +  if (t.dim() != 3 || t.size(0) != 3) { | 
|  | 64 | +    LOG(ERROR) << "input tensor must be 3HW  tensor"; | 
|  | 65 | +    return false; | 
|  | 66 | +  } | 
|  | 67 | + | 
|  | 68 | +  if (t.scalar_type() != torch::kFloat32 || !t.device().is_cpu()) { | 
|  | 69 | +    LOG(ERROR) << "tensor must be cpu float32"; | 
|  | 70 | +    return false; | 
|  | 71 | +  } | 
|  | 72 | + | 
|  | 73 | +  return true; | 
|  | 74 | +} | 
|  | 75 | + | 
|  | 76 | +}  // namespace xllm | 
0 commit comments