Skip to content

Commit

Permalink
Fix misra C++ quality rules around old cast and remove useless limits…
Browse files Browse the repository at this point in the history
… header include
  • Loading branch information
fspindle committed Jun 4, 2024
1 parent 14850e8 commit 63373bd
Show file tree
Hide file tree
Showing 32 changed files with 245 additions and 221 deletions.
20 changes: 10 additions & 10 deletions modules/core/include/visp3/core/vpImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1807,13 +1807,13 @@ template <class Type> void vpImage<Type>::quarterSizeImage(vpImage<Type> &res) c
*/
template <class Type> void vpImage<Type>::doubleSizeImage(vpImage<Type> &res)
{
int h = height * 2;
int w = width * 2;
unsigned int h = height * 2;
unsigned int w = width * 2;

res.resize(h, w);

for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
for (unsigned int i = 0; i < h; ++i) {
for (unsigned int j = 0; j < w; ++j) {
res[i][j] = (*this)[i >> 1][j >> 1];
}
}
Expand All @@ -1827,22 +1827,22 @@ template <class Type> void vpImage<Type>::doubleSizeImage(vpImage<Type> &res)
*/

// interpolate pixels B and I
for (int i = 0; i < h; i += 2) {
for (int j = 1; j < (w - 1); j += 2) {
for (unsigned int i = 0; i < h; i += 2) {
for (unsigned int j = 1; j < (w - 1); j += 2) {
res[i][j] = (Type)(0.5 * ((*this)[i >> 1][j >> 1] + (*this)[i >> 1][(j >> 1) + 1]));
}
}

// interpolate pixels E and G
for (int i = 1; i < (h - 1); i += 2) {
for (int j = 0; j < w; j += 2) {
for (unsigned int i = 1; i < (h - 1); i += 2) {
for (unsigned int j = 0; j < w; j += 2) {
res[i][j] = (Type)(0.5 * ((*this)[i >> 1][j >> 1] + (*this)[(i >> 1) + 1][j >> 1]));
}
}

// interpolate pixel F
for (int i = 1; i < (h - 1); i += 2) {
for (int j = 1; j < (w - 1); j += 2) {
for (unsigned int i = 1; i < (h - 1); i += 2) {
for (unsigned int j = 1; j < (w - 1); j += 2) {
res[i][j] = (Type)(0.25 * ((*this)[i >> 1][j >> 1] + (*this)[i >> 1][(j >> 1) + 1] +
(*this)[(i >> 1) + 1][j >> 1] + (*this)[(i >> 1) + 1][(j >> 1) + 1]));
}
Expand Down
1 change: 0 additions & 1 deletion modules/core/include/visp3/core/vpImagePoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <visp3/core/vpConfig.h>

#include <cmath> // std::fabs
#include <limits> // numeric_limits
#include <ostream>
#include <vector>

Expand Down
62 changes: 31 additions & 31 deletions modules/core/include/visp3/core/vpIoTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static inline unsigned long vp_mz_crc32(unsigned long crc, const unsigned char *
{
static const unsigned int s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
unsigned int crcu32 = (unsigned int)crc;
unsigned int crcu32 = static_cast<unsigned int>(crc);
if (!ptr) return 0;
crcu32 = ~crcu32;
while (buf_len--) {
Expand Down Expand Up @@ -274,42 +274,42 @@ template<typename T> void npz_save(std::string zipname, std::string fname, const
//build the local header
std::vector<char> local_header;
local_header += "PK"; //first part of sig
local_header += (uint16_t)0x0403; //second part of sig
local_header += (uint16_t)20; //min version to extract
local_header += (uint16_t)0; //general purpose bit flag
local_header += (uint16_t)0; //compression method
local_header += (uint16_t)0; //file last mod time
local_header += (uint16_t)0; //file last mod date
local_header += (uint32_t)crc; //crc
local_header += (uint32_t)nbytes; //compressed size
local_header += (uint32_t)nbytes; //uncompressed size
local_header += (uint16_t)fname.size(); //fname length
local_header += (uint16_t)0; //extra field length
local_header += static_cast<uint16_t>(0x0403); //second part of sig
local_header += static_cast<uint16_t>(20); //min version to extract
local_header += static_cast<uint16_t>(0); //general purpose bit flag
local_header += static_cast<uint16_t>(0); //compression method
local_header += static_cast<uint16_t>(0); //file last mod time
local_header += static_cast<uint16_t>(0); //file last mod date
local_header += static_cast<uint32_t>(crc); //crc
local_header += static_cast<uint32_t>(nbytes); //compressed size
local_header += static_cast<uint32_t>(nbytes); //uncompressed size
local_header += static_cast<uint16_t>(fname.size()); //fname length
local_header += static_cast<uint16_t>(0); //extra field length
local_header += fname;

//build global header
global_header += "PK"; //first part of sig
global_header += (uint16_t)0x0201; //second part of sig
global_header += (uint16_t)20; //version made by
global_header += static_cast<uint16_t>(0x0201); //second part of sig
global_header += static_cast<uint16_t>(20); //version made by
global_header.insert(global_header.end(), local_header.begin()+4, local_header.begin()+30);
global_header += (uint16_t)0; //file comment length
global_header += (uint16_t)0; //disk number where file starts
global_header += (uint16_t)0; //internal file attributes
global_header += (uint32_t)0; //external file attributes
global_header += (uint32_t)global_header_offset; //relative offset of local file header, since it begins where the global header used to begin
global_header += static_cast<uint16_t>(0); //file comment length
global_header += static_cast<uint16_t>(0); //disk number where file starts
global_header += static_cast<uint16_t>(0); //internal file attributes
global_header += static_cast<uint32_t>(0); //external file attributes
global_header += static_cast<uint32_t>(global_header_offset); //relative offset of local file header, since it begins where the global header used to begin
global_header += fname;

//build footer
std::vector<char> footer;
footer += "PK"; //first part of sig
footer += (uint16_t)0x0605; //second part of sig
footer += (uint16_t)0; //number of this disk
footer += (uint16_t)0; //disk where footer starts
footer += (uint16_t)(nrecs+1); //number of records on this disk
footer += (uint16_t)(nrecs+1); //total number of records
footer += (uint32_t)global_header.size(); //nbytes of global headers
footer += (uint32_t)(global_header_offset + nbytes + local_header.size()); //offset of start of global headers, since global header now starts after newly written array
footer += (uint16_t)0; //zip file comment length
footer += static_cast<uint16_t>(0x0605); //second part of sig
footer += static_cast<uint16_t>(0); //number of this disk
footer += static_cast<uint16_t>(0); //disk where footer starts
footer += static_cast<uint16_t>(nrecs+1); //number of records on this disk
footer += static_cast<uint16_t>(nrecs+1); //total number of records
footer += static_cast<uint32_t>(global_header.size()); //nbytes of global headers
footer += static_cast<uint32_t>(global_header_offset + nbytes + local_header.size()); //offset of start of global headers, since global header now starts after newly written array
footer += static_cast<uint16_t>(0); //zip file comment length

//write everything
fwrite(&local_header[0], sizeof(char), local_header.size(), fp);
Expand Down Expand Up @@ -374,11 +374,11 @@ template<typename T> std::vector<char> create_npy_header(const std::vector<size_
dict.back() = '\n';

std::vector<char> header;
header += (char)0x93;
header += static_cast<char>(0x93);
header += "NUMPY";
header += (char)0x01; //major version of numpy format
header += (char)0x00; //minor version of numpy format
header += (uint16_t)dict.size();
header += static_cast<char>(0x01); //major version of numpy format
header += static_cast<char>(0x00); //minor version of numpy format
header += static_cast<uint16_t>(dict.size());
header.insert(header.end(), dict.begin(), dict.end());

return header;
Expand Down
76 changes: 42 additions & 34 deletions modules/core/src/camera/vpXmlParserCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,18 @@ class vpXmlParserCamera::Impl
bool test_subsampling_height = true;

if (subsampling_width) {
test_subsampling_width = (abs((int)subsampl_width - (int)subsampling_width_tmp) <
(allowedPixelDiffOnImageSize * (int)(subsampling_width_tmp / subsampling_width)));
test_subsampling_width = (abs(static_cast<int>(subsampl_width) - static_cast<int>(subsampling_width_tmp)) <
(allowedPixelDiffOnImageSize * static_cast<int>(subsampling_width_tmp / subsampling_width)));
}
if (subsampling_height) {
test_subsampling_height = (abs((int)subsampl_height - (int)subsampling_height_tmp) <
(allowedPixelDiffOnImageSize * (int)(subsampling_height_tmp / subsampling_height)));
test_subsampling_height = (abs(static_cast<int>(subsampl_height) - static_cast<int>(subsampling_height_tmp)) <
(allowedPixelDiffOnImageSize * static_cast<int>(subsampling_height_tmp / subsampling_height)));
}
// if same name && same projection model && same image size camera already exists, we return SEQUENCE_OK
// otherwise it is a new camera that need to be updated and we return SEQUENCE_OK
bool same_name = (cam_name.empty() || (cam_name == camera_name_tmp));
bool same_img_size = (abs((int)im_width - (int)image_width_tmp) < allowedPixelDiffOnImageSize || im_width == 0) &&
(abs((int)im_height - (int)image_height_tmp) < allowedPixelDiffOnImageSize || im_height == 0) &&
bool same_img_size = (abs(static_cast<int>(im_width) - static_cast<int>(image_width_tmp)) < allowedPixelDiffOnImageSize || im_width == 0) &&
(abs(static_cast<int>(im_height) - static_cast<int>(image_height_tmp)) < allowedPixelDiffOnImageSize || im_height == 0) &&
(test_subsampling_width) && (test_subsampling_height);
if (same_name && same_img_size && same_proj_model) {
back = SEQUENCE_OK; // Camera exists
Expand All @@ -332,8 +332,8 @@ class vpXmlParserCamera::Impl
}
#if 0
if (!((projModelFound == true) &&
(abs((int)im_width - (int)image_width_tmp) < allowedPixelDiffOnImageSize || im_width == 0) &&
(abs((int)im_height - (int)image_height_tmp) < allowedPixelDiffOnImageSize || im_height == 0) &&
(abs(static_cast<int>(im_width) - static_cast<int>(image_width_tmp)) < allowedPixelDiffOnImageSize || im_width == 0) &&
(abs(static_cast<int>(im_height) - static_cast<int>(image_height_tmp)) < allowedPixelDiffOnImageSize || im_height == 0) &&
(test_subsampling_width) && (test_subsampling_height))) {
// Same images size, we need to check if the camera have the same name
if (!cam_name.empty() && (cam_name != camera_name_tmp)) {
Expand Down Expand Up @@ -501,8 +501,8 @@ class vpXmlParserCamera::Impl

std::vector<double> fixed_distortion_coeffs;

// In case disortion coefficients are missing, we should complete them with 0 values
// Since 0x3FF is 0011|1111|1111 and we are interrested in the most significant 1s shown below
// In case distortion coefficients are missing, we should complete them with 0 values
// Since 0x3FF is 0011|1111|1111 and we are interested in the most significant 1s shown below
// -- ---
// If we divide by 32 (>> 2^5 : 5 remaining least significant bits), we will have to check 5 bits only
int check = validation / 32;
Expand Down Expand Up @@ -661,21 +661,25 @@ class vpXmlParserCamera::Impl
unsigned int im_height, unsigned int subsampl_width = 0, unsigned int subsampl_height = 0)
{
vpXmlCodeType prop;

for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
if (node.type() != pugi::node_element)
continue;

if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
prop = CODE_XML_OTHER;
}
if (prop == CODE_XML_CAMERA) {
if (SEQUENCE_OK == read_camera_header(node, cam_name, im_width, im_height, subsampl_width, subsampl_height)) {
return node;
pugi::xml_node resNode = pugi::xml_node();

pugi::xml_node node = node_.first_child();
bool hasNotFoundCam = true;
while (node && hasNotFoundCam) {
if (node.type() == pugi::node_element) {
if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
prop = CODE_XML_OTHER;
}
if (prop == CODE_XML_CAMERA) {
if (SEQUENCE_OK == read_camera_header(node, cam_name, im_width, im_height, subsampl_width, subsampl_height)) {
resNode = node;
hasNotFoundCam = false;
}
}
}
node = node.next_sibling();
}
return pugi::xml_node();
return resNode;
}

/*!
Expand Down Expand Up @@ -998,23 +1002,27 @@ class vpXmlParserCamera::Impl
pugi::xml_node find_additional_info(const pugi::xml_node &node_)
{
vpXmlCodeType prop;
pugi::xml_node resNode = pugi::xml_node();

pugi::xml_node node = node_.first_child();
bool hasNotFoundInfo = true;
while (node && hasNotFoundInfo) {
if (node.type() == pugi::node_element) {
if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
prop = CODE_XML_OTHER;
}

for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
if (node.type() != pugi::node_element) {
continue;
}

if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
prop = CODE_XML_OTHER;
if (prop == CODE_XML_ADDITIONAL_INFO) {
// We found the node
resNode = node;
hasNotFoundInfo = false;
}
}

if (prop == CODE_XML_ADDITIONAL_INFO) {
// We found the node
return node;
}
node = node.next_sibling();
}

return pugi::xml_node();
return resNode;
}

/*!
Expand Down
4 changes: 1 addition & 3 deletions modules/core/src/display/vpDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
\brief Generic class for image display.
*/

#include <limits>

#include <visp3/core/vpDisplay.h>
#include <visp3/core/vpDisplayException.h>
#include <visp3/core/vpImageConvert.h>
Expand Down Expand Up @@ -327,6 +325,6 @@ void vpDisplay::setDownScalingFactor(vpScaleType scaleType)
{
if (!m_displayHasBeenInitialized) {
m_scaleType = scaleType;
}
}
}
END_VISP_NAMESPACE
10 changes: 5 additions & 5 deletions modules/core/src/display/vpDisplay_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void vp_display_display_ellipse(const vpImage<Type> &I, const vpImagePoint &cent
double t = (a - b) / (a + b);
t *= t; // t^2
double circumference = (angle / 2.0) * (a + b) * (1.0 + 3.0 * t / (10.0 + sqrt(4.0 - 3.0 * t)));
unsigned int nbpoints = (unsigned int)(floor(circumference / 20));
unsigned int nbpoints = static_cast<unsigned int>(floor(circumference / 20));
if (nbpoints < 10) {
nbpoints = 10;
}
Expand Down Expand Up @@ -563,15 +563,15 @@ template <class Type> void vp_display_display_roi(const vpImage<Type> &I, const
double left = floor(roi.getLeft());
double roiheight = floor(roi.getHeight());
double roiwidth = floor(roi.getWidth());
double iheight = (double)(I.getHeight());
double iwidth = (double)(I.getWidth());
double iheight = static_cast<double>(I.getHeight());
double iwidth = static_cast<double>(I.getWidth());

if (top < 0 || top > iheight || left < 0 || left > iwidth || top + roiheight > iheight || left + roiwidth > iwidth) {
throw(vpException(vpException::dimensionError, "Region of interest outside of the image"));
}

if (I.display != nullptr) {
(I.display)->displayImageROI(I, vpImagePoint(top, left), (unsigned int)roiwidth, (unsigned int)roiheight);
(I.display)->displayImageROI(I, vpImagePoint(top, left), static_cast<unsigned int>(roiwidth), static_cast<unsigned int>(roiheight));
}
}

Expand All @@ -585,7 +585,7 @@ template <class Type> void vp_display_flush(const vpImage<Type> &I)
template <class Type> void vp_display_flush_roi(const vpImage<Type> &I, const vpRect &roi)
{
if (I.display != nullptr) {
(I.display)->flushDisplayROI(roi.getTopLeft(), (unsigned int)roi.getWidth(), (unsigned int)roi.getHeight());
(I.display)->flushDisplayROI(roi.getTopLeft(), static_cast<unsigned int>(roi.getWidth()), static_cast<unsigned int>(roi.getHeight()));
}
}

Expand Down
7 changes: 5 additions & 2 deletions modules/core/src/image/vpCannyEdgeDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,9 @@ vpCannyEdgeDetection::recursiveSearchForStrongEdge(const std::pair<unsigned int,
bool test_col = false;
bool test_drdc = false;
bool edge_in_image_limit = false;
for (int dr = -1; (dr <= 1) && (!hasFoundStrongEdge); ++dr) {
for (int dc = -1; (dc <= 1) && (!hasFoundStrongEdge); ++dc) {
int dr = -1, dc = -1;
while ((dr <= 1) && (!hasFoundStrongEdge)) {
while ((dc <= 1) && (!hasFoundStrongEdge)) {
// reset the check for the edge on image limit
edge_in_image_limit = false;

Expand Down Expand Up @@ -551,7 +552,9 @@ vpCannyEdgeDetection::recursiveSearchForStrongEdge(const std::pair<unsigned int,
// continue - nothing to do
}
}
++dc;
}
++dr;
}
if (hasFoundStrongEdge) {
m_edgePointsCandidates[coordinates] = STRONG_EDGE;
Expand Down
2 changes: 2 additions & 0 deletions modules/core/src/image/vpImagePoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include <visp3/core/vpMath.h>
#include <visp3/core/vpRect.h>

#include <limits> // numeric_limits

BEGIN_VISP_NAMESPACE
/*!
Check if an image point belongs to a rectangle.
Expand Down
1 change: 0 additions & 1 deletion modules/core/src/math/matrix/vpMatrix_svd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

#include <cmath> // std::fabs
#include <iostream>
#include <limits> // numeric_limits

#ifdef VISP_HAVE_EIGEN3
#include <Eigen/SVD>
Expand Down
4 changes: 3 additions & 1 deletion modules/core/src/tools/file/basisu_miniz.h
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,8 @@ static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
#define TDEFL_PUT_BITS_FAST(b, l) { bit_buffer |= (((mz_uint64)(b)) << bits_in); bits_in += (l); }

flags = 1;
for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1) {
pLZ_codes = d->m_lz_code_buf;
while (pLZ_codes < pLZ_code_buf_end) {
if (flags == 1)
flags = *pLZ_codes++ | 0x100;

Expand Down Expand Up @@ -1712,6 +1713,7 @@ static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
pOutput_buf += (bits_in >> 3);
bit_buffer >>= (bits_in & ~7);
bits_in &= 7;
flags >>= 1;
}

#undef TDEFL_PUT_BITS_FAST
Expand Down
Loading

0 comments on commit 63373bd

Please sign in to comment.