SonarLint 警告:降低图像分块的认知复杂度(16 而不是 15)
SonarLint Warning: Reducing the cognitive complexity (16 instead of 15) for chunking an image
我编写了一个函数,其目的是获取从重新缩放的输入图像定义的子图像集合(这些子图像称为 "rescaled chunks of the rescaled input image")并将这些重新缩放的块重新assemble 到输出中整个图像(其尺寸是输入图像的尺寸,重新缩放)。所有块都具有相同的尺寸。
函数定义如下。问题是 SonarLint 警告我认知复杂度是 16 而不是 15,我不想更改这个默认的 SonarLint 限制。 我能做什么?
功能描述如下代码:
final BufferedImage reassembleChunksInASingleImage(final int inputImageWidth, final int inputImageHeight, final int scalingCoefficient, final int chunkWidth, final int chunkHeight, final List<BufferedImage> theChunks) {
logger.log(Level.INFO, "Reassembling...");
final int reassembled_chunks_image_width = scalingCoefficient * inputImageWidth;
final int reassembledChunksImageHeight = scalingCoefficient * inputImageHeight;
final int rescaled_chunk_width = scalingCoefficient * chunkWidth;
final int rescaledChunkHeight = scalingCoefficient * chunkHeight;
final BufferedImage reassembledChunksImage = new BufferedImage(reassembled_chunks_image_width, reassembledChunksImageHeight, BufferedImage.TYPE_INT_RGB);
int indexOfTheChunkToUse = 0;
for(int i = 0; i < reassembled_chunks_image_width; i += rescaled_chunk_width) {
for(int j = 0; j < reassembledChunksImageHeight; j += rescaledChunkHeight) {
final BufferedImage chunkToUse = theChunks.get(indexOfTheChunkToUse);
int iForDraw = i;
int jForDraw = j;
final int deltaI = reassembled_chunks_image_width - (i + rescaled_chunk_width);
final int deltaJ = reassembledChunksImageHeight - (j + rescaledChunkHeight);
if(deltaI < 0) {
iForDraw -= Math.abs(deltaI);
}
if(deltaJ < 0) {
jForDraw -= Math.abs(deltaJ);
}
for(int x = 0; x < rescaled_chunk_width; x++) {
for(int y = 0; y < rescaledChunkHeight; y++) {
int colorToDraw = chunkToUse.getRGB(x, y);
reassembledChunksImage.setRGB(iForDraw + x, jForDraw + y, colorToDraw);
}
}
indexOfTheChunkToUse++;
}
}
logger.log(Level.INFO, "Reassembling done.");
return reassembledChunksImage;
}
我浏览重新缩放的图像,使用块的适当(x 轴或 y 轴)维度(所有块具有相同的维度)增加水平或垂直偏移。相关的循环是:for(int i = 0; i < reassembled_chunks_image_width; i += rescaled_chunk_width) {for(int j = 0; j < reassembledChunksImageHeight; j += rescaledChunkHeight) {
我将当前缩放图像的像素颜色设置为当前块的颜色之一。相关的循环是:for(int x = 0; x < rescaled_chunk_width; x++) {for(int y = 0; y < rescaledChunkHeight; y++) {
.
如果当前块超出了重新缩放图像的范围,我将它向左或向顶部移动,然后绘制它。相关控件是:if(deltaI < 0) {if(deltaJ < 0) {
.
您可以尝试重构您的方法,例如
int iForDraw = getDraw(reassembled_chunks_image_width, rescaled_chunk_width, i);
int jForDraw = getDraw(reassembledChunksImageHeight, rescaledChunkHeight, j);
添加一个小方法,例如getDraw
private int getDraw(int reassembled_chunks_image_data, int rescaled_chunk_data, int index) {
int result = index;
int delta = reassembled_chunks_image_data - (index + rescaled_chunk_data);
if (delta < 0) {
result -= Math.abs(delta);
}
return result;
}
我编写了一个函数,其目的是获取从重新缩放的输入图像定义的子图像集合(这些子图像称为 "rescaled chunks of the rescaled input image")并将这些重新缩放的块重新assemble 到输出中整个图像(其尺寸是输入图像的尺寸,重新缩放)。所有块都具有相同的尺寸。
函数定义如下。问题是 SonarLint 警告我认知复杂度是 16 而不是 15,我不想更改这个默认的 SonarLint 限制。 我能做什么?
功能描述如下代码:
final BufferedImage reassembleChunksInASingleImage(final int inputImageWidth, final int inputImageHeight, final int scalingCoefficient, final int chunkWidth, final int chunkHeight, final List<BufferedImage> theChunks) {
logger.log(Level.INFO, "Reassembling...");
final int reassembled_chunks_image_width = scalingCoefficient * inputImageWidth;
final int reassembledChunksImageHeight = scalingCoefficient * inputImageHeight;
final int rescaled_chunk_width = scalingCoefficient * chunkWidth;
final int rescaledChunkHeight = scalingCoefficient * chunkHeight;
final BufferedImage reassembledChunksImage = new BufferedImage(reassembled_chunks_image_width, reassembledChunksImageHeight, BufferedImage.TYPE_INT_RGB);
int indexOfTheChunkToUse = 0;
for(int i = 0; i < reassembled_chunks_image_width; i += rescaled_chunk_width) {
for(int j = 0; j < reassembledChunksImageHeight; j += rescaledChunkHeight) {
final BufferedImage chunkToUse = theChunks.get(indexOfTheChunkToUse);
int iForDraw = i;
int jForDraw = j;
final int deltaI = reassembled_chunks_image_width - (i + rescaled_chunk_width);
final int deltaJ = reassembledChunksImageHeight - (j + rescaledChunkHeight);
if(deltaI < 0) {
iForDraw -= Math.abs(deltaI);
}
if(deltaJ < 0) {
jForDraw -= Math.abs(deltaJ);
}
for(int x = 0; x < rescaled_chunk_width; x++) {
for(int y = 0; y < rescaledChunkHeight; y++) {
int colorToDraw = chunkToUse.getRGB(x, y);
reassembledChunksImage.setRGB(iForDraw + x, jForDraw + y, colorToDraw);
}
}
indexOfTheChunkToUse++;
}
}
logger.log(Level.INFO, "Reassembling done.");
return reassembledChunksImage;
}
我浏览重新缩放的图像,使用块的适当(x 轴或 y 轴)维度(所有块具有相同的维度)增加水平或垂直偏移。相关的循环是:
for(int i = 0; i < reassembled_chunks_image_width; i += rescaled_chunk_width) {for(int j = 0; j < reassembledChunksImageHeight; j += rescaledChunkHeight) {
我将当前缩放图像的像素颜色设置为当前块的颜色之一。相关的循环是:
for(int x = 0; x < rescaled_chunk_width; x++) {for(int y = 0; y < rescaledChunkHeight; y++) {
.如果当前块超出了重新缩放图像的范围,我将它向左或向顶部移动,然后绘制它。相关控件是:
if(deltaI < 0) {if(deltaJ < 0) {
.
您可以尝试重构您的方法,例如
int iForDraw = getDraw(reassembled_chunks_image_width, rescaled_chunk_width, i);
int jForDraw = getDraw(reassembledChunksImageHeight, rescaledChunkHeight, j);
添加一个小方法,例如getDraw
private int getDraw(int reassembled_chunks_image_data, int rescaled_chunk_data, int index) {
int result = index;
int delta = reassembled_chunks_image_data - (index + rescaled_chunk_data);
if (delta < 0) {
result -= Math.abs(delta);
}
return result;
}