Skip to content

Commit

Permalink
Fix final confusion between format and encoding.
Browse files Browse the repository at this point in the history
Format is file format like jpg, bmp and more,
encoding is color encoding like bgr8, rgb8 and more.
  • Loading branch information
talregev committed May 10, 2019
1 parent f72d1f4 commit 952b590
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions cv_bridge/src/cv_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ Encoding getEncoding(const std::string& encoding)
return INVALID;
}

static const int SAME_FORMAT = -1;
static const int SAME_ENCODING = -1;

/** Return a lit of OpenCV conversion codes to get from one Format to the other
* The key is a pair: <FromFormat, ToFormat> and the value a succession of OpenCV code conversion
/** Return a lit of OpenCV conversion codes to get from one encoding to the other
* The key is a pair: <FromEncoding, ToEncoding> and the value a succession of OpenCV code conversion
* It's not efficient code but it is only called once and the structure is small enough
*/
std::map<std::pair<Encoding, Encoding>, std::vector<int> > getConversionCodes() {
std::map<std::pair<Encoding, Encoding>, std::vector<int> > res;
for(int i=0; i<=5; ++i)
res[std::pair<Encoding, Encoding>(Encoding(i),Encoding(i))].push_back(SAME_FORMAT);
res[std::pair<Encoding, Encoding>(Encoding(i),Encoding(i))].push_back(SAME_ENCODING);

res[std::make_pair(GRAY, RGB)].push_back(cv::COLOR_GRAY2RGB);
res[std::make_pair(GRAY, BGR)].push_back(cv::COLOR_GRAY2BGR);
Expand Down Expand Up @@ -214,7 +214,7 @@ const std::vector<int> getConversionCode(std::string src_encoding, std::string d
"] is. The conversion does not make sense");
if (!is_num_channels_the_same)
throw Exception("[" + src_encoding + "] and [" + dst_encoding + "] do not have the same number of channel");
return std::vector<int>(1, SAME_FORMAT);
return std::vector<int>(1, SAME_ENCODING);
}

// If we are converting from a color type to a non color type, we can only do so if we stick
Expand All @@ -223,7 +223,7 @@ const std::vector<int> getConversionCode(std::string src_encoding, std::string d
if (!is_num_channels_the_same)
throw Exception("[" + src_encoding + "] is a color encoding but [" + dst_encoding + "] " +
"is not so they must have the same OpenCV type, CV_8UC3, CV16UC1 ....");
return std::vector<int>(1, SAME_FORMAT);
return std::vector<int>(1, SAME_ENCODING);
}

// If we are converting from a color type to another type, then everything is fine
Expand All @@ -238,7 +238,7 @@ const std::vector<int> getConversionCode(std::string src_encoding, std::string d
// And deal with depth differences if the colors are different
std::vector<int> res = val->second;
if ((enc::bitDepth(src_encoding) != enc::bitDepth(dst_encoding)) && (getEncoding(src_encoding) != getEncoding(dst_encoding)))
res.push_back(SAME_FORMAT);
res.push_back(SAME_ENCODING);

return res;
}
Expand Down Expand Up @@ -320,7 +320,7 @@ CvImagePtr toCvCopyImpl(const cv::Mat& source,
cv::Mat image2;
for(size_t i=0; i<conversion_codes.size(); ++i) {
int conversion_code = conversion_codes[i];
if (conversion_code == SAME_FORMAT)
if (conversion_code == SAME_ENCODING)
{
// Same number of channels, but different bit depth
int src_depth = enc::bitDepth(src_encoding);
Expand Down Expand Up @@ -550,7 +550,7 @@ CvImageConstPtr cvtColorForDisplay(const CvImageConstPtr& source,
{
try
{
// Let's decide upon an output format
// Let's decide upon an output encoding
if (enc::numChannels(source->encoding) == 1)
{
if ((source->encoding == enc::TYPE_32SC1) ||
Expand Down Expand Up @@ -661,13 +661,13 @@ CvImageConstPtr cvtColorForDisplay(const CvImageConstPtr& source,
return cvtColor(img_scaled, encoding);
}

// If no color conversion is possible, we must "guess" the input format
// If no color conversion is possible, we must "guess" the input encoding
CvImagePtr source_typed(new CvImage());
source_typed->image = source->image;
source_typed->header = source->header;
source_typed->encoding = source->encoding;

// If we get the OpenCV format, if we have 1,3 or 4 channels, we are most likely in mono, BGR or BGRA modes
// If we get the OpenCV encoding, if we have 1,3 or 4 channels, we are most likely in mono, BGR or BGRA modes
if (source->encoding == "CV_8UC1")
source_typed->encoding = enc::MONO8;
else if (source->encoding == "16UC1")
Expand All @@ -687,7 +687,7 @@ CvImageConstPtr cvtColorForDisplay(const CvImageConstPtr& source,

try
{
// Now that the output is a proper color format, try to see if any conversion is possible
// Now that the output is a proper color encoding, try to see if any conversion is possible
return cvtColor(source_typed, encoding);
}
catch (cv_bridge::Exception& e)
Expand Down

0 comments on commit 952b590

Please sign in to comment.