Merge pull request #25931 from zihaomu:clean_code

code clean #25931

Align code and remove redundant CMake code

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
zihaomu 2024-07-18 22:18:37 +08:00 committed by GitHub
parent 0020831414
commit 1125755345
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 70 additions and 47 deletions

View File

@ -213,7 +213,7 @@ file(GLOB_RECURSE dnn_int_hdrs
) )
set(dnn_plugin_srcs ${dnn_srcs} ${dnn_int_hdrs}) set(dnn_plugin_srcs ${dnn_srcs} ${dnn_int_hdrs})
ocv_list_filterout_ex(dnn_plugin_srcs ocv_list_filterout_ex(dnn_plugin_srcs
"/src/dnn.cpp$|/src/dnn_utils.cpp$|/src/dnn_utils.cpp$|/src/dnn_read.cpp$|/src/registry.cpp$|/src/backend.cpp$" "/src/dnn.cpp$|/src/dnn_utils.cpp$|/src/dnn_read.cpp$|/src/registry.cpp$|/src/backend.cpp$"
# importers # importers
"/src/(caffe|darknet|onnx|tensorflow|torch)/" "/src/(caffe|darknet|onnx|tensorflow|torch)/"
# executors # executors

View File

@ -301,8 +301,10 @@ static int RGBE_WriteBytes_RLE(FILE *fp, unsigned char *data, int numbytes)
if (nonrun_count > 128) if (nonrun_count > 128)
nonrun_count = 128; nonrun_count = 128;
buf[0] = static_cast<unsigned char>(nonrun_count); buf[0] = static_cast<unsigned char>(nonrun_count);
if (fwrite(buf,sizeof(buf[0]),1,fp) < 1) if (fwrite(buf,sizeof(buf[0]),1,fp) < 1)
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
if (fwrite(&data[cur],sizeof(data[0])*nonrun_count,1,fp) < 1) if (fwrite(&data[cur],sizeof(data[0])*nonrun_count,1,fp) < 1)
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
cur += nonrun_count; cur += nonrun_count;
@ -311,6 +313,7 @@ static int RGBE_WriteBytes_RLE(FILE *fp, unsigned char *data, int numbytes)
if (run_count >= MINRUNLENGTH) { if (run_count >= MINRUNLENGTH) {
buf[0] = static_cast<unsigned char>(128 + run_count); buf[0] = static_cast<unsigned char>(128 + run_count);
buf[1] = data[beg_run]; buf[1] = data[beg_run];
if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1) if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1)
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
cur += run_count; cur += run_count;
@ -330,19 +333,23 @@ int RGBE_WritePixels_RLE(FILE *fp, float *data, int scanline_width,
if ((scanline_width < 8)||(scanline_width > 0x7fff)) if ((scanline_width < 8)||(scanline_width > 0x7fff))
/* run length encoding is not allowed so write flat*/ /* run length encoding is not allowed so write flat*/
return RGBE_WritePixels(fp,data,scanline_width*num_scanlines); return RGBE_WritePixels(fp,data,scanline_width*num_scanlines);
buffer = (unsigned char *)malloc(sizeof(unsigned char)*4*scanline_width); buffer = (unsigned char *)malloc(sizeof(unsigned char)*4*scanline_width);
if (buffer == NULL) if (buffer == NULL)
/* no buffer space so write flat */ /* no buffer space so write flat */
return RGBE_WritePixels(fp,data,scanline_width*num_scanlines); return RGBE_WritePixels(fp,data,scanline_width*num_scanlines);
while(num_scanlines-- > 0) { while(num_scanlines-- > 0) {
rgbe[0] = 2; rgbe[0] = 2;
rgbe[1] = 2; rgbe[1] = 2;
rgbe[2] = static_cast<unsigned char>(scanline_width >> 8); rgbe[2] = static_cast<unsigned char>(scanline_width >> 8);
rgbe[3] = scanline_width & 0xFF; rgbe[3] = scanline_width & 0xFF;
if (fwrite(rgbe, sizeof(rgbe), 1, fp) < 1) { if (fwrite(rgbe, sizeof(rgbe), 1, fp) < 1) {
free(buffer); free(buffer);
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
} }
for(i=0;i<scanline_width;i++) { for(i=0;i<scanline_width;i++) {
float2rgbe(rgbe,data[RGBE_DATA_RED], float2rgbe(rgbe,data[RGBE_DATA_RED],
data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]); data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]);
@ -352,11 +359,14 @@ int RGBE_WritePixels_RLE(FILE *fp, float *data, int scanline_width,
buffer[i+3*scanline_width] = rgbe[3]; buffer[i+3*scanline_width] = rgbe[3];
data += RGBE_DATA_SIZE; data += RGBE_DATA_SIZE;
} }
/* write out each of the four channels separately run length encoded */ /* write out each of the four channels separately run length encoded */
/* first red, then green, then blue, then exponent */ /* first red, then green, then blue, then exponent */
for(i=0;i<4;i++) { for(i=0;i<4;i++)
{
if ((err = RGBE_WriteBytes_RLE(fp,&buffer[i*scanline_width], if ((err = RGBE_WriteBytes_RLE(fp,&buffer[i*scanline_width],
scanline_width)) != RGBE_RETURN_SUCCESS) { scanline_width)) != RGBE_RETURN_SUCCESS)
{
free(buffer); free(buffer);
return err; return err;
} }
@ -376,6 +386,7 @@ int RGBE_ReadPixels_RLE(FILE *fp, float *data, int scanline_width,
if ((scanline_width < 8)||(scanline_width > 0x7fff)) if ((scanline_width < 8)||(scanline_width > 0x7fff))
/* run length encoding is not allowed so read flat*/ /* run length encoding is not allowed so read flat*/
return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines); return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines);
scanline_buffer = NULL; scanline_buffer = NULL;
/* read in each successive scanline */ /* read in each successive scanline */
while(num_scanlines > 0) { while(num_scanlines > 0) {
@ -383,6 +394,7 @@ int RGBE_ReadPixels_RLE(FILE *fp, float *data, int scanline_width,
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
} }
if ((rgbe[0] != 2)||(rgbe[1] != 2)||(rgbe[2] & 0x80)) { if ((rgbe[0] != 2)||(rgbe[1] != 2)||(rgbe[2] & 0x80)) {
/* this file is not run length encoded */ /* this file is not run length encoded */
rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN],&data[RGBE_DATA_BLUE],rgbe); rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN],&data[RGBE_DATA_BLUE],rgbe);
@ -390,13 +402,15 @@ int RGBE_ReadPixels_RLE(FILE *fp, float *data, int scanline_width,
free(scanline_buffer); free(scanline_buffer);
return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines-1); return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines-1);
} }
if ((((int)rgbe[2])<<8 | rgbe[3]) != scanline_width) { if ((((int)rgbe[2])<<8 | rgbe[3]) != scanline_width) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_format_error,"wrong scanline width"); return rgbe_error(rgbe_format_error,"wrong scanline width");
} }
if (scanline_buffer == NULL) if (scanline_buffer == NULL)
scanline_buffer = (unsigned char *) scanline_buffer = (unsigned char *)malloc(sizeof(unsigned char)*4*scanline_width);
malloc(sizeof(unsigned char)*4*scanline_width);
if (scanline_buffer == NULL) if (scanline_buffer == NULL)
return rgbe_error(rgbe_memory_error,"unable to allocate buffer space"); return rgbe_error(rgbe_memory_error,"unable to allocate buffer space");
@ -404,12 +418,17 @@ int RGBE_ReadPixels_RLE(FILE *fp, float *data, int scanline_width,
/* read each of the four channels for the scanline into the buffer */ /* read each of the four channels for the scanline into the buffer */
for(i=0;i<4;i++) { for(i=0;i<4;i++) {
ptr_end = &scanline_buffer[(i+1)*scanline_width]; ptr_end = &scanline_buffer[(i+1)*scanline_width];
while(ptr < ptr_end) {
if (fread(buf,sizeof(buf[0])*2,1,fp) < 1) { while(ptr < ptr_end)
{
if (fread(buf,sizeof(buf[0])*2,1,fp) < 1)
{
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
} }
if (buf[0] > 128) {
if (buf[0] > 128)
{
/* a run of the same value */ /* a run of the same value */
count = buf[0]-128; count = buf[0]-128;
if ((count == 0)||(count > ptr_end - ptr)) { if ((count == 0)||(count > ptr_end - ptr)) {
@ -419,16 +438,20 @@ int RGBE_ReadPixels_RLE(FILE *fp, float *data, int scanline_width,
while(count-- > 0) while(count-- > 0)
*ptr++ = buf[1]; *ptr++ = buf[1];
} }
else { else
{
/* a non-run */ /* a non-run */
count = buf[0]; count = buf[0];
if ((count == 0)||(count > ptr_end - ptr)) { if ((count == 0)||(count > ptr_end - ptr))
{
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_format_error,"bad scanline data"); return rgbe_error(rgbe_format_error,"bad scanline data");
} }
*ptr++ = buf[1]; *ptr++ = buf[1];
if (--count > 0) { if (--count > 0) {
if (fread(ptr,sizeof(*ptr)*count,1,fp) < 1) { if (fread(ptr,sizeof(*ptr)*count,1,fp) < 1)
{
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
} }
@ -437,14 +460,14 @@ int RGBE_ReadPixels_RLE(FILE *fp, float *data, int scanline_width,
} }
} }
} }
/* now convert data from buffer into floats */ /* now convert data from buffer into floats */
for(i=0;i<scanline_width;i++) { for(i=0;i<scanline_width;i++) {
rgbe[0] = scanline_buffer[i]; rgbe[0] = scanline_buffer[i];
rgbe[1] = scanline_buffer[i+scanline_width]; rgbe[1] = scanline_buffer[i+scanline_width];
rgbe[2] = scanline_buffer[i+2*scanline_width]; rgbe[2] = scanline_buffer[i+2*scanline_width];
rgbe[3] = scanline_buffer[i+3*scanline_width]; rgbe[3] = scanline_buffer[i+3*scanline_width];
rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN], rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN],&data[RGBE_DATA_BLUE],rgbe);
&data[RGBE_DATA_BLUE],rgbe);
data += RGBE_DATA_SIZE; data += RGBE_DATA_SIZE;
} }
num_scanlines--; num_scanlines--;