[IE Samples] make coverity happy (#9203)
* make coverity happy * apply code style
This commit is contained in:
parent
9aedece398
commit
aa457268d4
@ -1,31 +1,72 @@
|
|||||||
#include "bmp_reader.h"
|
#include "bmp_reader.h"
|
||||||
|
|
||||||
|
#include <memory.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int readBmpImage(const char* fileName, BitMap* image) {
|
int readBmpImage(const char* fileName, BitMap* image) {
|
||||||
FILE* input = fopen(fileName, "rb");
|
size_t cnt;
|
||||||
|
int status = 0;
|
||||||
|
FILE* input = 0;
|
||||||
|
|
||||||
if (input == NULL) {
|
if (NULL == fileName || NULL == image) {
|
||||||
printf("[BMP] file %s is not opened\n", fileName);
|
printf("[BMP] bad arguments\n");
|
||||||
return 1;
|
status = -1;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
fread(&image->header.type, 2, 1, input);
|
memset(image, 0, sizeof(BitMap));
|
||||||
|
|
||||||
|
input = fopen(fileName, "rb");
|
||||||
|
if (input == NULL) {
|
||||||
|
printf("[BMP] file %s is not opened\n", fileName);
|
||||||
|
status = 1;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt = fread(&image->header.type, sizeof(image->header.type), sizeof(unsigned char), input);
|
||||||
|
if (cnt != sizeof(image->header.type)) {
|
||||||
|
printf("[BMP] file read error\n");
|
||||||
|
status = 2;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
|
||||||
if (image->header.type != 'M' * 256 + 'B') {
|
if (image->header.type != 'M' * 256 + 'B') {
|
||||||
printf("[BMP] file is not bmp type\n");
|
printf("[BMP] file is not bmp type\n");
|
||||||
return 2;
|
status = 2;
|
||||||
|
goto Exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
fread(&image->header.size, 4, 1, input);
|
cnt = fread(&image->header.size, sizeof(image->header.size), sizeof(unsigned char), input);
|
||||||
fread(&image->header.reserved, 4, 1, input);
|
if (cnt != sizeof(image->header.size)) {
|
||||||
fread(&image->header.offset, 4, 1, input);
|
printf("[BMP] file read error\n");
|
||||||
|
status = 2;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
|
||||||
fread(&image->infoHeader, sizeof(BmpInfoHeader), 1, input);
|
cnt = fread(&image->header.reserved, sizeof(image->header.reserved), sizeof(unsigned char), input);
|
||||||
|
if (cnt != sizeof(image->header.reserved)) {
|
||||||
|
printf("[BMP] file read error\n");
|
||||||
|
status = 2;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt = fread(&image->header.offset, sizeof(image->header.offset), sizeof(unsigned char), input);
|
||||||
|
if (cnt != sizeof(image->header.offset)) {
|
||||||
|
printf("[BMP] file read error\n");
|
||||||
|
status = 2;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt = fread(&image->infoHeader, sizeof(BmpInfoHeader), sizeof(unsigned char), input);
|
||||||
|
if (cnt != sizeof(image->header.offset)) {
|
||||||
|
printf("[BMP] file read error\n");
|
||||||
|
status = 2;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
|
||||||
image->width = image->infoHeader.width;
|
image->width = image->infoHeader.width;
|
||||||
image->height = image->infoHeader.height;
|
image->height = abs(image->infoHeader.height);
|
||||||
|
|
||||||
if (image->infoHeader.bits != 24) {
|
if (image->infoHeader.bits != 24) {
|
||||||
printf("[BMP] 24bpp only supported. But input has: %d\n", image->infoHeader.bits);
|
printf("[BMP] 24bpp only supported. But input has: %d\n", image->infoHeader.bits);
|
||||||
@ -38,21 +79,49 @@ int readBmpImage(const char* fileName, BitMap* image) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int padSize = image->width & 3;
|
int padSize = image->width & 3;
|
||||||
|
size_t row_size = (size_t)image->width * 3;
|
||||||
char pad[3];
|
char pad[3];
|
||||||
size_t size = image->width * image->height * 3;
|
size_t size = row_size * image->height;
|
||||||
|
|
||||||
image->data = malloc(sizeof(char) * size);
|
image->data = malloc(sizeof(char) * size);
|
||||||
|
if (NULL == image->data) {
|
||||||
|
printf("[BMP] memory allocation failed\n");
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
fseek(input, image->header.offset, 0);
|
if (0 != fseek(input, image->header.offset, SEEK_SET)) {
|
||||||
|
printf("[BMP] file seek error\n");
|
||||||
|
status = 2;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
|
||||||
// reading by rows in invert vertically
|
// reading by rows in invert vertically
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < image->height; i++) {
|
for (i = 0; i < image->height; i++) {
|
||||||
unsigned int storeAt = image->infoHeader.height < 0 ? i : (unsigned int)image->height - 1 - i;
|
unsigned int storeAt = image->infoHeader.height < 0 ? i : (unsigned int)image->height - 1 - i;
|
||||||
fread(image->data + image->width * 3 * storeAt, image->width * 3, 1, input);
|
cnt = fread(image->data + row_size * storeAt, row_size, sizeof(unsigned char), input);
|
||||||
fread(pad, padSize, 1, input);
|
if (cnt != row_size) {
|
||||||
|
printf("[BMP] file read error\n");
|
||||||
|
status = 2;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt = fread(pad, padSize, sizeof(unsigned char), input);
|
||||||
|
if (cnt != padSize) {
|
||||||
|
printf("[BMP] file read error\n");
|
||||||
|
status = 2;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(input);
|
Exit:
|
||||||
return 0;
|
if (0 != status && NULL != image && NULL != image->data) {
|
||||||
|
free(image->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NULL != input) {
|
||||||
|
fclose(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user